From 0906bc3b23b055d73a08b94af21e38a56b5ef786 Mon Sep 17 00:00:00 2001 From: Alec Geatches Date: Thu, 8 Jun 2023 13:01:33 -0600 Subject: [PATCH 1/6] Test if tests are failing From 0fe041a58e8127f985f3b0ab5564a1856ef7219c Mon Sep 17 00:00:00 2001 From: Alec Geatches Date: Thu, 8 Jun 2023 13:08:11 -0600 Subject: [PATCH 2/6] Update composer test commands to use updated wp-env container --- composer.json | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/composer.json b/composer.json index 366cd9cc..92e82a43 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 /bin/bash -c phpunit", + "test-multisite": "wp-env run tests-cli --env-cwd=wp-content/plugins/vip-block-data-api /bin/bash -c 'WP_MULTISITE=1 phpunit'", "test-watch": [ "Composer\\Config::disableProcessTimeout", "nodemon -w ./ --ignore vendor/ -e php --exec 'composer run test'" From 93af091ac729cc86c447165763fe1c7c9645f331 Mon Sep 17 00:00:00 2001 From: Alec Geatches Date: Fri, 9 Jun 2023 09:30:50 -0600 Subject: [PATCH 3/6] Use separate multisite PHPUnit configuration file --- composer.json | 4 ++-- phpunit-multisite.xml.dist | 20 ++++++++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) create mode 100644 phpunit-multisite.xml.dist diff --git a/composer.json b/composer.json index 92e82a43..fd7362ce 100644 --- a/composer.json +++ b/composer.json @@ -6,8 +6,8 @@ "scripts": { "phpcs": "phpcs", "phpcs-fix": "phpcbf", - "test": "wp-env run tests-cli --env-cwd=wp-content/plugins/vip-block-data-api /bin/bash -c phpunit", - "test-multisite": "wp-env run tests-cli --env-cwd=wp-content/plugins/vip-block-data-api /bin/bash -c 'WP_MULTISITE=1 phpunit'", + "test": "wp-env run tests-cli --env-cwd=wp-content/plugins/vip-block-data-api ./vendor/bin/phpunit -c phpunit.xml.dist", + "test-multisite": "wp-env run tests-cli --env-cwd=wp-content/plugins/vip-block-data-api ./vendor/bin/phpunit -c phpunit-multisite.xml.dist", "test-watch": [ "Composer\\Config::disableProcessTimeout", "nodemon -w ./ --ignore vendor/ -e php --exec 'composer run test'" diff --git a/phpunit-multisite.xml.dist b/phpunit-multisite.xml.dist new file mode 100644 index 00000000..f0a548d4 --- /dev/null +++ b/phpunit-multisite.xml.dist @@ -0,0 +1,20 @@ + + + + + + + + ./tests/ + + + From 5feb6badb3acc676170cf366e58a27a20004055c Mon Sep 17 00:00:00 2001 From: Alec Geatches Date: Fri, 9 Jun 2023 09:44:30 -0600 Subject: [PATCH 4/6] Remove separate multisite configuration, use environment variables --- composer.json | 4 ++-- phpunit-multisite.xml.dist | 20 -------------------- 2 files changed, 2 insertions(+), 22 deletions(-) delete mode 100644 phpunit-multisite.xml.dist diff --git a/composer.json b/composer.json index fd7362ce..ed8a8f9d 100644 --- a/composer.json +++ b/composer.json @@ -6,8 +6,8 @@ "scripts": { "phpcs": "phpcs", "phpcs-fix": "phpcbf", - "test": "wp-env run tests-cli --env-cwd=wp-content/plugins/vip-block-data-api ./vendor/bin/phpunit -c phpunit.xml.dist", - "test-multisite": "wp-env run tests-cli --env-cwd=wp-content/plugins/vip-block-data-api ./vendor/bin/phpunit -c phpunit-multisite.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/phpunit-multisite.xml.dist b/phpunit-multisite.xml.dist deleted file mode 100644 index f0a548d4..00000000 --- a/phpunit-multisite.xml.dist +++ /dev/null @@ -1,20 +0,0 @@ - - - - - - - - ./tests/ - - - From 09ed42edb5bb216fc54b6e7cee383ed0eb39fec6 Mon Sep 17 00:00:00 2001 From: Alec Geatches Date: Fri, 9 Jun 2023 09:44:55 -0600 Subject: [PATCH 5/6] Fix PHPUnit 10 expectException() deprecations --- tests/rest/test-rest-api.php | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/tests/rest/test-rest-api.php b/tests/rest/test-rest-api.php index 8baadcf5..34cac041 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,19 @@ 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(). + + set_error_handler( + static function ( int $errno, string $errstr ): never { + restore_error_handler(); + throw new \Exception( $errstr, $errno ); + }, + E_USER_WARNING + ); + } } From f1a03544fea3d66f9b227a39ec8e49f49a9c59e7 Mon Sep 17 00:00:00 2001 From: Alec Geatches Date: Fri, 9 Jun 2023 09:49:47 -0600 Subject: [PATCH 6/6] Add phpcs:ignore for error handler workaround --- tests/rest/test-rest-api.php | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/rest/test-rest-api.php b/tests/rest/test-rest-api.php index 34cac041..4e30edaf 100644 --- a/tests/rest/test-rest-api.php +++ b/tests/rest/test-rest-api.php @@ -489,6 +489,7 @@ private function convert_next_error_to_exception() { // 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();