#50366 closed defect (bug) (worksforme)
WordPress rest_api_init action doesn't work in PHPUnit tests
Reported by: | skarabeq | Owned by: | |
---|---|---|---|
Milestone: | Priority: | normal | |
Severity: | normal | Version: | 5.4.2 |
Component: | REST API | Keywords: | |
Focuses: | Cc: |
Description
In the new WordPress 5.4.2 I have the problem with rest API tests.
I have the following PHPUnit test:
<?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.
Change History (15)
#1
@
5 years ago
- Component changed from HTTP API to REST API
- Keywords reporter-feedback added
- Severity changed from blocker to normal
#2
@
5 years ago
Hi, @ocean90
No, I have run tests without plugins and I have removed all other methods. I have run the following test:
<?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; } /** * Dummy test. */ public function testAPI() { $this->assertTrue(true); } }
#3
@
5 years ago
If I remove do_action( 'rest_api_init' );
from the setUp method the tests works well. So i think it is related to this action.
#4
@
5 years ago
I have tried with this test - https://github.com/bobbingwide/wordpress-develop-tests/blob/3ced2b707e159b41b0e2f4be7153259dd5fd019d/phpunit/tests/rest-api/rest-server.php
I have copied the test from the above and the result is the same:
PHPUnit 5.7.27 by Sebastian Bergmann and contributors. F Time: 1.29 seconds, Memory: 38.50MB There was 1 failure: 1) Tests_REST_Server::test_envelope 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
#8
@
5 years ago
- Severity changed from blocker to normal
Do you have any GitHub repository or pull request where one could see the full source code in context?
Changing back the severity because this is not a confirmed critical issue
#9
@
5 years ago
@swissspidy, unfortunately, my repo is private and I can't share it with you. But I found the problem. I have the registered route
<?php $news = new News; register_rest_route('news/', 'posts', [ [ 'methods' => 'GET', 'callback' => [ $news, 'get_items' ], ], 'schema' => [ $news, 'data_schema' ] ]
I have removed the last slash chart and all PHPUnit tests work well. So now it is:
<?php $news = new News; register_rest_route('news', 'posts', [ [ 'methods' => 'GET', 'callback' => [ $news, 'get_items' ], ], 'schema' => [ $news, 'data_schema' ] ]
#10
@
5 years ago
- Keywords reporter-feedback removed
- Milestone Awaiting Review deleted
- Resolution set to worksforme
- Status changed from new to closed
#11
@
5 years ago
- Resolution worksforme deleted
- Status changed from closed to reopened
I think this should be fixed because this error isn't adequate for me! If there is problem with the namespace
parameter of the function register_rest_route
you have to throw adequate error message, like a The namespace parameter has incorrect value.
or something adequate to know what is the real problem!
#12
follow-up:
↓ 13
@
5 years ago
Since [47842] there's a _doing_it_wrong
call saying "Namespace must not start or end with a slash." This will be logged as a E_USER_NOTICE
level message.
How is this message not adequate?
#13
in reply to:
↑ 12
@
5 years ago
Replying to swissspidy:
Since [47842] there's a
_doing_it_wrong
call saying "Namespace must not start or end with a slash." This will be logged as aE_USER_NOTICE
level message.
How is this message not adequate?
@swissspidy, I have not received the message from this ticket [47842]. I have received this one - Unexpected incorrect usage notice for register_rest_route
. For this message I mean is not very clear, what is not correct.
#14
follow-up:
↓ 15
@
5 years ago
- Resolution set to worksforme
- Status changed from reopened to closed
@skarabeq you are receiving that message because you are running unit tests. The test runner expects no incorrect usage notices to occur, but one did occur for the register_rest_route
function. The test runner suppresses the normal error trigger so that it can be unit testable. If you run your code outside of PHPUnit you'll see the full _doing_it_wrong
error.
#15
in reply to:
↑ 14
@
5 years ago
Replying to TimothyBlynJacobs:
@skarabeq you are receiving that message because you are running unit tests. The test runner expects no incorrect usage notices to occur, but one did occur for the
register_rest_route
function. The test runner suppresses the normal error trigger so that it can be unit testable. If you run your code outside of PHPUnit you'll see the full_doing_it_wrong
error.
Thank for the explanation, but for me, it is strange, different messages for the same thing in a different environment. Anyway, for the future, I will know what means that message Unexpected incorrect usage notice for register_rest_route
.
@ocean90, @swissspidy and @TimothyBlynJacobs thank you for support.
Hello @skarabeq, thanks for the report.
This seems to be related to #49749. Are you running the tests with a plugin activated? Are your namespaces still starting/ending with a slash?