From 09a2050fca40250036b222474572749dd3c45b7e Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Thu, 23 Nov 2023 14:39:16 +0000 Subject: [PATCH] Tests: Avoid an infinite loop in `Spy_REST_Server` if a non-existing method is called. Follow-up to [34928]. Props xknown, joemcgill. Fixes #59601. git-svn-id: https://develop.svn.wordpress.org/trunk@57133 602fd350-edb4-49c9-b593-d223f7449a82 --- tests/phpunit/includes/spy-rest-server.php | 4 ++++ tests/phpunit/tests/rest-api.php | 5 +++++ 2 files changed, 9 insertions(+) diff --git a/tests/phpunit/includes/spy-rest-server.php b/tests/phpunit/includes/spy-rest-server.php index 49cda8748a..4266a3de56 100644 --- a/tests/phpunit/includes/spy-rest-server.php +++ b/tests/phpunit/includes/spy-rest-server.php @@ -25,6 +25,10 @@ class Spy_REST_Server extends WP_REST_Server { * @return mixed */ public function __call( $method, $args ) { + if ( ! method_exists( $this, $method ) ) { + throw new Error( sprintf( 'Call to undefined method %s::%s()', get_class( $this ), $method ) ); + } + return call_user_func_array( array( $this, $method ), $args ); } diff --git a/tests/phpunit/tests/rest-api.php b/tests/phpunit/tests/rest-api.php index 4a575b8803..7b0ee18f68 100644 --- a/tests/phpunit/tests/rest-api.php +++ b/tests/phpunit/tests/rest-api.php @@ -31,6 +31,11 @@ class Tests_REST_API extends WP_UnitTestCase { return 'Spy_REST_Server'; } + public function test_rest_get_server_fails_with_undefined_method() { + $this->expectException( Error::class ); + rest_get_server()->does_not_exist(); + } + /** * Checks that the main classes are loaded. */