Skip to content

Commit

Permalink
Merge pull request #33 from Automattic/fix/wp-env-test-run
Browse files Browse the repository at this point in the history
Fix test runner with latest version of wp-env
  • Loading branch information
alecgeatches authored Jun 9, 2023
2 parents ef35774 + f1a0354 commit cdcbc1f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
5 changes: 2 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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'"
Expand Down
25 changes: 22 additions & 3 deletions tests/rest/test-rest-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,8 @@ public function test_rest_api_returns_error_for_classic_content() {
$post_id = $this->get_post_id_with_content( '<p>Classic editor content</p>' );

// 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 ) );

Expand All @@ -425,7 +426,8 @@ public function test_rest_api_returns_error_for_include_and_exclude_filter() {
$post_id = $this->get_post_id_with_content( '<!-- wp:paragraph --><p>content</p><!-- /wp:paragraph -->' );

// 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(
Expand Down Expand Up @@ -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 ) );
Expand All @@ -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
);
}
}

0 comments on commit cdcbc1f

Please sign in to comment.