Skip to content

[REFACTOR] Tests: explicit mock expectations for PHPUnit 14 - #12169

Merged
chfsx merged 2 commits into
ILIAS-eLearning:trunkfrom
srsolutionsag:improvement/12/phpunit-14-mock-expects
Oct 8, 2026
Merged

chfsx merged 2 commits into
ILIAS-eLearning:trunkfrom
srsolutionsag:improvement/12/phpunit-14-mock-expects

Conversation

@chfsx

@chfsx chfsx commented Oct 5, 2026

Copy link
Copy Markdown
Member

PHPUnit 12.5 deprecates calling with*() on a mock without an explicit expects(), as well as the any() invocation count. Both will be removed in PHPUnit 14. This PR removes all PHPUnit deprecations from the unit tests of the components I am an authority of.

Changes

  • with(), withParameterSetsInOrder() and withParameterSetsInAnyOrder() on mocks now get an explicit expects($this->atLeastOnce()). withAnyParameters() without expects() has no effect and is removed.
  • Mocks without any expectation are created as stubs (createStub()) instead of createMock(). This also removes most of the "no expectations configured" notices.

Making the invocation count explicit revealed two mock configurations which were never used:

  • ResourceStorage CollectionTest::testGetCollectionOfUser: the stub for getResourceIdStrings() was never called and is removed.
  • OrgUnit ilOrgUnitOperationRegisteredObjectiveTest: for an unknown context isApplicable() fails before the operation is looked up, so doesOperationExistInContext() now expects never() in that case.

In addition, ilServicesGlobalScreenTest now defines ANONYMOUS_USER_ID itself. It only passed when another test had defined the constant before, and failed when the GlobalScreen tests were run on their own.

Methodology

The changes were made with Rector 2.6.7 (including rector-phpunit) and reviewed afterwards. The configuration:

return RectorConfig::configure()
    ->withComposerBased(phpunit: true)
    ->withRules([
        ExpectsForMockWithRector::class,
    ])
    ->withSkip([
        ParentTestClassConstructorRector::class,
    ]);
  • withComposerBased(phpunit: true) only applies the rector-phpunit rules which match the installed phpunit/phpunit version (13.1). Among others, this set turns mocks without expectations into stubs.
  • ParentTestClassConstructorRector is skipped. It adds a __construct() to TestCase descendants such as ilCtrlPathTestBase, but the TestCase constructor is final since PHPUnit 12.
  • ExpectsForMockWithRector is a small custom rule. It adds expects($this->atLeastOnce()) only to method() chains which use with*(), replaces expects($this->any()) in such chains, and drops withAnyParameters() without expects(). rector-phpunit ships ExplicitMockExpectsCallRector, but that rule only handles mocks in local variables of test methods and adds expects() to every method() call. Most mocks here are properties set up in setUp().
  • The phpunitCodeQuality set was deliberately not used. Its broader refactorings, e.g. finalizing test classes or narrowing setUp() properties, broke several tests.

Affected components

Bibliographic, Cache, Component, EventHandling, File, FileDelivery, FileServices, Filesystem, GlobalScreen, HTTP, Init, MainMenu, OrgUnit, ResourceStorage, Setup, UICore

Only test code is changed. @thibsy, could you have a look at the changes in UICore, Setup, Component and Init? @mjansenDatabay for Init, @maalers / @mhomann-tf for OrgUnit and Bibliographic, in case you want to look at them.

Result

All tests of these components pass without PHPUnit deprecations. The remaining deprecations in the suite are in Test, TestQuestionPool, IndividualAssessment, LearningSequence and ILIASObject.

@thibsy thibsy left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thx a lot @chfsx for the PR!

Changes to UICore, Setup and Init look good. But I quickly ran the tests on your branch locally and there were two errors that stem from FileDelivery. Maybe you need to look into this:

1) ILIAS\Tests\FileDelivery\Delivery\StreamDeliveryIsolationTest::testValidTokenIsServedOnTheContentHost
InvalidArgumentException: The argument $resource must be of type resource but was "boolean"

trunk/components/ILIAS/Filesystem/src/Stream/Streams.php:67
trunk/components/ILIAS/FileDelivery/src/Delivery/StreamDelivery.php:185
trunk/components/ILIAS/FileDelivery/tests/Delivery/StreamDeliveryIsolationTest.php:124
trunk/components/ILIAS/FileDelivery/tests/Delivery/StreamDeliveryIsolationTest.php:168

2) ILIAS\Tests\FileDelivery\Delivery\StreamDeliveryIsolationTest::testValidTokenIsServedOnAnyHostWhenIsolationIsDisabled
InvalidArgumentException: The argument $resource must be of type resource but was "boolean"

trunk/components/ILIAS/Filesystem/src/Stream/Streams.php:67
trunk/components/ILIAS/FileDelivery/src/Delivery/StreamDelivery.php:185
trunk/components/ILIAS/FileDelivery/tests/Delivery/StreamDeliveryIsolationTest.php:124
trunk/components/ILIAS/FileDelivery/tests/Delivery/StreamDeliveryIsolationTest.php:184

There were also more warnings from FileDelivery tests which were caused because of some directory not being found.

Or am I missing something on my machine?

Best, @thibsy

@chfsx

chfsx commented Oct 6, 2026

Copy link
Copy Markdown
Member Author

Thanks, I forgot to rebase on trunk, those issues were fixed there

@chfsx chfsx self-assigned this Oct 6, 2026
@chfsx
chfsx force-pushed the improvement/12/phpunit-14-mock-expects branch from 4a6442d to c8d381b Compare October 8, 2026 07:47
@mjansenDatabay mjansenDatabay added the php Pull requests that update Php code label Oct 8, 2026
@chfsx

chfsx commented Oct 8, 2026

Copy link
Copy Markdown
Member Author

@thibsy the branch is now rebased on trunk. The StreamDeliveryIsolationTest errors and the FileDelivery warnings are gone, all checks pass.

@thibsy

thibsy commented Oct 8, 2026

Copy link
Copy Markdown
Contributor

LGTM, thx a lot!

@chfsx
chfsx merged commit 28276c5 into ILIAS-eLearning:trunk Oct 8, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

php Pull requests that update Php code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants