[wp-trac] [WordPress Trac] #50366: WordPress rest_api_init action doesn't work in PHPUnit tests

WordPress Trac noreply at wordpress.org
Thu Jun 11 07:33:29 UTC 2020


#50366: WordPress rest_api_init action doesn't work in PHPUnit tests
--------------------------+-----------------------------
 Reporter:  skarabeq      |      Owner:  (none)
     Type:  defect (bug)  |     Status:  new
 Priority:  normal        |  Milestone:  Awaiting Review
Component:  HTTP API      |    Version:  5.4.2
 Severity:  blocker       |   Keywords:
  Focuses:                |
--------------------------+-----------------------------
 In the new WordPress 5.4.2 I have the problem with rest API tests.

 I have the following PHPUnit test:

 {{{#!php
 <?php
 class WordPressTestExample extends \WP_UnitTestCase
 {
     /**
      * Test REST Server
      *
      * @var \WP_REST_Server
      */
     private $server;


     /**
      * Initialize WP API rest.
      */
     public function setUp()
     {
         parent::setUp();

         global $wp_rest_server;

         $wp_rest_server = new \WP_REST_Server;
         $this->server   = $wp_rest_server;

         do_action('rest_api_init');
     }

     /**
      * Destruction API WP rest server.
      */
     public function tearDown()
     {
         parent::tearDown();

         global $wp_rest_server;
         $wp_rest_server = null;
     }

     public function testAPI()
     {
         $this->assertTrue(true);
     }
 }
 }}}

 But when I run the PHPUnit tests I have an error:


 {{{
 1) WordPressTestExample::testAPI
 Unexpected incorrect usage notice for register_rest_route
 Failed asserting that an array is empty.

 /tmp/wordpress-tests-lib/includes/abstract-testcase.php:507
 /tmp/wordpress-tests-lib/includes/abstract-testcase.php:519
 }}}

 The problem is generated from `setUp` where I set the Rest server of
 WordPress.
 In the previews version of WordPress, I haven't this problem. It's only in
 new version 5.4.2.

 The version of the PHPUnit which I have use is 5.7.

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/50366>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list