Make WordPress Core

Ticket #53844: 53844.diff

File 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

  • new file tests/phpunit/includes/mock-utils.php

    diff --git a/tests/phpunit/includes/mock-utils.php b/tests/phpunit/includes/mock-utils.php
    new file mode 100644
    index 0000000000..f47bbfb2b6
    - +  
     1<?php
     2/**
     3 * Miscellaneous classes for mocking.
     4 *
     5 * @package WordPress
     6 * @subpackage UnitTests
     7 */
     8
     9/**
     10 * Class used to mock a class that has __invoke method
     11 */
     12class Mock_REST_Invokable {
     13
     14        public function __invoke() {}
     15}
  • tests/phpunit/tests/rest-api/rest-request.php

    diff --git a/tests/phpunit/tests/rest-api/rest-request.php b/tests/phpunit/tests/rest-api/rest-request.php
    index 836cb6159d..9b0372ff9d 100644
    a b class Tests_REST_Request extends WP_UnitTestCase { 
    1616                parent::set_up();
    1717
    1818                $this->request = new WP_REST_Request();
     19
     20                // Require necessary files.
     21                require_once DIR_TESTROOT . '/includes/mock-utils.php';
    1922        }
    2023
    2124        public function test_header() {
    class Tests_REST_Request extends WP_UnitTestCase { 
    10141017                $request->set_query_params( array( 'test' => 'value' ) );
    10151018
    10161019                $error    = new WP_Error( 'error_code', __( 'Error Message' ), array( 'status' => 400 ) );
    1017                 $callback = $this->createPartialMock( 'stdClass', array( '__invoke' ) );
     1020                $callback = $this->createPartialMock( 'Mock_REST_Invokable', array( '__invoke' ) );
    10181021                $callback->expects( self::once() )->method( '__invoke' )->with( self::identicalTo( $request ) )->willReturn( $error );
    10191022                $request->set_attributes(
    10201023                        array(
    class Tests_REST_Request extends WP_UnitTestCase { 
    10381041                $request->set_query_params( array( 'test' => 'value' ) );
    10391042
    10401043                $error    = new WP_Error( 'error_code', __( 'Error Message' ), array( 'status' => 400 ) );
    1041                 $callback = $this->createPartialMock( 'stdClass', array( '__invoke' ) );
     1044                $callback = $this->createPartialMock( 'Mock_REST_Invokable', array( '__invoke' ) );
    10421045                $callback->expects( self::once() )->method( '__invoke' )->with( self::identicalTo( $request ) )->willReturn( $error );
    10431046                $request->set_attributes(
    10441047                        array(
    class Tests_REST_Request extends WP_UnitTestCase { 
    10561059                $request = new WP_REST_Request();
    10571060                $request->set_query_params( array( 'test' => 'value' ) );
    10581061
    1059                 $callback = $this->createPartialMock( 'stdClass', array( '__invoke' ) );
     1062                $callback = $this->createPartialMock( 'Mock_REST_Invokable', array( '__invoke' ) );
    10601063                $callback->expects( self::never() )->method( '__invoke' );
    10611064                $request->set_attributes(
    10621065                        array(
  • tests/phpunit/tests/rest-api/rest-server.php

    diff --git a/tests/phpunit/tests/rest-api/rest-server.php b/tests/phpunit/tests/rest-api/rest-server.php
    index 03d1cf21fa..af6a036aa9 100644
    a b class Tests_REST_Server extends WP_Test_REST_TestCase { 
    1818                add_filter( 'wp_rest_server_class', array( $this, 'filter_wp_rest_server_class' ) );
    1919                $GLOBALS['wp_rest_server'] = rest_get_server();
    2020                remove_filter( 'wp_rest_server_class', array( $this, 'filter_wp_rest_server_class' ) );
     21
     22                // Require necessary files.
     23                require_once DIR_TESTROOT . '/includes/mock-utils.php';
    2124        }
    2225
    2326        public function tear_down() {
    class Tests_REST_Server extends WP_Test_REST_TestCase { 
    15731576         * @ticket 50244
    15741577         */
    15751578        public function test_callbacks_are_not_executed_if_request_validation_fails() {
    1576                 $callback = $this->createPartialMock( 'stdClass', array( '__invoke' ) );
     1579                $callback = $this->createPartialMock( 'Mock_REST_Invokable', array( '__invoke' ) );
    15771580                $callback->expects( self::never() )->method( '__invoke' );
    1578                 $permission_callback = $this->createPartialMock( 'stdClass', array( '__invoke' ) );
     1581                $permission_callback = $this->createPartialMock( 'Mock_REST_Invokable', array( '__invoke' ) );
    15791582                $permission_callback->expects( self::never() )->method( '__invoke' );
    15801583
    15811584                register_rest_route(