Make WordPress Core

Opened 3 years ago

Closed 2 years ago

Last modified 2 years ago

#53844 closed defect (bug) (fixed)

Fix four warnings in the test suite

Reported by: jrf's profile jrf Owned by: hellofromtonya's profile hellofromTonya
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)

53844.diff (3.8 KB) - added by sourovroy 3 years ago.
Add a new mock class with invoke method. We will use that class instead of stdClass
53844.2.diff (4.0 KB) - added by sourovroy 2 years ago.
Hi @jrf

Download all attachments as: .zip

Change History (14)

@sourovroy
3 years ago

Add a new mock class with invoke method. We will use that class instead of stdClass

#1 @sourovroy
3 years ago

  • Keywords has-patch added; needs-patch removed

#2 @hellofromTonya
2 years ago

  • Owner set to hellofromTonya
  • Status changed from new to assigned

#3 @hellofromTonya
2 years ago

  • Milestone changed from Awaiting Review to 6.0

#4 @jrf
2 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:

  1. 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 to Mock_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.
  1. Only a few methods in the test classes need this mock. The require_once for the file has been put in the set_up() method, but the file only needs to be includes once no matter what. What about placing the require_once statement in a set_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.

@sourovroy
2 years ago

Hi @jrf

#5 @sourovroy
2 years ago

Hi @jrf,
I have added a new patch by following the requested changes. Please check my new patch 53844.2.diff​.

#6 @jrf
2 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 @hellofromTonya
2 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.

#8 @hellofromTonya
2 years ago

  • Status changed from assigned to reviewing

Prepping commit.

This ticket was mentioned in PR #1934 on WordPress/wordpress-develop by hellofromtonya.


2 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

#10 @hellofromTonya
2 years ago

  • Resolution set to fixed
  • Status changed from reviewing to closed

In 52235:

Build/Test Tools: Fix warnings from stdClass::__invoke() callback mocks in REST API tests.

When running core tests on PHPUnit 8.x and 9.x, four non-blocking warnings were displayed for the REST API tests:

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 are due to the PHP native stdClass not having a __invoke() method declared.

This commit adds a Mock_Invokable reusable class and replaces the stdClass with this new class.

Follow-up to [48945], [48947].

Props sourovroy, jrf.
Fixes #53844.

This ticket was mentioned in Slack in #core by sergey. View the logs.


2 years ago

Note: See TracTickets for help on using tickets.