Make WordPress Core


Ignore:
Timestamp:
02/21/2018 04:24:30 PM (9 years ago)
Author:
SergeyBiryukov
Message:

Tests: Replace use of $this->server with rest_get_server() for better memory recycling.

Props danielbachhuber.
Fixes #41641.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/oembed/controller.php

    r42343 r42724  
    4646                /** @var WP_REST_Server $wp_rest_server */
    4747                global $wp_rest_server;
    48                 $this->server = $wp_rest_server = new Spy_REST_Server();
    49 
    50                 do_action( 'rest_api_init', $this->server );
     48                $wp_rest_server = new Spy_REST_Server;
     49                do_action( 'rest_api_init', $wp_rest_server );
     50
    5151                add_filter( 'pre_http_request', array( $this, 'mock_embed_request' ), 10, 3 );
    5252                $this->request_count = 0;
     
    5555        public function tearDown() {
    5656                parent::tearDown();
     57                /** @var WP_REST_Server $wp_rest_server */
     58                global $wp_rest_server;
     59                $wp_rest_server = null;
    5760
    5861                remove_filter( 'pre_http_request', array( $this, 'mock_embed_request' ), 10 );
     
    180183        public function test_route_availability() {
    181184                // Check the route was registered correctly.
    182                 $filtered_routes = $this->server->get_routes();
     185                $filtered_routes = rest_get_server()->get_routes();
    183186                $this->assertArrayHasKey( '/oembed/1.0/embed', $filtered_routes );
    184187                $route = $filtered_routes['/oembed/1.0/embed'];
     
    201204                $request = new WP_REST_Request( 'POST', '/oembed/1.0/embed' );
    202205
    203                 $response = $this->server->dispatch( $request );
     206                $response = rest_get_server()->dispatch( $request );
    204207                $data     = $response->get_data();
    205208
     
    210213                $request = new WP_REST_Request( 'GET', '/oembed/1.0/embed' );
    211214
    212                 $response = $this->server->dispatch( $request );
     215                $response = rest_get_server()->dispatch( $request );
    213216                $data     = $response->get_data();
    214217
     
    221224                $request->set_param( 'url', 'http://google.com/' );
    222225
    223                 $response = $this->server->dispatch( $request );
     226                $response = rest_get_server()->dispatch( $request );
    224227                $data     = $response->get_data();
    225228
     
    234237                $request->set_param( 'format', 'random' );
    235238
    236                 $response = $this->server->dispatch( $request );
     239                $response = rest_get_server()->dispatch( $request );
    237240                $data     = $response->get_data();
    238241
     
    258261                $request->set_param( 'maxwidth', 400 );
    259262
    260                 $response = $this->server->dispatch( $request );
     263                $response = rest_get_server()->dispatch( $request );
    261264                $data     = $response->get_data();
    262265
     
    301304                $request->set_param( 'maxwidth', 400 );
    302305
    303                 $response = $this->server->dispatch( $request );
     306                $response = rest_get_server()->dispatch( $request );
    304307                $data     = $response->get_data();
    305308
     
    346349                $request->set_param( 'maxwidth', 400 );
    347350
    348                 $response = $this->server->dispatch( $request );
     351                $response = rest_get_server()->dispatch( $request );
    349352                $data     = $response->get_data();
    350353
     
    389392                $request->set_param( 'maxwidth', 400 );
    390393
    391                 $response = $this->server->dispatch( $request );
     394                $response = rest_get_server()->dispatch( $request );
    392395                $data     = $response->get_data();
    393396
     
    415418                $request->set_param( 'format', 'xml' );
    416419
    417                 $response = $this->server->dispatch( $request );
    418                 $output   = get_echo( '_oembed_rest_pre_serve_request', array( true, $response, $request, $this->server ) );
     420                $response = rest_get_server()->dispatch( $request );
     421                $output   = get_echo( '_oembed_rest_pre_serve_request', array( true, $response, $request, rest_get_server() ) );
    419422
    420423                $xml = simplexml_load_string( $output );
     
    429432                $request->set_param( 'format', 'json' );
    430433
    431                 $response = $this->server->dispatch( $request );
    432 
    433                 $this->assertTrue( _oembed_rest_pre_serve_request( true, $response, $request, $this->server ) );
     434                $response = rest_get_server()->dispatch( $request );
     435
     436                $this->assertTrue( _oembed_rest_pre_serve_request( true, $response, $request, rest_get_server() ) );
    434437        }
    435438
     
    441444                $request->set_param( 'format', 'xml' );
    442445
    443                 $response = $this->server->dispatch( $request );
    444 
    445                 $this->assertTrue( _oembed_rest_pre_serve_request( true, $response, $request, $this->server ) );
     446                $response = rest_get_server()->dispatch( $request );
     447
     448                $this->assertTrue( _oembed_rest_pre_serve_request( true, $response, $request, rest_get_server() ) );
    446449        }
    447450
     
    478481                // Test without a login.
    479482                $request  = new WP_REST_Request( 'GET', '/oembed/1.0/proxy' );
    480                 $response = $this->server->dispatch( $request );
     483                $response = rest_get_server()->dispatch( $request );
    481484
    482485                $this->assertEquals( 400, $response->get_status() );
     
    486489                $request = new WP_REST_Request( 'GET', '/oembed/1.0/proxy' );
    487490                $request->set_param( 'url', self::INVALID_OEMBED_URL );
    488                 $response = $this->server->dispatch( $request );
     491                $response = rest_get_server()->dispatch( $request );
    489492
    490493                $this->assertEquals( 403, $response->get_status() );
     
    497500                $request = new WP_REST_Request( 'GET', '/oembed/1.0/proxy' );
    498501                $request->set_param( 'url', self::INVALID_OEMBED_URL );
    499                 $response = $this->server->dispatch( $request );
     502                $response = rest_get_server()->dispatch( $request );
    500503                $this->assertEquals( 404, $response->get_status() );
    501504                $data = $response->get_data();
     
    507510                $request = new WP_REST_Request( 'GET', '/oembed/1.0/proxy' );
    508511                $request->set_param( 'type', 'xml' );
    509                 $response = $this->server->dispatch( $request );
     512                $response = rest_get_server()->dispatch( $request );
    510513
    511514                $this->assertEquals( 400, $response->get_status() );
     
    519522                $request->set_param( 'maxheight', 789 );
    520523                $request->set_param( '_wpnonce', wp_create_nonce( 'wp_rest' ) );
    521                 $response = $this->server->dispatch( $request );
     524                $response = rest_get_server()->dispatch( $request );
    522525                $this->assertEquals( 200, $response->get_status() );
    523526                $this->assertEquals( 1, $this->request_count );
    524527
    525528                // Subsequent request is cached and so it should not cause a request.
    526                 $this->server->dispatch( $request );
     529                rest_get_server()->dispatch( $request );
    527530                $this->assertEquals( 1, $this->request_count );
    528531
     
    534537                $request->set_param( 'maxwidth', 456 );
    535538                $request->set_param( 'maxheight', 789 );
    536                 $response = $this->server->dispatch( $request );
     539                $response = rest_get_server()->dispatch( $request );
    537540                $this->assertEquals( 1, $this->request_count );
    538541
     
    555558                $request->set_param( 'url', self::INVALID_OEMBED_URL );
    556559                $request->set_param( 'discover', 0 );
    557                 $response = $this->server->dispatch( $request );
     560                $response = rest_get_server()->dispatch( $request );
    558561                $this->assertEquals( 404, $response->get_status() );
    559562                $this->assertEquals( 0, $this->request_count );
     
    566569                $request = new WP_REST_Request( 'GET', '/oembed/1.0/proxy' );
    567570                $request->set_param( 'url', self::INVALID_OEMBED_URL );
    568                 $response = $this->server->dispatch( $request );
     571                $response = rest_get_server()->dispatch( $request );
    569572                $this->assertEquals( 404, $response->get_status() );
    570573                $this->assertEquals( 1, $this->request_count );
     
    577580                $request->set_param( 'discover', 'notaboolean' );
    578581
    579                 $response = $this->server->dispatch( $request );
     582                $response = rest_get_server()->dispatch( $request );
    580583
    581584                $this->assertEquals( 400, $response->get_status() );
Note: See TracChangeset for help on using the changeset viewer.