Make WordPress Core

Ticket #53844: 53844.2.diff

File 53844.2.diff, 4.0 KB (added by sourovroy, 3 years ago)

Hi @jrf

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

    diff --git a/tests/phpunit/includes/mock-invokable.php b/tests/phpunit/includes/mock-invokable.php
    new file mode 100644
    index 0000000000..03db3e053a
    - +  
     1<?php
     2/**
     3 * File for Mock_Invokable class.
     4 *
     5 * @package WordPress
     6 * @subpackage UnitTests
     7 */
     8
     9/**
     10 * Class Mock_Invokable.
     11 *
     12 * This class is using to mock a class that has __invoke method.
     13 */
     14class Mock_Invokable {
     15
     16        public function __invoke() {}
     17}
  • 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..509c754f65 100644
    a b class Tests_REST_Request extends WP_UnitTestCase { 
    1818                $this->request = new WP_REST_Request();
    1919        }
    2020
     21        /**
     22         * Called before setting up all tests.
     23         */
     24        public static function set_up_before_class() {
     25                parent::set_up_before_class();
     26
     27                // Require files that need to load once.
     28                require_once DIR_TESTROOT . '/includes/mock-invokable.php';
     29        }
     30
    2131        public function test_header() {
    2232                $value = 'application/x-wp-example';
    2333
    class Tests_REST_Request extends WP_UnitTestCase { 
    10141024                $request->set_query_params( array( 'test' => 'value' ) );
    10151025
    10161026                $error    = new WP_Error( 'error_code', __( 'Error Message' ), array( 'status' => 400 ) );
    1017                 $callback = $this->createPartialMock( 'stdClass', array( '__invoke' ) );
     1027                $callback = $this->createPartialMock( 'Mock_Invokable', array( '__invoke' ) );
    10181028                $callback->expects( self::once() )->method( '__invoke' )->with( self::identicalTo( $request ) )->willReturn( $error );
    10191029                $request->set_attributes(
    10201030                        array(
    class Tests_REST_Request extends WP_UnitTestCase { 
    10381048                $request->set_query_params( array( 'test' => 'value' ) );
    10391049
    10401050                $error    = new WP_Error( 'error_code', __( 'Error Message' ), array( 'status' => 400 ) );
    1041                 $callback = $this->createPartialMock( 'stdClass', array( '__invoke' ) );
     1051                $callback = $this->createPartialMock( 'Mock_Invokable', array( '__invoke' ) );
    10421052                $callback->expects( self::once() )->method( '__invoke' )->with( self::identicalTo( $request ) )->willReturn( $error );
    10431053                $request->set_attributes(
    10441054                        array(
    class Tests_REST_Request extends WP_UnitTestCase { 
    10561066                $request = new WP_REST_Request();
    10571067                $request->set_query_params( array( 'test' => 'value' ) );
    10581068
    1059                 $callback = $this->createPartialMock( 'stdClass', array( '__invoke' ) );
     1069                $callback = $this->createPartialMock( 'Mock_Invokable', array( '__invoke' ) );
    10601070                $callback->expects( self::never() )->method( '__invoke' );
    10611071                $request->set_attributes(
    10621072                        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 30977958fa..990cf30e79 100644
    a b class Tests_REST_Server extends WP_Test_REST_TestCase { 
    2828                parent::tear_down();
    2929        }
    3030
     31        /**
     32         * Called before setting up all tests.
     33         */
     34        public static function set_up_before_class() {
     35                parent::set_up_before_class();
     36
     37                // Require files that need to load once.
     38                require_once DIR_TESTROOT . '/includes/mock-invokable.php';
     39        }
     40
    3141        public function test_envelope() {
    3242                $data    = array(
    3343                        'amount of arbitrary data' => 'alot',
    class Tests_REST_Server extends WP_Test_REST_TestCase { 
    15731583         * @ticket 50244
    15741584         */
    15751585        public function test_callbacks_are_not_executed_if_request_validation_fails() {
    1576                 $callback = $this->createPartialMock( 'stdClass', array( '__invoke' ) );
     1586                $callback = $this->createPartialMock( 'Mock_Invokable', array( '__invoke' ) );
    15771587                $callback->expects( self::never() )->method( '__invoke' );
    1578                 $permission_callback = $this->createPartialMock( 'stdClass', array( '__invoke' ) );
     1588                $permission_callback = $this->createPartialMock( 'Mock_Invokable', array( '__invoke' ) );
    15791589                $permission_callback->expects( self::never() )->method( '__invoke' );
    15801590
    15811591                register_rest_route(