diff --git a/composer.json b/composer.json index 366cd9cc..ed8a8f9d 100644 --- a/composer.json +++ b/composer.json @@ -6,9 +6,8 @@ "scripts": { "phpcs": "phpcs", "phpcs-fix": "phpcbf", - "test": "wp-env run phpunit 'phpunit -c /var/www/html/wp-content/plugins/vip-block-data-api/phpunit.xml.dist'", - "test-multisite": "wp-env run phpunit 'WP_MULTISITE=1 phpunit -c /var/www/html/wp-content/plugins/vip-block-data-api/phpunit.xml.dist'", - "test-debug": "wp-env run tests-wordpress '/var/www/html/wp-content/plugins/vip-block-data-api/vendor/bin/phpunit -c /var/www/html/wp-content/plugins/vip-block-data-api/phpunit.xml.dist'", + "test": "wp-env run tests-cli --env-cwd=wp-content/plugins/vip-block-data-api ./vendor/bin/phpunit", + "test-multisite": "wp-env run tests-cli --env-cwd=wp-content/plugins/vip-block-data-api /bin/bash -c 'WP_MULTISITE=1 ./vendor/bin/phpunit'", "test-watch": [ "Composer\\Config::disableProcessTimeout", "nodemon -w ./ --ignore vendor/ -e php --exec 'composer run test'" diff --git a/tests/rest/test-rest-api.php b/tests/rest/test-rest-api.php index 8baadcf5..4e30edaf 100644 --- a/tests/rest/test-rest-api.php +++ b/tests/rest/test-rest-api.php @@ -405,7 +405,8 @@ public function test_rest_api_returns_error_for_classic_content() { $post_id = $this->get_post_id_with_content( '
Classic editor content
' ); // Ignore exception created by PHPUnit called when trigger_error() is called internally - $this->expectException( \PHPUnit\Framework\Error\Error::class ); + $this->convert_next_error_to_exception(); + $this->expectExceptionMessage( 'vip-block-data-api-no-blocks' ); $request = new WP_REST_Request( 'GET', sprintf( '/vip-block-data-api/v1/posts/%d/blocks', $post_id ) ); @@ -425,7 +426,8 @@ public function test_rest_api_returns_error_for_include_and_exclude_filter() { $post_id = $this->get_post_id_with_content( 'content
' ); // Ignore exception created by PHPUnit called when trigger_error() is called internally - $this->expectException( \PHPUnit\Framework\Error\Error::class ); + $this->convert_next_error_to_exception(); + $this->expectExceptionMessage( 'vip-block-data-api-invalid-params' ); $request = new WP_REST_Request( 'GET', sprintf( '/vip-block-data-api/v1/posts/%d/blocks', $post_id ) ); $request->set_query_params( array( @@ -454,7 +456,8 @@ public function test_rest_api_returns_error_for_unexpected_exception() { }; // Ignore exception created by PHPUnit called when trigger_error() is called internally - $this->expectException( \PHPUnit\Framework\Error\Error::class ); + $this->convert_next_error_to_exception(); + $this->expectExceptionMessage( 'vip-block-data-api-parser-error' ); add_filter( 'vip_block_data_api__sourced_block_result', $exception_causing_parser_function ); $request = new WP_REST_Request( 'GET', sprintf( '/vip-block-data-api/v1/posts/%d/blocks', $post_id ) ); @@ -479,4 +482,20 @@ private function get_post_id_with_content( $post_content, $post_status = 'publis 'post_status' => $post_status, ] ); } + + private function convert_next_error_to_exception() { + // See https://github.com/sebastianbergmann/phpunit/issues/5062 + // In PHPUnit 10, errors thrown in code can not be caught by expectException(). + // This method is now deprecated. Use this workaround to convert the next error + // to an exception, which can be matched with expectExceptionMessage(). + + // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_set_error_handler -- Used for catching errors in tests. + set_error_handler( + static function ( int $errno, string $errstr ): never { + restore_error_handler(); + throw new \Exception( $errstr, $errno ); + }, + E_USER_WARNING + ); + } }