Skip to content

Commit 164b22c

Browse files
Tests: First pass at using assertSame() instead of assertEquals() in most of the unit tests.
This ensures that not only the return values match the expected results, but also that their type is the same. Going forward, stricter type checking by using `assertSame()` should generally be preferred to `assertEquals()` where appropriate, to make the tests more reliable. Props johnbillion, jrf, SergeyBiryukov. See #38266. git-svn-id: https://develop.svn.wordpress.org/trunk@48937 602fd350-edb4-49c9-b593-d223f7449a82
1 parent ba7c6a2 commit 164b22c

426 files changed

Lines changed: 7965 additions & 7955 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: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -659,16 +659,29 @@ public function assertDiscardWhitespace( $expected, $actual ) {
659659
$this->assertEquals( preg_replace( '/\s*/', '', $expected ), preg_replace( '/\s*/', '', $actual ) );
660660
}
661661

662+
/**
663+
* Asserts that two values have the same type and value, with EOL differences discarded.
664+
*
665+
* @since 5.6.0
666+
*
667+
* @param string $expected The expected value.
668+
* @param string $actual The actual value.
669+
*/
670+
public function assertSameIgnoreEOL( $expected, $actual ) {
671+
$this->assertSame( str_replace( "\r\n", "\n", $expected ), str_replace( "\r\n", "\n", $actual ) );
672+
}
673+
662674
/**
663675
* Asserts that two values are equal, with EOL differences discarded.
664676
*
665677
* @since 5.4.0
678+
* @since 5.6.0 Turned into an alias for `::assertSameIgnoreEOL()`.
666679
*
667680
* @param string $expected The expected value.
668681
* @param string $actual The actual value.
669682
*/
670683
public function assertEqualsIgnoreEOL( $expected, $actual ) {
671-
$this->assertEquals( str_replace( "\r\n", "\n", $expected ), str_replace( "\r\n", "\n", $actual ) );
684+
$this->assertSameIgnoreEOL( $expected, $actual );
672685
}
673686

674687
/**

tests/phpunit/tests/actions.php

Lines changed: 48 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ function test_simple_action() {
1515
do_action( $tag );
1616

1717
// Only one event occurred for the hook, with empty args.
18-
$this->assertEquals( 1, $a->get_call_count() );
18+
$this->assertSame( 1, $a->get_call_count() );
1919
// Only our hook was called.
20-
$this->assertEquals( array( $tag ), $a->get_tags() );
20+
$this->assertSame( array( $tag ), $a->get_tags() );
2121

2222
$argsvar = $a->get_args();
2323
$args = array_pop( $argsvar );
24-
$this->assertEquals( array( '' ), $args );
24+
$this->assertSame( array( '' ), $args );
2525
}
2626

2727
function test_remove_action() {
@@ -32,14 +32,14 @@ function test_remove_action() {
3232
do_action( $tag );
3333

3434
// Make sure our hook was called correctly.
35-
$this->assertEquals( 1, $a->get_call_count() );
36-
$this->assertEquals( array( $tag ), $a->get_tags() );
35+
$this->assertSame( 1, $a->get_call_count() );
36+
$this->assertSame( array( $tag ), $a->get_tags() );
3737

3838
// Now remove the action, do it again, and make sure it's not called this time.
3939
remove_action( $tag, array( &$a, 'action' ) );
4040
do_action( $tag );
41-
$this->assertEquals( 1, $a->get_call_count() );
42-
$this->assertEquals( array( $tag ), $a->get_tags() );
41+
$this->assertSame( 1, $a->get_call_count() );
42+
$this->assertSame( array( $tag ), $a->get_tags() );
4343

4444
}
4545

@@ -50,7 +50,7 @@ function test_has_action() {
5050
$this->assertFalse( has_action( $tag, $func ) );
5151
$this->assertFalse( has_action( $tag ) );
5252
add_action( $tag, $func );
53-
$this->assertEquals( 10, has_action( $tag, $func ) );
53+
$this->assertSame( 10, has_action( $tag, $func ) );
5454
$this->assertTrue( has_action( $tag ) );
5555
remove_action( $tag, $func );
5656
$this->assertFalse( has_action( $tag, $func ) );
@@ -70,8 +70,8 @@ function test_multiple_actions() {
7070
do_action( $tag );
7171

7272
// Both actions called once each.
73-
$this->assertEquals( 1, $a1->get_call_count() );
74-
$this->assertEquals( 1, $a2->get_call_count() );
73+
$this->assertSame( 1, $a1->get_call_count() );
74+
$this->assertSame( 1, $a2->get_call_count() );
7575
}
7676

7777
function test_action_args_1() {
@@ -84,9 +84,9 @@ function test_action_args_1() {
8484
do_action( $tag, $val );
8585

8686
$call_count = $a->get_call_count();
87-
$this->assertEquals( 1, $call_count );
87+
$this->assertSame( 1, $call_count );
8888
$argsvar = $a->get_args();
89-
$this->assertEquals( array( $val ), array_pop( $argsvar ) );
89+
$this->assertSame( array( $val ), array_pop( $argsvar ) );
9090
}
9191

9292
function test_action_args_2() {
@@ -104,14 +104,14 @@ function test_action_args_2() {
104104

105105
$call_count = $a1->get_call_count();
106106
// $a1 should be called with both args.
107-
$this->assertEquals( 1, $call_count );
107+
$this->assertSame( 1, $call_count );
108108
$argsvar1 = $a1->get_args();
109-
$this->assertEquals( array( $val1, $val2 ), array_pop( $argsvar1 ) );
109+
$this->assertSame( array( $val1, $val2 ), array_pop( $argsvar1 ) );
110110

111111
// $a2 should be called with one only.
112-
$this->assertEquals( 1, $a2->get_call_count() );
112+
$this->assertSame( 1, $a2->get_call_count() );
113113
$argsvar2 = $a2->get_args();
114-
$this->assertEquals( array( $val1 ), array_pop( $argsvar2 ) );
114+
$this->assertSame( array( $val1 ), array_pop( $argsvar2 ) );
115115
}
116116

117117
/**
@@ -138,19 +138,19 @@ function test_action_args_3() {
138138

139139
$call_count = $a1->get_call_count();
140140
// $a1 should be called with both args.
141-
$this->assertEquals( 1, $call_count );
141+
$this->assertSame( 1, $call_count );
142142
$argsvar1 = $a1->get_args();
143-
$this->assertEquals( array( $val1, $val2 ), array_pop( $argsvar1 ) );
143+
$this->assertSame( array( $val1, $val2 ), array_pop( $argsvar1 ) );
144144

145145
// $a2 should be called with one only.
146-
$this->assertEquals( 1, $a2->get_call_count() );
146+
$this->assertSame( 1, $a2->get_call_count() );
147147
$argsvar2 = $a2->get_args();
148-
$this->assertEquals( array( $val1 ), array_pop( $argsvar2 ) );
148+
$this->assertSame( array( $val1 ), array_pop( $argsvar2 ) );
149149

150150
// $a3 should be called with both args.
151-
$this->assertEquals( 1, $a3->get_call_count() );
151+
$this->assertSame( 1, $a3->get_call_count() );
152152
$argsvar3 = $a3->get_args();
153-
$this->assertEquals( array( $val1, $val2 ), array_pop( $argsvar3 ) );
153+
$this->assertSame( array( $val1, $val2 ), array_pop( $argsvar3 ) );
154154
}
155155

156156
/**
@@ -181,7 +181,7 @@ function test_action_priority() {
181181
do_action( $tag );
182182

183183
// Two events, one per action.
184-
$this->assertEquals( 2, $a->get_call_count() );
184+
$this->assertSame( 2, $a->get_call_count() );
185185

186186
$expected = array(
187187
// 'action2' is called first because it has priority 9.
@@ -198,7 +198,7 @@ function test_action_priority() {
198198
),
199199
);
200200

201-
$this->assertEquals( $expected, $a->get_events() );
201+
$this->assertSame( $expected, $a->get_events() );
202202
}
203203

204204
function test_did_action() {
@@ -207,8 +207,8 @@ function test_did_action() {
207207

208208
// Do action $tag1 but not $tag2.
209209
do_action( $tag1 );
210-
$this->assertEquals( 1, did_action( $tag1 ) );
211-
$this->assertEquals( 0, did_action( $tag2 ) );
210+
$this->assertSame( 1, did_action( $tag1 ) );
211+
$this->assertSame( 0, did_action( $tag2 ) );
212212

213213
// Do action $tag2 a random number of times.
214214
$count = rand( 0, 10 );
@@ -217,8 +217,8 @@ function test_did_action() {
217217
}
218218

219219
// $tag1's count hasn't changed, $tag2 should be correct.
220-
$this->assertEquals( 1, did_action( $tag1 ) );
221-
$this->assertEquals( $count, did_action( $tag2 ) );
220+
$this->assertSame( 1, did_action( $tag1 ) );
221+
$this->assertSame( $count, did_action( $tag2 ) );
222222

223223
}
224224

@@ -229,17 +229,17 @@ function test_all_action() {
229229

230230
// Add an 'all' action.
231231
add_action( 'all', array( &$a, 'action' ) );
232-
$this->assertEquals( 10, has_filter( 'all', array( &$a, 'action' ) ) );
232+
$this->assertSame( 10, has_filter( 'all', array( &$a, 'action' ) ) );
233233
// Do some actions.
234234
do_action( $tag1 );
235235
do_action( $tag2 );
236236
do_action( $tag1 );
237237
do_action( $tag1 );
238238

239239
// Our action should have been called once for each tag.
240-
$this->assertEquals( 4, $a->get_call_count() );
240+
$this->assertSame( 4, $a->get_call_count() );
241241
// Only our hook was called.
242-
$this->assertEquals( array( $tag1, $tag2, $tag1, $tag1 ), $a->get_tags() );
242+
$this->assertSame( array( $tag1, $tag2, $tag1, $tag1 ), $a->get_tags() );
243243

244244
remove_action( 'all', array( &$a, 'action' ) );
245245
$this->assertFalse( has_filter( 'all', array( &$a, 'action' ) ) );
@@ -251,19 +251,19 @@ function test_remove_all_action() {
251251
$tag = __FUNCTION__;
252252

253253
add_action( 'all', array( &$a, 'action' ) );
254-
$this->assertEquals( 10, has_filter( 'all', array( &$a, 'action' ) ) );
254+
$this->assertSame( 10, has_filter( 'all', array( &$a, 'action' ) ) );
255255
do_action( $tag );
256256

257257
// Make sure our hook was called correctly.
258-
$this->assertEquals( 1, $a->get_call_count() );
259-
$this->assertEquals( array( $tag ), $a->get_tags() );
258+
$this->assertSame( 1, $a->get_call_count() );
259+
$this->assertSame( array( $tag ), $a->get_tags() );
260260

261261
// Now remove the action, do it again, and make sure it's not called this time.
262262
remove_action( 'all', array( &$a, 'action' ) );
263263
$this->assertFalse( has_filter( 'all', array( &$a, 'action' ) ) );
264264
do_action( $tag );
265-
$this->assertEquals( 1, $a->get_call_count() );
266-
$this->assertEquals( array( $tag ), $a->get_tags() );
265+
$this->assertSame( 1, $a->get_call_count() );
266+
$this->assertSame( array( $tag ), $a->get_tags() );
267267
}
268268

269269
function test_action_ref_array() {
@@ -312,7 +312,7 @@ function test_action_keyed_array() {
312312
function test_action_self_removal() {
313313
add_action( 'test_action_self_removal', array( $this, 'action_self_removal' ) );
314314
do_action( 'test_action_self_removal' );
315-
$this->assertEquals( 1, did_action( 'test_action_self_removal' ) );
315+
$this->assertSame( 1, did_action( 'test_action_self_removal' ) );
316316
}
317317

318318
function action_self_removal() {
@@ -332,8 +332,8 @@ function test_action_recursion() {
332332
add_action( $tag, array( $this, 'action_that_causes_recursion' ), 12, 1 );
333333
do_action( $tag, $tag );
334334

335-
$this->assertEquals( 2, $a->get_call_count(), 'recursive actions should call all callbacks with earlier priority' );
336-
$this->assertEquals( 2, $b->get_call_count(), 'recursive actions should call callbacks with later priority' );
335+
$this->assertSame( 2, $a->get_call_count(), 'recursive actions should call all callbacks with earlier priority' );
336+
$this->assertSame( 2, $b->get_call_count(), 'recursive actions should call callbacks with later priority' );
337337
}
338338

339339
function action_that_causes_recursion( $tag ) {
@@ -364,11 +364,11 @@ function test_action_callback_manipulation_while_running() {
364364
do_action( $tag, $tag, array( $a, $b, $c, $d, $e ) );
365365
do_action( $tag, $tag, array( $a, $b, $c, $d, $e ) );
366366

367-
$this->assertEquals( 2, $a->get_call_count(), 'callbacks should run unless otherwise instructed' );
368-
$this->assertEquals( 1, $b->get_call_count(), 'callback removed by same priority callback should still get called' );
369-
$this->assertEquals( 1, $c->get_call_count(), 'callback added by same priority callback should not get called' );
370-
$this->assertEquals( 2, $d->get_call_count(), 'callback added by earlier priority callback should get called' );
371-
$this->assertEquals( 1, $e->get_call_count(), 'callback added by later priority callback should not get called' );
367+
$this->assertSame( 2, $a->get_call_count(), 'callbacks should run unless otherwise instructed' );
368+
$this->assertSame( 1, $b->get_call_count(), 'callback removed by same priority callback should still get called' );
369+
$this->assertSame( 1, $c->get_call_count(), 'callback added by same priority callback should not get called' );
370+
$this->assertSame( 2, $d->get_call_count(), 'callback added by earlier priority callback should get called' );
371+
$this->assertSame( 1, $e->get_call_count(), 'callback added by later priority callback should not get called' );
372372
}
373373

374374
function action_that_manipulates_a_running_hook( $tag, $mocks ) {
@@ -435,7 +435,7 @@ function test_array_access_of_wp_filter_global() {
435435
'accepted_args' => 1,
436436
),
437437
);
438-
$this->assertEquals( 11, has_action( $tag, '__return_null' ) );
438+
$this->assertSame( 11, has_action( $tag, '__return_null' ) );
439439
}
440440

441441
/**
@@ -448,7 +448,7 @@ function test_current_action() {
448448
$wp_current_filter[] = 'first';
449449
$wp_current_filter[] = 'second'; // Let's say a second action was invoked.
450450

451-
$this->assertEquals( 'second', current_action() );
451+
$this->assertSame( 'second', current_action() );
452452
}
453453

454454
/**
@@ -498,7 +498,7 @@ function test_doing_filter_real() {
498498

499499
add_filter( 'testing', array( $this, 'apply_testing_filter' ) );
500500
$this->assertTrue( has_action( 'testing' ) );
501-
$this->assertEquals( 10, has_action( 'testing', array( $this, 'apply_testing_filter' ) ) );
501+
$this->assertSame( 10, has_action( 'testing', array( $this, 'apply_testing_filter' ) ) );
502502

503503
apply_filters( 'testing', '' );
504504

@@ -519,7 +519,7 @@ function apply_testing_filter() {
519519

520520
add_filter( 'testing_nested', array( $this, 'apply_testing_nested_filter' ) );
521521
$this->assertTrue( has_action( 'testing_nested' ) );
522-
$this->assertEquals( 10, has_action( 'testing_nested', array( $this, 'apply_testing_nested_filter' ) ) );
522+
$this->assertSame( 10, has_action( 'testing_nested', array( $this, 'apply_testing_nested_filter' ) ) );
523523

524524
apply_filters( 'testing_nested', '' );
525525

tests/phpunit/tests/actions/callbacks.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ function test_callback_representations() {
1515

1616
add_action( $tag, array( 'Class', 'method' ) );
1717

18-
$this->assertEquals( 10, has_action( $tag, array( 'Class', 'method' ) ) );
18+
$this->assertSame( 10, has_action( $tag, array( 'Class', 'method' ) ) );
1919

20-
$this->assertEquals( 10, has_action( $tag, 'Class::method' ) );
20+
$this->assertSame( 10, has_action( $tag, 'Class::method' ) );
2121
}
2222
}

tests/phpunit/tests/admin/includesCommunityEvents.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,8 @@ public function test_get_events_valid_response() {
164164

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

170170
remove_filter( 'pre_http_request', array( $this, '_http_request_valid_response' ) );
171171
}
@@ -185,8 +185,8 @@ public function test_get_cached_events_valid_response() {
185185

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

191191
remove_filter( 'pre_http_request', array( $this, '_http_request_valid_response' ) );
192192
}
@@ -273,9 +273,9 @@ public function test_get_events_pin_wordcamp() {
273273
* so that it remains in the list. The other events should remain unchanged.
274274
*/
275275
$this->assertCount( 3, $response_body['events'] );
276-
$this->assertEquals( $response_body['events'][0]['title'], 'Flexbox + CSS Grid: Magic for Responsive Layouts' );
277-
$this->assertEquals( $response_body['events'][1]['title'], 'Part 3- Site Maintenance - Tools to Make It Easy' );
278-
$this->assertEquals( $response_body['events'][2]['title'], 'WordCamp San Diego' );
276+
$this->assertSame( $response_body['events'][0]['title'], 'Flexbox + CSS Grid: Magic for Responsive Layouts' );
277+
$this->assertSame( $response_body['events'][1]['title'], 'Part 3- Site Maintenance - Tools to Make It Easy' );
278+
$this->assertSame( $response_body['events'][2]['title'], 'WordCamp San Diego' );
279279

280280
remove_filter( 'pre_http_request', array( $this, '_http_request_valid_response_unpinned_wordcamp' ) );
281281
}
@@ -375,9 +375,9 @@ public function test_get_events_dont_pin_multiple_wordcamps() {
375375
* WordCamp LA should not be stuck to the list, because San Diego already appears naturally.
376376
*/
377377
$this->assertCount( 3, $response_body['events'] );
378-
$this->assertEquals( $response_body['events'][0]['title'], 'WordCamp San Diego' );
379-
$this->assertEquals( $response_body['events'][1]['title'], 'Part 3- Site Maintenance - Tools to Make It Easy' );
380-
$this->assertEquals( $response_body['events'][2]['title'], 'WordPress Q&A' );
378+
$this->assertSame( $response_body['events'][0]['title'], 'WordCamp San Diego' );
379+
$this->assertSame( $response_body['events'][1]['title'], 'Part 3- Site Maintenance - Tools to Make It Easy' );
380+
$this->assertSame( $response_body['events'][2]['title'], 'WordPress Q&A' );
381381

382382
remove_filter( 'pre_http_request', array( $this, '_http_request_valid_response_multiple_wordcamps' ) );
383383
}
@@ -488,7 +488,7 @@ public function test_get_unsafe_client_ip( $raw_ip, $expected_result ) {
488488
$_SERVER['HTTP_CLIENT_IP'] = $raw_ip;
489489
$actual_result = WP_Community_Events::get_unsafe_client_ip();
490490

491-
$this->assertEquals( $expected_result, $actual_result );
491+
$this->assertSame( $expected_result, $actual_result );
492492
}
493493

494494
/**

0 commit comments

Comments
 (0)