Repository navigation
Conversation
The `getMockedStdClassWithMethods()` helper relied on `getMockBuilder()` combined with `addMethods()`/`setMethods()` to add non-existent methods to `stdClass`. These methods are deprecated or removed in recent PHPUnit versions, which blocks upgrading the test suite. Replace the mock with a small `CallbackRecorder` fixture which records the arguments of every call made to it, and assert against the recorded calls. This works independently of the PHPUnit version and on PHP 5.6. Fixes WordPress#814
Allow `CallbackRecorder` instances to share a call order log, so `testDispatchWithMultipleRegisteredHooks()` also verifies that the callbacks are called in priority order, not only how often and with which arguments. In `testAfterRequestCallback()`, check the number of arguments before checking their types, so a change in the dispatched arguments results in a clear assertion failure instead of an undefined array key error.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Pull Request Type
This is a:
Context
Fixes #814
The test suite used PHPUnit mock objects in four places to verify that hook callbacks are called, how often and with which arguments.
These mocks were created via the
getMockedStdClassWithMethods()helper, which usesgetMockBuilder( stdClass::class )combined withaddMethods()(PHPUnit 8.3+) orsetMethods()(older PHPUnit) to add non-existent methods tostdClass.setMethods()was already removed in PHPUnit 10, andaddMethods()andgetMockBuilder()are on their way out as well, so this would block running the test suite on PHPUnit 11/12.Detailed Description
As the tests only need to know which calls a callback received, there is no need for a mocking framework here.
This PR adds a small
Tests\Fixtures\CallbackRecorderclass.It is an invokable object which records the arguments of every call made to it, which the tests can retrieve via
get_calls().The tests now register a recorder as the hook callback and assert against the recorded calls.
This does not use any of the PHPUnit mock API, so it works independently of the PHPUnit version, and it does not use anonymous classes, so it still runs on PHP 5.6.
Changed tests:
Hooks\DispatchTest::testDispatchWithSingleRegisteredHook()Hooks\DispatchTest::testDispatchWithMultipleRegisteredHooks()- uses one recorder per callback, and still verifies that the callback registered twice onhook_bis called twice, and that the callback onhook_ais not called.Transport\BaseTestCase::testProgressCallback()Transport\BaseTestCase::testAfterRequestCallback()- verifies the argument types for every call, as thewith()expectation did before.The now unused
getMockedStdClassWithMethods()helper has been removed from theTestCase.I considered Mockery, as suggested in the issue, but with only four, fairly simple, call verifications, adding a new dev dependency (which would need to support the full PHP 5.6 - 8.x and PHPUnit 5.7 - 12 range) did not seem worth it.
Happy to switch if you prefer that route though.
Quality assurance
Tested locally with PHP 8.5 and PHPUnit 10.5 (
composer lint,composer checkcsandcomposer test10).To make sure the new assertions are not vacuous, I also temporarily made
Hooks::dispatch()return early without calling any callbacks, which made all six affected test runs (the twoDispatchTesttests, plus both transport tests for Curl and Fsockopen) fail with the expected messages.