Skip to content

Conversation

Shadow243
Copy link
Member

@Shadow243 Shadow243 commented Sep 18, 2025

New Test Suites

  • tests/phpunit/modules/core/mailbox.php – Tests for Hm_Mailbox

    • Bridge pattern for IMAP, JMAP, EWS, SMTP
    • Connection/authentication handling
    • Search across protocols
    • Error handling and edge cases
  • tests/phpunit/lib/searchable.php – Tests for Searchable trait

    • getBy() method with parameters and filters
    • Edge cases and errors
  • tests/phpunit/lib/scram_authenticator.php – SCRAM authentication

    • Hash algorithm detection/validation
    • Client proof for multiple algorithms
    • Authentication flow
  • tests/phpunit/lib/ini_set.php – PHP configuration validation

    • Session security, compression, open_basedir
    • Compatibility: PHP 7.3+, 8.0+, 8.1

@Shadow243 Shadow243 changed the title feat(unit) add comprehensive unit test suites for core library classes & lib test(unit) add comprehensive unit test suites for core library classes & lib Sep 18, 2025
@Shadow243 Shadow243 changed the title test(unit) add comprehensive unit test suites for core library classes & lib test(unit): add comprehensive unit test suites for core library classes & lib Sep 18, 2025
@Shadow243 Shadow243 force-pushed the phpunit-0 branch 5 times, most recently from 2db6857 to 856f038 Compare September 18, 2025 17:35
@Shadow243 Shadow243 marked this pull request as draft September 18, 2025 17:36
@Shadow243 Shadow243 force-pushed the phpunit-0 branch 5 times, most recently from 6ec3c46 to ac583ca Compare September 18, 2025 18:13
@Shadow243 Shadow243 marked this pull request as ready for review September 18, 2025 18:13
@Shadow243 Shadow243 requested a review from kroky September 18, 2025 18:13

public function setUp(): void {
if (!defined('APP_PATH')) {
require_once __DIR__.'/../bootstrap.php';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Proper bootstrap means we don't call these inside the unit tests themselves. Why would the app path not be defined? Tests should be executed by a single phpunit command which should always properly bootstrap the framework.

}
// Load mocks first
if (!class_exists('Hm_Mock_Session', false)) {
require_once APP_PATH.'tests/phpunit/mocks.php';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not the correct place to include such files. Do it in the bootstrap system.

}
}

trait Mock_Searchable {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

None of the classes or traits called mocks are actual mocks - you copy/implement details from the actual code in the test suite - this is counterproductive as code will diverge over time. See the documentation on how to mock objects https://docs.phpunit.de/en/9.6/test-doubles.html#mock-objects - you add some expectations to methods that should be called and what should be returned but you actually work with the real objects and not some classes that resemble the real classes partially. In theory, it is best to follow this pattern:

  • use the object itself whenever possible
  • if testing becomes complicated as the original class depends on 3rd party external connections, you can mock the result of some of its methods and return what you want to test
  • note that merely testing the return of data that you supplied as a mock is not a productive test. The test objective is to test code that runs in the framework and not code that is written in the tests themselves. So, whenever you want to test a method, see what external connections it uses, mock them and actually call that method on that original class/object and test that it works correctly. There is a subtle but important difference here. The way you did it with these mocks is that you test the mocks themselves which doesn't make sense.

Also, you shouldn't use reflection to set private properties or do similar changes that were not designed to be done with the class in question. If you want to test something that's not available for some reason, do:

  • think if it actually needs to be tested. Private methods usually are not tested. Testing the public interface of the class is enough to ensure it works fine as it calls private methods internally, so they become part of the tests.
  • if something private really needs to be tested, refactor the original class/interface and make it public or separate it to parts that are testable
    Refactoring original code because of testing is a legitimate use-case, so don't be afraid to do it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kroky I’ve applied all the requested corrections and fixed the other failing tests.

For Hm_Test_Mailbox, I added some imports in setUp to avoid conflicts. These are only required there and not elsewhere.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In addition, in hm-jmap.php I added a few missing compatibility functions (is_supported, get_message_sort_order, and sort_by_fetch) with TODOs and comments. These provide placeholders so we can later implement proper JMAP behavior and confirm everything works correctly with JMAP as well.

@Shadow243 Shadow243 marked this pull request as draft September 20, 2025 11:31
@Shadow243 Shadow243 force-pushed the phpunit-0 branch 9 times, most recently from 4415838 to 8970896 Compare September 22, 2025 23:47
@Shadow243 Shadow243 force-pushed the phpunit-0 branch 15 times, most recently from da74152 to 0a4b43f Compare September 23, 2025 01:11
@Shadow243 Shadow243 marked this pull request as ready for review September 23, 2025 01:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants