From d4b2fae8381193784956e17e35a20c7881ffdda2 Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Thu, 10 Jun 2021 19:18:15 +0000 Subject: [PATCH] Tests: Use `assertSameSets()` in some newly introduced tests. This ensures that not only the array values being compared are equal, but also that their type is the same. Going forward, stricter type checking by using `assertSameSets()` or `assertSameSetsWithIndex()` should generally be preferred, to make the tests more reliable. Follow-up to [48939], [49925], [50157], [50959], [50960], [50995], [51079]. See #52625. git-svn-id: https://develop.svn.wordpress.org/trunk@51137 602fd350-edb4-49c9-b593-d223f7449a82 --- .../tests/rest-api/rest-posts-controller.php | 2 +- .../tests/rest-api/rest-themes-controller.php | 6 ++-- .../rest-api/rest-widget-types-controller.php | 6 ++-- .../rest-api/rest-widgets-controller.php | 30 +++++++++---------- tests/phpunit/tests/theme/wpThemeJson.php | 10 +++---- 5 files changed, 27 insertions(+), 27 deletions(-) diff --git a/tests/phpunit/tests/rest-api/rest-posts-controller.php b/tests/phpunit/tests/rest-api/rest-posts-controller.php index 54e00c1dff..b167e4a629 100644 --- a/tests/phpunit/tests/rest-api/rest-posts-controller.php +++ b/tests/phpunit/tests/rest-api/rest-posts-controller.php @@ -1155,7 +1155,7 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te $response = rest_get_server()->dispatch( $request ); $data = $response->get_data(); - $this->assertEqualSets( $post_ids, array_column( $data, 'id' ) ); + $this->assertSameSets( $post_ids, array_column( $data, 'id' ) ); } /** diff --git a/tests/phpunit/tests/rest-api/rest-themes-controller.php b/tests/phpunit/tests/rest-api/rest-themes-controller.php index 3e4d4103dd..a228a81319 100644 --- a/tests/phpunit/tests/rest-api/rest-themes-controller.php +++ b/tests/phpunit/tests/rest-api/rest-themes-controller.php @@ -219,7 +219,7 @@ class WP_Test_REST_Themes_Controller extends WP_Test_REST_Controller_Testcase { 'theme_uri', 'version', ); - $this->assertEqualSets( $fields, array_keys( $data[0] ) ); + $this->assertSameSets( $fields, array_keys( $data[0] ) ); $this->assertContains( 'twentytwenty', wp_list_pluck( $data, 'stylesheet' ) ); $this->assertNotContains( get_stylesheet(), wp_list_pluck( $data, 'stylesheet' ) ); @@ -1236,8 +1236,8 @@ class WP_Test_REST_Themes_Controller extends WP_Test_REST_Controller_Testcase { ); $fields_links = array( 'collection', 'self' ); - $this->assertEqualSets( $fields, array_keys( $data ) ); - $this->assertEqualSets( $fields_links, array_keys( $links ) ); + $this->assertSameSets( $fields, array_keys( $data ) ); + $this->assertSameSets( $fields_links, array_keys( $links ) ); } /** diff --git a/tests/phpunit/tests/rest-api/rest-widget-types-controller.php b/tests/phpunit/tests/rest-api/rest-widget-types-controller.php index b09117ed94..d415294759 100644 --- a/tests/phpunit/tests/rest-api/rest-widget-types-controller.php +++ b/tests/phpunit/tests/rest-api/rest-widget-types-controller.php @@ -402,7 +402,7 @@ class WP_Test_REST_Widget_Types_Controller extends WP_Test_REST_Controller_Testc "\t\t\t", $data['preview'] ); - $this->assertEqualSets( + $this->assertSameSets( array( 'encoded' => base64_encode( serialize( array( 'title' => 'Test title' ) ) ), 'hash' => wp_hash( serialize( array( 'title' => 'Test title' ) ) ), @@ -438,7 +438,7 @@ class WP_Test_REST_Widget_Types_Controller extends WP_Test_REST_Controller_Testc "\t\t\t", $data['preview'] ); - $this->assertEqualSets( + $this->assertSameSets( array( 'encoded' => base64_encode( serialize( array( 'title' => 'Updated title' ) ) ), 'hash' => wp_hash( serialize( array( 'title' => 'Updated title' ) ) ), @@ -482,7 +482,7 @@ class WP_Test_REST_Widget_Types_Controller extends WP_Test_REST_Controller_Testc "\t\t\t", $data['preview'] ); - $this->assertEqualSets( + $this->assertSameSets( array( 'encoded' => base64_encode( serialize( array( 'title' => 'Test title' ) ) ), 'hash' => wp_hash( serialize( array( 'title' => 'Test title' ) ) ), diff --git a/tests/phpunit/tests/rest-api/rest-widgets-controller.php b/tests/phpunit/tests/rest-api/rest-widgets-controller.php index 93453e20c2..fc815e7aa2 100644 --- a/tests/phpunit/tests/rest-api/rest-widgets-controller.php +++ b/tests/phpunit/tests/rest-api/rest-widgets-controller.php @@ -272,12 +272,13 @@ class WP_Test_REST_Widgets_Controller extends WP_Test_REST_Controller_Testcase { $response = rest_get_server()->dispatch( $request ); $data = $response->get_data(); $data = $this->remove_links( $data ); - $this->assertEqualSets( + $this->assertSameSets( array( array( 'id' => 'block-1', 'id_base' => 'block', 'sidebar' => 'sidebar-1', + 'rendered' => '

Block test

', 'instance' => array( 'encoded' => base64_encode( serialize( @@ -297,12 +298,12 @@ class WP_Test_REST_Widgets_Controller extends WP_Test_REST_Controller_Testcase { 'content' => $block_content, ), ), - 'rendered' => '

Block test

', ), array( 'id' => 'rss-1', 'id_base' => 'rss', 'sidebar' => 'sidebar-1', + 'rendered' => 'RSS RSS test', 'instance' => array( 'encoded' => base64_encode( serialize( @@ -321,14 +322,13 @@ class WP_Test_REST_Widgets_Controller extends WP_Test_REST_Controller_Testcase { ) ), ), - 'rendered' => 'RSS RSS test', ), array( 'id' => 'testwidget', 'id_base' => 'testwidget', 'sidebar' => 'sidebar-1', - 'instance' => null, 'rendered' => '

Default id

Default text', + 'instance' => null, ), ), $data @@ -447,11 +447,12 @@ class WP_Test_REST_Widgets_Controller extends WP_Test_REST_Controller_Testcase { $request = new WP_REST_Request( 'GET', '/wp/v2/widgets/text-1' ); $response = rest_get_server()->dispatch( $request ); $data = $response->get_data(); - $this->assertEqualSets( + $this->assertSameSets( array( 'id' => 'text-1', 'id_base' => 'text', 'sidebar' => 'sidebar-1', + 'rendered' => '
Custom text test
', 'instance' => array( 'encoded' => base64_encode( serialize( @@ -471,7 +472,6 @@ class WP_Test_REST_Widgets_Controller extends WP_Test_REST_Controller_Testcase { 'text' => 'Custom text test', ), ), - 'rendered' => '
Custom text test
', ), $data ); @@ -565,7 +565,7 @@ class WP_Test_REST_Widgets_Controller extends WP_Test_REST_Controller_Testcase { $data = $response->get_data(); $this->assertSame( 'text-2', $data['id'] ); $this->assertSame( 'sidebar-1', $data['sidebar'] ); - $this->assertEqualSets( + $this->assertSameSetsWithIndex( array( 'text' => 'Updated text test', 'title' => '', @@ -657,7 +657,7 @@ class WP_Test_REST_Widgets_Controller extends WP_Test_REST_Controller_Testcase { $data = $response->get_data(); $this->assertSame( 'block-7', $data['id'] ); $this->assertSame( 'sidebar-1', $data['sidebar'] ); - $this->assertEqualSets( + $this->assertSameSets( array( 'content' => '

Block test

', ), @@ -721,7 +721,7 @@ class WP_Test_REST_Widgets_Controller extends WP_Test_REST_Controller_Testcase { $data = $response->get_data(); $this->assertSame( 'text-2', $data['id'] ); $this->assertSame( 'sidebar-1', $data['sidebar'] ); - $this->assertEqualSets( + $this->assertSameSetsWithIndex( array( 'text' => 'Updated text test', 'title' => '', @@ -756,7 +756,7 @@ class WP_Test_REST_Widgets_Controller extends WP_Test_REST_Controller_Testcase { $data = $response->get_data(); $this->assertSame( 'text-2', $data['id'] ); $this->assertSame( 'sidebar-1', $data['sidebar'] ); - $this->assertEqualSets( + $this->assertSameSetsWithIndex( array( 'text' => 'Text 1', 'title' => '', @@ -779,7 +779,7 @@ class WP_Test_REST_Widgets_Controller extends WP_Test_REST_Controller_Testcase { $data = $response->get_data(); $this->assertSame( 'text-3', $data['id'] ); $this->assertSame( 'sidebar-1', $data['sidebar'] ); - $this->assertEqualSets( + $this->assertSameSetsWithIndex( array( 'text' => 'Text 2', 'title' => '', @@ -827,7 +827,7 @@ class WP_Test_REST_Widgets_Controller extends WP_Test_REST_Controller_Testcase { $data = $response->get_data(); $this->assertSame( 'text-2', $data['id'] ); $this->assertSame( 'sidebar-1', $data['sidebar'] ); - $this->assertEqualSets( + $this->assertSameSetsWithIndex( array( 'text' => 'Updated text test', 'title' => '', @@ -874,7 +874,7 @@ class WP_Test_REST_Widgets_Controller extends WP_Test_REST_Controller_Testcase { $this->assertSame( 'text-1', $data['id'] ); $this->assertSame( 'sidebar-1', $data['sidebar'] ); - $this->assertEqualSets( + $this->assertSameSetsWithIndex( array( 'text' => 'Updated text test', 'title' => '', @@ -963,7 +963,7 @@ class WP_Test_REST_Widgets_Controller extends WP_Test_REST_Controller_Testcase { $this->assertSame( 'text-1', $data['id'] ); $this->assertSame( 'sidebar-1', $data['sidebar'] ); - $this->assertEqualSets( + $this->assertSameSetsWithIndex( array( 'text' => 'Updated text test', 'title' => '', @@ -1170,7 +1170,7 @@ class WP_Test_REST_Widgets_Controller extends WP_Test_REST_Controller_Testcase { $response = rest_get_server()->dispatch( $request ); $data = $response->get_data(); - $this->assertEqualSets( + $this->assertSameSetsWithIndex( array( 'text' => 'Updated \\" \\\' text test', 'title' => '', diff --git a/tests/phpunit/tests/theme/wpThemeJson.php b/tests/phpunit/tests/theme/wpThemeJson.php index f936bf53c3..1945272495 100644 --- a/tests/phpunit/tests/theme/wpThemeJson.php +++ b/tests/phpunit/tests/theme/wpThemeJson.php @@ -61,7 +61,7 @@ class Tests_Theme_wpThemeJson extends WP_UnitTestCase { ), ); - $this->assertEqualSetsWithIndex( $expected, $actual ); + $this->assertSameSetsWithIndex( $expected, $actual ); } function test_get_stylesheet() { @@ -656,7 +656,7 @@ class Tests_Theme_wpThemeJson extends WP_UnitTestCase { ); $actual = WP_Theme_JSON::get_from_editor_settings( array() ); - $this->assertEqualSetsWithIndex( $expected, $actual ); + $this->assertSameSetsWithIndex( $expected, $actual ); } /** @@ -673,7 +673,7 @@ class Tests_Theme_wpThemeJson extends WP_UnitTestCase { $actual = WP_Theme_JSON::get_from_editor_settings( $input ); - $this->assertEqualSetsWithIndex( $expected, $actual['settings']['spacing'] ); + $this->assertSameSetsWithIndex( $expected, $actual['settings']['spacing'] ); } /** @@ -690,7 +690,7 @@ class Tests_Theme_wpThemeJson extends WP_UnitTestCase { $actual = WP_Theme_JSON::get_from_editor_settings( $input ); - $this->assertEqualSetsWithIndex( $expected, $actual['settings']['spacing'] ); + $this->assertSameSetsWithIndex( $expected, $actual['settings']['spacing'] ); } /** @@ -707,7 +707,7 @@ class Tests_Theme_wpThemeJson extends WP_UnitTestCase { $actual = WP_Theme_JSON::get_from_editor_settings( $input ); - $this->assertEqualSetsWithIndex( $expected, $actual['settings']['spacing'] ); + $this->assertSameSetsWithIndex( $expected, $actual['settings']['spacing'] ); } }