From 96bad40b0ccfde564e3d9b09520c7506fe36f2ba Mon Sep 17 00:00:00 2001 From: Dovid Levine Date: Wed, 25 Sep 2024 12:26:35 +0300 Subject: [PATCH] tests: fix `setUp()`/`tearDown()` methods (#290) --- .changeset/dull-pandas-joke.md | 5 +++++ tests/unit/AutoloaderTest.php | 5 ++--- tests/unit/BlockAttributesObjectTest.php | 9 +++++---- tests/unit/BlockQueriesTest.php | 9 +++++---- tests/unit/BlockSupportsAnchorTest.php | 5 +++-- tests/unit/ContentBlocksResolverTest.php | 7 +++++-- tests/unit/CoreImageTest.php | 8 +++++--- tests/unit/CoreTableTest.php | 10 +++++----- tests/unit/CoreVideoTest.php | 10 +++++----- tests/unit/EditorBlockInterfaceTest.php | 4 ++++ tests/unit/PostTypeBlockInterfaceTest.php | 1 + tests/unit/RegistryTestCase.php | 1 + tests/unit/UpdateCallbacksTest.php | 5 ++++- 13 files changed, 50 insertions(+), 29 deletions(-) create mode 100644 .changeset/dull-pandas-joke.md diff --git a/.changeset/dull-pandas-joke.md b/.changeset/dull-pandas-joke.md new file mode 100644 index 00000000..fc90f01b --- /dev/null +++ b/.changeset/dull-pandas-joke.md @@ -0,0 +1,5 @@ +--- +"@wpengine/wp-graphql-content-blocks": patch +--- + +tests: fix `setUp()`/`tearDown()` methods to prevent PHPUnit lifecycle issues. diff --git a/tests/unit/AutoloaderTest.php b/tests/unit/AutoloaderTest.php index 47515c3b..e22d63c1 100644 --- a/tests/unit/AutoloaderTest.php +++ b/tests/unit/AutoloaderTest.php @@ -34,15 +34,14 @@ public function testAutoload() { } public function testRequireAutoloader() { - $reflection = new \ReflectionClass( $this->autoloader ); - $is_loaded_property = $reflection->getProperty( 'is_loaded' ); + $reflection = new \ReflectionClass( $this->autoloader ); + $is_loaded_property = $reflection->getProperty( 'is_loaded' ); $is_loaded_property->setAccessible( true ); $is_loaded_property->setValue( $this->autoloader, false ); $method = $reflection->getMethod( 'require_autoloader' ); $method->setAccessible( true ); - $this->assertTrue( $method->invokeArgs( $this->autoloader, [ WPGRAPHQL_CONTENT_BLOCKS_PLUGIN_DIR . '/vendor/autoload.php' ] ) ); $is_loaded_property->setValue( $this->autoloader, false ); diff --git a/tests/unit/BlockAttributesObjectTest.php b/tests/unit/BlockAttributesObjectTest.php index a1f6a97a..5cb193b5 100644 --- a/tests/unit/BlockAttributesObjectTest.php +++ b/tests/unit/BlockAttributesObjectTest.php @@ -9,8 +9,6 @@ final class BlockAttributesObjectTest extends PluginTestCase { public function setUp(): void { parent::setUp(); - global $wpdb; - $this->post_id = wp_insert_post( [ 'post_title' => 'Post Title', @@ -27,13 +25,16 @@ public function setUp(): void { 'post_status' => 'publish', ] ); + + \WPGraphQL::clear_schema(); } public function tearDown(): void { // your tear down methods here - parent::tearDown(); - wp_delete_post( $this->post_id, true ); + \WPGraphQL::clear_schema(); + + parent::tearDown(); } public function test_resolve_attribute_object_type() { diff --git a/tests/unit/BlockQueriesTest.php b/tests/unit/BlockQueriesTest.php index ec60e8cd..0c7e11e4 100644 --- a/tests/unit/BlockQueriesTest.php +++ b/tests/unit/BlockQueriesTest.php @@ -9,8 +9,6 @@ final class BlockQueriesTest extends PluginTestCase { public function setUp(): void { parent::setUp(); - global $wpdb; - $this->post_id = wp_insert_post( [ 'post_title' => 'Post Title', @@ -38,13 +36,16 @@ public function setUp(): void { 'post_status' => 'publish', ] ); + + \WPGraphQL::clear_schema(); } public function tearDown(): void { // your tear down methods here - parent::tearDown(); - wp_delete_post( $this->post_id, true ); + \WPGraphQL::clear_schema(); + + parent::tearDown(); } public function test_retrieve_non_flatten_editor_blocks() { diff --git a/tests/unit/BlockSupportsAnchorTest.php b/tests/unit/BlockSupportsAnchorTest.php index d75103e1..cbf8074c 100644 --- a/tests/unit/BlockSupportsAnchorTest.php +++ b/tests/unit/BlockSupportsAnchorTest.php @@ -48,10 +48,11 @@ public function setUp(): void { public function tearDown(): void { // your tear down methods here - parent::tearDown(); - wp_delete_post( $this->post_id, true ); + delete_option( 'graphql_general_settings' ); \WPGraphQL::clear_schema(); + + parent::tearDown(); } /** diff --git a/tests/unit/ContentBlocksResolverTest.php b/tests/unit/ContentBlocksResolverTest.php index 5920d69f..ae7f4d4c 100644 --- a/tests/unit/ContentBlocksResolverTest.php +++ b/tests/unit/ContentBlocksResolverTest.php @@ -83,15 +83,18 @@ public function setUp(): void { ); $this->instance = new ContentBlocksResolver(); + + \WPGraphQL::clear_schema(); } public function tearDown(): void { // your tear down methods here - parent::tearDown(); - wp_delete_post( $this->post_id, true ); wp_delete_post( $this->reusable_post_id, true ); wp_delete_post( $this->reusable_block_id, true ); + \WPGraphQL::clear_schema(); + + parent::tearDown(); } public function test_resolve_content_blocks_resolves_reusable_blocks() { diff --git a/tests/unit/CoreImageTest.php b/tests/unit/CoreImageTest.php index ea3fee56..18c32a8f 100644 --- a/tests/unit/CoreImageTest.php +++ b/tests/unit/CoreImageTest.php @@ -10,7 +10,6 @@ final class CoreImageTest extends PluginTestCase { public function setUp(): void { parent::setUp(); - global $wpdb; $this->attachment_id = $this->factory->attachment->create_upload_object( WP_TEST_DATA_DIR . '/images/test-image.jpg' ); $this->post_id = wp_insert_post( @@ -30,13 +29,16 @@ public function setUp(): void { 'post_status' => 'publish', ] ); + + \WPGraphQL::clear_schema(); } public function tearDown(): void { // your tear down methods here - parent::tearDown(); - wp_delete_post( $this->post_id, true ); + \WPGraphQL::clear_schema(); + + parent::tearDown(); } public function test_retrieve_core_image_media_details() { diff --git a/tests/unit/CoreTableTest.php b/tests/unit/CoreTableTest.php index 24fe7e93..56dee256 100644 --- a/tests/unit/CoreTableTest.php +++ b/tests/unit/CoreTableTest.php @@ -9,8 +9,6 @@ final class CoreTableTest extends PluginTestCase { public function setUp(): void { parent::setUp(); - global $wpdb; - $this->post_id = wp_insert_post( [ 'post_title' => 'Post Title', @@ -31,16 +29,18 @@ public function setUp(): void { 'post_status' => 'publish', ] ); + + \WPGraphQL::clear_schema(); } public function tearDown(): void { - parent::tearDown(); - wp_delete_post( $this->post_id, true ); + \WPGraphQL::clear_schema(); + + parent::tearDown(); } public function test_retrieve_core_table_attribute_fields() { - $this->markTestSkipped( 'must be revisited since the test is failing on the CI for an unknown reason' ); $query = ' fragment CoreTableBlockFragment on CoreTable { attributes { diff --git a/tests/unit/CoreVideoTest.php b/tests/unit/CoreVideoTest.php index bdb1ddf2..7a455b38 100644 --- a/tests/unit/CoreVideoTest.php +++ b/tests/unit/CoreVideoTest.php @@ -9,8 +9,6 @@ final class CoreVideoTest extends PluginTestCase { public function setUp(): void { parent::setUp(); - global $wpdb; - $this->post_id = wp_insert_post( [ 'post_title' => 'Post Title', @@ -28,17 +26,19 @@ public function setUp(): void { 'post_status' => 'publish', ] ); + + \WPGraphQL::clear_schema(); } public function tearDown(): void { // your tear down methods here - parent::tearDown(); - wp_delete_post( $this->post_id, true ); + \WPGraphQL::clear_schema(); + + parent::tearDown(); } public function test_retrieve_core_video_attributes() { - $this->markTestSkipped( 'must be revisited since the test is failing on the CI for an unknown reason' ); $query = ' fragment CoreVideoBlockFragment on CoreVideo { attributes { diff --git a/tests/unit/EditorBlockInterfaceTest.php b/tests/unit/EditorBlockInterfaceTest.php index fb83d807..45c89456 100644 --- a/tests/unit/EditorBlockInterfaceTest.php +++ b/tests/unit/EditorBlockInterfaceTest.php @@ -11,10 +11,14 @@ public function setUp(): void { $settings = get_option( 'graphql_general_settings', [] ); $settings['public_introspection_enabled'] = 'on'; update_option( 'graphql_general_settings', $settings ); + + \WPGraphQL::clear_schema(); } public function tearDown(): void { // your tear down methods here + delete_option( 'graphql_general_settings' ); + \WPGraphQL::clear_schema(); // then parent::tearDown(); diff --git a/tests/unit/PostTypeBlockInterfaceTest.php b/tests/unit/PostTypeBlockInterfaceTest.php index b2e79f33..f52198da 100644 --- a/tests/unit/PostTypeBlockInterfaceTest.php +++ b/tests/unit/PostTypeBlockInterfaceTest.php @@ -20,6 +20,7 @@ public function setUp(): void { public function tearDown(): void { // your tear down methods here + delete_option( 'graphql_general_settings' ); \WPGraphQL::clear_schema(); parent::tearDown(); diff --git a/tests/unit/RegistryTestCase.php b/tests/unit/RegistryTestCase.php index 2a2bb22e..2b891800 100644 --- a/tests/unit/RegistryTestCase.php +++ b/tests/unit/RegistryTestCase.php @@ -24,6 +24,7 @@ public function setUp(): void { public function tearDown(): void { // your tear down methods here + delete_option( 'graphql_general_settings' ); \WPGraphQL::clear_schema(); parent::tearDown(); diff --git a/tests/unit/UpdateCallbacksTest.php b/tests/unit/UpdateCallbacksTest.php index 48df9352..b3323bee 100644 --- a/tests/unit/UpdateCallbacksTest.php +++ b/tests/unit/UpdateCallbacksTest.php @@ -1,8 +1,11 @@