#53844 closed defect (bug) (fixed)
Fix four warnings in the test suite
Reported by: |
|
Owned by: |
|
---|---|---|---|
Milestone: | 5.9 | Priority: | normal |
Severity: | normal | Version: | |
Component: | Build/Test Tools | Keywords: | has-patch commit has-unit-tests |
Focuses: | rest-api | Cc: |
Description
Follow up after the projected changes for ticket #46149, which are expected to be committed soon.
When the WP Core tests are run on PHPUnit 8.x and 9.x, four - non-blocking - warnings will be displayed:
There were 4 warnings: 1) Tests_REST_Request::test_route_level_validate_callback createPartialMock called with method(s) __invoke that do not exist in stdClass. This will not be allowed in future versions of PHPUnit. 2) Tests_REST_Request::test_route_level_validate_callback_no_parameter_callbacks createPartialMock called with method(s) __invoke that do not exist in stdClass. This will not be allowed in future versions of PHPUnit. 3) Tests_REST_Request::test_route_level_validate_callback_is_not_executed_if_parameter_validation_fails createPartialMock called with method(s) __invoke that do not exist in stdClass. This will not be allowed in future versions of PHPUnit. 4) Tests_REST_Server::test_callbacks_are_not_executed_if_request_validation_fails createPartialMock called with method(s) __invoke that do not exist in stdClass. This will not be allowed in future versions of PHPUnit.
These warnings relate to the following code pattern which is used in all four of the referenced tests:
<?php $callback = $this->createPartialMock( 'stdClass', array( '__invoke' ) );
The PHP native stdClass
class does not have the __invoke()
method declared.
As these warnings are currently non-blocking, they will not be addressed in ticket #46149. However, they should be addressed in the foreseeable future to prevent this becoming a blocker once PHPUnit 10 will be released.
Typically, this could be solved by declaring a dummy anonymous class with an __invoke()
method, used only in the test suite, but anonymous classes is a PHP 7.0+ feature and can therefore not be used.
I suspect the best way to solve these warnings in a PHP cross-version compatible manner, will be to create a dummy class with an __invoke()
method, as a test fixture and switching out the use of stdClass
in these test methods in favour of using the dummy class.
Attachments (2)
Change History (14)
#4
@
3 years ago
@sourovroy Thank you for creating a patch for this ticket and sorry it took me a while to get to reviewing it.
I've had a good look at your patch now and would like to suggest the following changes:
- Generally speaking it is preferred (and will become a hard rule in the future) to have only one class per file. The
mock-utils.php
file looks to be set up to potentially contain more classes in the future.
I'd like to recommend the following changes to that file/class:
- Rename the class from
Mock_REST_Invokable
toMock_Invokable
. There is nothing REST specific in the mock class, so no need to give the impression that the mock can only be used for REST API tests. - Rename the file to match the class, i.e.
mock-invokable.php
. - Adjust the file docblock to match.
- Only a few methods in the test classes need this mock. The
require_once
for the file has been put in theset_up()
method, but the file only needs to be includes once no matter what. What about placing therequire_once
statement in aset_up_before_class()
method instead ? That way the file hit will only happen once for each test class, instead of multiple times.
Other than that, this is 100% the sort of solution I envisioned and once the above mentioned tweaks have been made, I think this patch can go into Core.
#5
@
3 years ago
Hi @jrf,
I have added a new patch by following the requested changes. Please check my new patch 53844.2.diff.
#6
@
3 years ago
- Keywords commit added
@sourovroy Thanks for making these changes. This is ready for commit.
@hellofromTonya As this is a test only change, can this go into 5.9 ?
#7
@
3 years ago
- Milestone changed from 6.0 to 5.9
@jrf @sourovroy Yes this can go in as 5.9 is still in defect phase and Beta 1 has not yet been released.
This ticket was mentioned in PR #1934 on WordPress/wordpress-develop by hellofromtonya.
3 years ago
#9
- Keywords has-unit-tests added
Applies 53844.2.diff patch and adds the new mock class to skip the WordPress.Files.Filename
phpcs sniff for its file naming.
Created the PR as a confidence check.
Trac ticket: https://core.trac.wordpress.org/ticket/53844
hellofromtonya commented on PR #1934:
3 years ago
#11
Committed via changeset https://core.trac.wordpress.org/changeset/52235.
Add a new mock class with invoke method. We will use that class instead of stdClass