Make WordPress Core

Changeset 52146


Ignore:
Timestamp:
11/12/2021 01:45:05 PM (5 years ago)
Author:
hellofromTonya
Message:

Build/Test Tools: Mock no results remote request in WP_REST_Block_Directory_Controller:: get_items().

  • Refactors the mock logic to a helper function for reuse in multiple tests.
  • Mocks the remote request in WP_REST_Block_Directory_Controller_Test:: test_get_items_no_results() using the mock helper.

Follow-up to [48242], [52137].

Props hellofromTonya, sergeybiryukov.
See #54420.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/rest-api/rest-block-directory-controller.php

    r52137 r52146  
    7070        public function test_get_items() {
    7171                wp_set_current_user( self::$admin_id );
    72                 add_filter(
    73                         'pre_http_request',
    74                         static function() {
    75                                 /*
    76                                  * Mocks the request to:
    77                                  * https://api.wordpress.org/plugins/info/1.2/?action=query_plugins&request%5Bblock%5D=foo&request%5Bper_page%5D=10&request%5Bpage%5D=1&request%5Blocale%5D=en_US&request%5Bwp_version%5D=5.9
    78                                  */
    79                                 return array(
    80                                         'headers'  => array(),
    81                                         'response' => array(
    82                                                 'code'    => 200,
    83                                                 'message' => 'OK',
    84                                         ),
    85                                         'body'     => '{"info":{"page":1,"pages":0,"results":0},"plugins":[]}',
    86                                         'cookies'  => array(),
    87                                         'filename' => null,
    88                                 );
    89                         }
     72                $this->mock_remote_request(
     73                        array(
     74                                'body' => '{"info":{"page":1,"pages":0,"results":0},"plugins":[]}',
     75                        )
    9076                );
    9177
    … …  
    129115        public function test_get_items_no_results() {
    130116                wp_set_current_user( self::$admin_id );
     117                $this->mock_remote_request(
     118                        array(
     119                                'body' => '{"info":{"page":1,"pages":0,"results":0},"plugins":[]}',
     120                        )
     121                );
    131122
    132123                $request = new WP_REST_Request( 'GET', '/wp/v2/block-directory/search' );
    … …  
    307298                );
    308299        }
     300
     301        /**
     302         * Mocks the remote request via `'pre_http_request'` filter by
     303         * returning the expected response.
     304         *
     305         * @since 5.9.0
     306         *
     307         * @param array $expected Expected response, which is merged with the default response.
     308         */
     309        private function mock_remote_request( array $expected ) {
     310                add_filter(
     311                        'pre_http_request',
     312                        static function() use ( $expected ) {
     313                                $default = array(
     314                                        'headers'  => array(),
     315                                        'response' => array(
     316                                                'code'    => 200,
     317                                                'message' => 'OK',
     318                                        ),
     319                                        'body'     => '',
     320                                        'cookies'  => array(),
     321                                        'filename' => null,
     322                                );
     323                                return array_merge( $default, $expected );
     324                        }
     325                );
     326        }
    309327}
Note: See TracChangeset for help on using the changeset viewer.