Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content

Tests: stop using the PHPUnit mock builder for hook callbacks - #1115

Open
yaman-694 wants to merge 2 commits into
WordPress:developfrom
yaman-694:tests/814-replace-mock-builder
Open

yaman-694 wants to merge 2 commits into
WordPress:developfrom
yaman-694:tests/814-replace-mock-builder

Conversation

@yaman-694

Copy link
Copy Markdown

Pull Request Type

  • I have checked there is no other PR open for the same change.

This is a:

  • Bug fix
  • New feature
  • Documentation improvement
  • Code quality improvement

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 uses getMockBuilder( stdClass::class ) combined with addMethods() (PHPUnit 8.3+) or setMethods() (older PHPUnit) to add non-existent methods to stdClass.

setMethods() was already removed in PHPUnit 10, and addMethods() and getMockBuilder() 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\CallbackRecorder class.
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 on hook_b is called twice, and that the callback on hook_a is not called.
  • Transport\BaseTestCase::testProgressCallback()
  • Transport\BaseTestCase::testAfterRequestCallback() - verifies the argument types for every call, as the with() expectation did before.

The now unused getMockedStdClassWithMethods() helper has been removed from the TestCase.

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

  • This change does NOT contain a breaking change (fix or feature that would cause existing functionality to change).
  • I have commented my code, particularly in hard-to-understand areas.
  • I have added unit tests to accompany this PR.
  • The (new/existing) tests cover this PR 100%.
  • I have (manually) tested this code to the best of my abilities.
  • My code follows the style guidelines of this project.

Tested locally with PHP 8.5 and PHPUnit 10.5 (composer lint, composer checkcs and composer 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 two DispatchTest tests, plus both transport tests for Curl and Fsockopen) fail with the expected messages.

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

No deployments
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.

Tests: investigate alternatives for mocking

1 participant