Skip to content

Commit 8be943d

Browse files
Tests: Introduce assertSameSets() and assertSameSetsWithIndex(), and use them where appropriate.
This ensures that not only the array values being compared are equal, but also that their type is the same. These new methods replace most of the existing instances of `assertEqualSets()` and `assertEqualSetsWithIndex()`. Going forward, stricter type checking by using `assertSameSets()` or `assertSameSetsWithIndex()` should generally be preferred, to make the tests more reliable. Follow-up to [48937]. See #38266. git-svn-id: https://develop.svn.wordpress.org/trunk@48939 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 5e2a75e commit 8be943d

105 files changed

Lines changed: 757 additions & 729 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

tests/phpunit/includes/abstract-testcase.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -684,6 +684,20 @@ public function assertEqualsIgnoreEOL( $expected, $actual ) {
684684
$this->assertSameIgnoreEOL( $expected, $actual );
685685
}
686686

687+
/**
688+
* Asserts that the contents of two un-keyed, single arrays are the same, without accounting for the order of elements.
689+
*
690+
* @since 5.6.0
691+
*
692+
* @param array $expected Expected array.
693+
* @param array $actual Array to check.
694+
*/
695+
public function assertSameSets( $expected, $actual ) {
696+
sort( $expected );
697+
sort( $actual );
698+
$this->assertSame( $expected, $actual );
699+
}
700+
687701
/**
688702
* Asserts that the contents of two un-keyed, single arrays are equal, without accounting for the order of elements.
689703
*
@@ -698,6 +712,20 @@ public function assertEqualSets( $expected, $actual ) {
698712
$this->assertEquals( $expected, $actual );
699713
}
700714

715+
/**
716+
* Asserts that the contents of two keyed, single arrays are the same, without accounting for the order of elements.
717+
*
718+
* @since 5.6.0
719+
*
720+
* @param array $expected Expected array.
721+
* @param array $actual Array to check.
722+
*/
723+
public function assertSameSetsWithIndex( $expected, $actual ) {
724+
ksort( $expected );
725+
ksort( $actual );
726+
$this->assertSame( $expected, $actual );
727+
}
728+
701729
/**
702730
* Asserts that the contents of two keyed, single arrays are equal, without accounting for the order of elements.
703731
*

tests/phpunit/tests/admin/includesCommunityEvents.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ public function test_get_events_valid_response() {
163163
$response = $this->instance->get_events();
164164

165165
$this->assertNotWPError( $response );
166-
$this->assertEqualSetsWithIndex( $this->get_user_location(), $response['location'] );
166+
$this->assertSameSetsWithIndex( $this->get_user_location(), $response['location'] );
167167
$this->assertSame( gmdate( 'l, M j, Y', strtotime( 'next Sunday 1pm' ) ), $response['events'][0]['formatted_date'] );
168168
$this->assertSame( '1:00 pm', $response['events'][0]['formatted_time'] );
169169

@@ -184,7 +184,7 @@ public function test_get_cached_events_valid_response() {
184184
$cached_events = $this->instance->get_cached_events();
185185

186186
$this->assertNotWPError( $cached_events );
187-
$this->assertEqualSetsWithIndex( $this->get_user_location(), $cached_events['location'] );
187+
$this->assertSameSetsWithIndex( $this->get_user_location(), $cached_events['location'] );
188188
$this->assertSame( gmdate( 'l, M j, Y', strtotime( 'next Sunday 1pm' ) ), $cached_events['events'][0]['formatted_date'] );
189189
$this->assertSame( '1:00 pm', $cached_events['events'][0]['formatted_time'] );
190190

tests/phpunit/tests/admin/includesTemplate.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ public function test_get_settings_errors_sources() {
161161
set_transient( 'settings_errors', array( $blogname_error ) );
162162
$wp_settings_errors = null;
163163
add_settings_error( $blogdescription_error['setting'], $blogdescription_error['code'], $blogdescription_error['message'], $blogdescription_error['type'] );
164-
$this->assertEqualSets( array( $blogname_error, $blogdescription_error ), get_settings_errors() );
164+
$this->assertSameSets( array( $blogname_error, $blogdescription_error ), get_settings_errors() );
165165

166166
$wp_settings_errors = null;
167167
}

tests/phpunit/tests/admin/includesTheme.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ function test_page_templates() {
4848

4949
switch_theme( $theme['Template'], $theme['Stylesheet'] );
5050

51-
$this->assertEqualSetsWithIndex(
51+
$this->assertSameSetsWithIndex(
5252
array(
5353
'Top Level' => 'template-top-level.php',
5454
'Sub Dir' => 'subdir/template-sub-dir.php',
@@ -62,7 +62,7 @@ function test_page_templates() {
6262

6363
switch_theme( $theme['Template'], $theme['Stylesheet'] );
6464

65-
$this->assertEqualSetsWithIndex(
65+
$this->assertSameSetsWithIndex(
6666
array(
6767
'Top Level' => 'template-top-level.php',
6868
'Sub Dir' => 'subdir/template-sub-dir.php',
@@ -81,14 +81,14 @@ function test_page_templates_different_post_types() {
8181

8282
switch_theme( $theme['Template'], $theme['Stylesheet'] );
8383

84-
$this->assertEqualSetsWithIndex(
84+
$this->assertSameSetsWithIndex(
8585
array(
8686
'Top Level' => 'template-top-level-post-types.php',
8787
'Sub Dir' => 'subdir/template-sub-dir-post-types.php',
8888
),
8989
get_page_templates( null, 'foo' )
9090
);
91-
$this->assertEqualSetsWithIndex(
91+
$this->assertSameSetsWithIndex(
9292
array(
9393
'Top Level' => 'template-top-level-post-types.php',
9494
'Sub Dir' => 'subdir/template-sub-dir-post-types.php',
@@ -107,7 +107,7 @@ function test_page_templates_for_post_types_with_trailing_periods() {
107107

108108
switch_theme( $theme['Template'], $theme['Stylesheet'] );
109109

110-
$this->assertEqualSetsWithIndex(
110+
$this->assertSameSetsWithIndex(
111111
array(
112112
'No Trailing Period' => '38766/no-trailing-period-post-types.php',
113113
'Trailing Period.' => '38766/trailing-period-post-types.php',
@@ -118,7 +118,7 @@ function test_page_templates_for_post_types_with_trailing_periods() {
118118
),
119119
get_page_templates( null, 'period' )
120120
);
121-
$this->assertEqualSetsWithIndex(
121+
$this->assertSameSetsWithIndex(
122122
array(
123123
'No Trailing Period' => '38766/no-trailing-period-post-types.php',
124124
'Trailing Period.' => '38766/trailing-period-post-types.php',
@@ -139,7 +139,7 @@ function test_page_templates_child_theme() {
139139

140140
switch_theme( $theme['Template'], $theme['Stylesheet'] );
141141

142-
$this->assertEqualSetsWithIndex(
142+
$this->assertSameSetsWithIndex(
143143
array(
144144
'Top Level' => 'template-top-level-post-types.php',
145145
'Sub Dir' => 'subdir/template-sub-dir-post-types.php',
@@ -149,15 +149,15 @@ function test_page_templates_child_theme() {
149149
get_page_templates( null, 'foo' )
150150
);
151151

152-
$this->assertEqualSetsWithIndex(
152+
$this->assertSameSetsWithIndex(
153153
array(
154154
'Top Level' => 'template-top-level-post-types.php',
155155
'Sub Dir' => 'subdir/template-sub-dir-post-types.php',
156156
),
157157
get_page_templates( null, 'post' )
158158
);
159159

160-
$this->assertEqualSetsWithIndex(
160+
$this->assertSameSetsWithIndex(
161161
array(
162162
'Top Level' => 'template-top-level.php',
163163
'Sub Dir' => 'subdir/template-sub-dir.php',
@@ -180,7 +180,7 @@ public function test_get_post_templates_child_theme() {
180180

181181
$post_templates = $theme->get_post_templates();
182182

183-
$this->assertEqualSetsWithIndex(
183+
$this->assertSameSetsWithIndex(
184184
array(
185185
'template-top-level-post-types.php' => 'Top Level',
186186
'subdir/template-sub-dir-post-types.php' => 'Sub Dir',
@@ -190,15 +190,15 @@ public function test_get_post_templates_child_theme() {
190190
$post_templates['foo']
191191
);
192192

193-
$this->assertEqualSetsWithIndex(
193+
$this->assertSameSetsWithIndex(
194194
array(
195195
'template-top-level-post-types.php' => 'Top Level',
196196
'subdir/template-sub-dir-post-types.php' => 'Sub Dir',
197197
),
198198
$post_templates['post']
199199
);
200200

201-
$this->assertEqualSetsWithIndex(
201+
$this->assertSameSetsWithIndex(
202202
array(
203203
'template-top-level.php' => 'Top Level',
204204
'subdir/template-sub-dir.php' => 'Sub Dir',

tests/phpunit/tests/ajax/DeletePlugin.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public function test_missing_plugin() {
4141
),
4242
);
4343

44-
$this->assertEqualSets( $expected, $response );
44+
$this->assertSameSets( $expected, $response );
4545
}
4646

4747
public function test_missing_slug() {
@@ -67,7 +67,7 @@ public function test_missing_slug() {
6767
),
6868
);
6969

70-
$this->assertEqualSets( $expected, $response );
70+
$this->assertSameSets( $expected, $response );
7171
}
7272

7373
public function test_missing_capability() {
@@ -94,7 +94,7 @@ public function test_missing_capability() {
9494
),
9595
);
9696

97-
$this->assertEqualSets( $expected, $response );
97+
$this->assertSameSets( $expected, $response );
9898
}
9999

100100
public function test_invalid_file() {
@@ -123,7 +123,7 @@ public function test_invalid_file() {
123123
),
124124
);
125125

126-
$this->assertEqualSets( $expected, $response );
126+
$this->assertSameSets( $expected, $response );
127127
}
128128

129129
public function test_delete_plugin() {
@@ -153,6 +153,6 @@ public function test_delete_plugin() {
153153
),
154154
);
155155

156-
$this->assertEqualSets( $expected, $response );
156+
$this->assertSameSets( $expected, $response );
157157
}
158158
}

tests/phpunit/tests/ajax/ManageThemes.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public function test_missing_slug() {
7070
),
7171
);
7272

73-
$this->assertEqualSets( $expected, $response );
73+
$this->assertSameSets( $expected, $response );
7474
}
7575

7676
public function test_missing_capability() {
@@ -92,13 +92,13 @@ public function test_missing_capability() {
9292
'data' => array(
9393
'update' => 'theme',
9494
'slug' => 'foo',
95-
'errorMessage' => 'Sorry, you are not allowed to update themes for this site.',
9695
'oldVersion' => '',
9796
'newVersion' => '',
97+
'errorMessage' => 'Sorry, you are not allowed to update themes for this site.',
9898
),
9999
);
100100

101-
$this->assertEqualSets( $expected, $response );
101+
$this->assertSameSets( $expected, $response );
102102
}
103103

104104
public function test_update_theme() {
@@ -128,14 +128,14 @@ public function test_update_theme() {
128128
'data' => array(
129129
'update' => 'theme',
130130
'slug' => 'twentyten',
131-
'errorMessage' => 'The theme is at the latest version.',
132131
'oldVersion' => $theme->get( 'Version' ),
133132
'newVersion' => '',
134133
'debug' => array( 'The theme is at the latest version.' ),
134+
'errorMessage' => 'The theme is at the latest version.',
135135
),
136136
);
137137

138-
$this->assertEqualSets( $expected, $response );
138+
$this->assertSameSets( $expected, $response );
139139
}
140140

141141
function test_uppercase_theme_slug() {
@@ -161,11 +161,11 @@ function test_uppercase_theme_slug() {
161161
'slug' => 'camelCase',
162162
'oldVersion' => '1.0',
163163
'newVersion' => '',
164-
'errorMessage' => 'The theme is at the latest version.',
165164
'debug' => array( 'The theme is at the latest version.' ),
165+
'errorMessage' => 'The theme is at the latest version.',
166166
),
167167
);
168168

169-
$this->assertEqualSets( $expected, $response );
169+
$this->assertSameSets( $expected, $response );
170170
}
171171
}

tests/phpunit/tests/ajax/QuickEdit.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,6 @@ public function test_dont_process_terms_if_taxonomy_does_not_allow_show_on_quick
8181

8282
// 'wptests_tax_2' terms should have been added successfully.
8383
$post_terms_2 = wp_get_object_terms( $post->ID, 'wptests_tax_2' );
84-
$this->assertEqualSets( array( $t2 ), wp_list_pluck( $post_terms_2, 'term_id' ) );
84+
$this->assertSameSets( array( $t2 ), wp_list_pluck( $post_terms_2, 'term_id' ) );
8585
}
8686
}

tests/phpunit/tests/ajax/UpdatePlugin.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public function test_missing_plugin() {
4141
),
4242
);
4343

44-
$this->assertEqualSets( $expected, $response );
44+
$this->assertSameSets( $expected, $response );
4545
}
4646

4747
public function test_missing_slug() {
@@ -67,7 +67,7 @@ public function test_missing_slug() {
6767
),
6868
);
6969

70-
$this->assertEqualSets( $expected, $response );
70+
$this->assertSameSets( $expected, $response );
7171
}
7272

7373
public function test_missing_capability() {
@@ -90,13 +90,13 @@ public function test_missing_capability() {
9090
'data' => array(
9191
'update' => 'plugin',
9292
'slug' => 'foo',
93-
'errorMessage' => 'Sorry, you are not allowed to update plugins for this site.',
9493
'oldVersion' => '',
9594
'newVersion' => '',
95+
'errorMessage' => 'Sorry, you are not allowed to update plugins for this site.',
9696
),
9797
);
9898

99-
$this->assertEqualSets( $expected, $response );
99+
$this->assertSameSets( $expected, $response );
100100
}
101101

102102
public function test_invalid_file() {
@@ -121,13 +121,13 @@ public function test_invalid_file() {
121121
'data' => array(
122122
'update' => 'plugin',
123123
'slug' => 'foo',
124-
'errorMessage' => 'Sorry, you are not allowed to update plugins for this site.',
125124
'oldVersion' => '',
126125
'newVersion' => '',
126+
'errorMessage' => 'Sorry, you are not allowed to update plugins for this site.',
127127
),
128128
);
129129

130-
$this->assertEqualSets( $expected, $response );
130+
$this->assertSameSets( $expected, $response );
131131
}
132132

133133
public function test_update_plugin() {
@@ -155,15 +155,15 @@ public function test_update_plugin() {
155155
'data' => array(
156156
'update' => 'plugin',
157157
'slug' => 'hello-dolly',
158-
'plugin' => 'hello.php',
159-
'pluginName' => 'Hello Dolly',
160-
'errorMessage' => 'The plugin is at the latest version.',
161158
'oldVersion' => 'Version 1.7.2',
162159
'newVersion' => '',
160+
'plugin' => 'hello.php',
161+
'pluginName' => 'Hello Dolly',
163162
'debug' => array( 'The plugin is at the latest version.' ),
163+
'errorMessage' => 'The plugin is at the latest version.',
164164
),
165165
);
166166

167-
$this->assertEqualSets( $expected, $response );
167+
$this->assertSameSets( $expected, $response );
168168
}
169169
}

tests/phpunit/tests/blocks/block-type-registry.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,6 @@ public function test_get_all_registered() {
186186
}
187187

188188
$registered = $this->registry->get_all_registered();
189-
$this->assertEqualSets( $names, array_keys( $registered ) );
189+
$this->assertSameSets( $names, array_keys( $registered ) );
190190
}
191191
}

tests/phpunit/tests/blocks/block-type.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -369,12 +369,12 @@ public function test_register_block() {
369369

370370
$this->assertSame( 'Test title', $block_type->title );
371371
$this->assertSame( 'Test category', $block_type->category );
372-
$this->assertEqualSets( array( 'core/third-party' ), $block_type->parent );
372+
$this->assertSameSets( array( 'core/third-party' ), $block_type->parent );
373373
$this->assertSame( 'icon.png', $block_type->icon );
374374
$this->assertSame( 'test description', $block_type->description );
375-
$this->assertEqualSets( array( 'test keyword' ), $block_type->keywords );
375+
$this->assertSameSets( array( 'test keyword' ), $block_type->keywords );
376376
$this->assertSame( 'test_domain', $block_type->textdomain );
377-
$this->assertEqualSets( array( 'alignment' => true ), $block_type->supports );
377+
$this->assertSameSets( array( 'alignment' => true ), $block_type->supports );
378378
}
379379

380380
/**

0 commit comments

Comments
 (0)