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.

git-svn-id: https://develop.svn.wordpress.org/trunk@52235 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Tonya Mork 2021-11-23 18:55:23 +00:00
parent c16bc1228c
commit 0725caf9a0
4 changed files with 43 additions and 5 deletions

View File

@ -249,6 +249,7 @@
<element value="WP_REST_Test_Controller"/>
<element value="WP_Image_Editor_Mock"/>
<element value="WP_Filesystem_MockFS"/>
<element value="Mock_Invokable"/>
<element value="MockPHPMailer"/>
<element value="MockAction"/>
<element value="WP_Object_Cache"/>

View File

@ -0,0 +1,17 @@
<?php
/**
* File for Mock_Invokable class.
*
* @package WordPress
* @subpackage UnitTests
*/
/**
* Class Mock_Invokable.
*
* This class is using to mock a class that has __invoke method.
*/
class Mock_Invokable {
public function __invoke() {}
}

View File

@ -18,6 +18,16 @@ class Tests_REST_Request extends WP_UnitTestCase {
$this->request = new WP_REST_Request();
}
/**
* Called before setting up all tests.
*/
public static function set_up_before_class() {
parent::set_up_before_class();
// Require files that need to load once.
require_once DIR_TESTROOT . '/includes/mock-invokable.php';
}
public function test_header() {
$value = 'application/x-wp-example';
@ -1014,7 +1024,7 @@ class Tests_REST_Request extends WP_UnitTestCase {
$request->set_query_params( array( 'test' => 'value' ) );
$error = new WP_Error( 'error_code', __( 'Error Message' ), array( 'status' => 400 ) );
$callback = $this->createPartialMock( 'stdClass', array( '__invoke' ) );
$callback = $this->createPartialMock( 'Mock_Invokable', array( '__invoke' ) );
$callback->expects( self::once() )->method( '__invoke' )->with( self::identicalTo( $request ) )->willReturn( $error );
$request->set_attributes(
array(
@ -1038,7 +1048,7 @@ class Tests_REST_Request extends WP_UnitTestCase {
$request->set_query_params( array( 'test' => 'value' ) );
$error = new WP_Error( 'error_code', __( 'Error Message' ), array( 'status' => 400 ) );
$callback = $this->createPartialMock( 'stdClass', array( '__invoke' ) );
$callback = $this->createPartialMock( 'Mock_Invokable', array( '__invoke' ) );
$callback->expects( self::once() )->method( '__invoke' )->with( self::identicalTo( $request ) )->willReturn( $error );
$request->set_attributes(
array(
@ -1056,7 +1066,7 @@ class Tests_REST_Request extends WP_UnitTestCase {
$request = new WP_REST_Request();
$request->set_query_params( array( 'test' => 'value' ) );
$callback = $this->createPartialMock( 'stdClass', array( '__invoke' ) );
$callback = $this->createPartialMock( 'Mock_Invokable', array( '__invoke' ) );
$callback->expects( self::never() )->method( '__invoke' );
$request->set_attributes(
array(

View File

@ -35,6 +35,16 @@ class Tests_REST_Server extends WP_Test_REST_TestCase {
parent::tear_down();
}
/**
* Called before setting up all tests.
*/
public static function set_up_before_class() {
parent::set_up_before_class();
// Require files that need to load once.
require_once DIR_TESTROOT . '/includes/mock-invokable.php';
}
public function test_envelope() {
$data = array(
'amount of arbitrary data' => 'alot',
@ -1630,9 +1640,9 @@ class Tests_REST_Server extends WP_Test_REST_TestCase {
* @ticket 50244
*/
public function test_callbacks_are_not_executed_if_request_validation_fails() {
$callback = $this->createPartialMock( 'stdClass', array( '__invoke' ) );
$callback = $this->createPartialMock( 'Mock_Invokable', array( '__invoke' ) );
$callback->expects( self::never() )->method( '__invoke' );
$permission_callback = $this->createPartialMock( 'stdClass', array( '__invoke' ) );
$permission_callback = $this->createPartialMock( 'Mock_Invokable', array( '__invoke' ) );
$permission_callback->expects( self::never() )->method( '__invoke' );
register_rest_route(