Skip to content

Commit a3770fe

Browse files
Tests: Use assertSame() in some newly introduced 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. Follow-up to [61032], [61045]. See #64324. git-svn-id: https://develop.svn.wordpress.org/trunk@61373 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 48b2b65 commit a3770fe

File tree

3 files changed

+35
-35
lines changed

3 files changed

+35
-35
lines changed

tests/phpunit/tests/rest-api/wpRestAbilitiesV1CategoriesController.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -171,9 +171,9 @@ public function test_get_item(): void {
171171
$this->assertEquals( 200, $response->get_status() );
172172

173173
$data = $response->get_data();
174-
$this->assertEquals( 'test-data-retrieval', $data['slug'] );
175-
$this->assertEquals( 'Data Retrieval', $data['label'] );
176-
$this->assertEquals( 'Abilities that retrieve and return data from the WordPress site.', $data['description'] );
174+
$this->assertSame( 'test-data-retrieval', $data['slug'] );
175+
$this->assertSame( 'Data Retrieval', $data['label'] );
176+
$this->assertSame( 'Abilities that retrieve and return data from the WordPress site.', $data['description'] );
177177
$this->assertArrayHasKey( 'meta', $data );
178178
}
179179

@@ -189,10 +189,10 @@ public function test_get_item_with_meta(): void {
189189
$this->assertEquals( 200, $response->get_status() );
190190

191191
$data = $response->get_data();
192-
$this->assertEquals( 'test-communication', $data['slug'] );
192+
$this->assertSame( 'test-communication', $data['slug'] );
193193
$this->assertArrayHasKey( 'meta', $data );
194194
$this->assertIsArray( $data['meta'] );
195-
$this->assertEquals( 'high', $data['meta']['priority'] );
195+
$this->assertSame( 'high', $data['meta']['priority'] );
196196
}
197197

198198
/**
@@ -212,8 +212,8 @@ public function test_get_item_with_selected_fields(): void {
212212

213213
$data = $response->get_data();
214214
$this->assertCount( 2, $data, 'Response should only contain the requested fields.' );
215-
$this->assertEquals( 'test-data-retrieval', $data['slug'] );
216-
$this->assertEquals( 'Data Retrieval', $data['label'] );
215+
$this->assertSame( 'test-data-retrieval', $data['slug'] );
216+
$this->assertSame( 'Data Retrieval', $data['label'] );
217217
}
218218

219219
/**
@@ -230,7 +230,7 @@ public function test_get_item_not_found(): void {
230230
$this->assertEquals( 404, $response->get_status() );
231231

232232
$data = $response->get_data();
233-
$this->assertEquals( 'rest_ability_category_not_found', $data['code'] );
233+
$this->assertSame( 'rest_ability_category_not_found', $data['code'] );
234234
}
235235

236236
/**
@@ -424,8 +424,8 @@ public function test_get_schema(): void {
424424
$this->assertArrayHasKey( 'schema', $data );
425425
$schema = $data['schema'];
426426

427-
$this->assertEquals( 'ability-category', $schema['title'] );
428-
$this->assertEquals( 'object', $schema['type'] );
427+
$this->assertSame( 'ability-category', $schema['title'] );
428+
$this->assertSame( 'object', $schema['type'] );
429429
$this->assertArrayHasKey( 'properties', $schema );
430430

431431
$properties = $schema['properties'];
@@ -438,7 +438,7 @@ public function test_get_schema(): void {
438438
$this->assertArrayHasKey( 'meta', $properties );
439439

440440
$slug_property = $properties['slug'];
441-
$this->assertEquals( 'string', $slug_property['type'] );
441+
$this->assertSame( 'string', $slug_property['type'] );
442442
$this->assertTrue( $slug_property['readonly'] );
443443
}
444444

tests/phpunit/tests/rest-api/wpRestAbilitiesV1ListController.php

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -307,10 +307,10 @@ public function test_get_item(): void {
307307

308308
$data = $response->get_data();
309309
$this->assertCount( 7, $data, 'Response should contain all fields.' );
310-
$this->assertEquals( 'test/calculator', $data['name'] );
311-
$this->assertEquals( 'Calculator', $data['label'] );
312-
$this->assertEquals( 'Performs basic calculations', $data['description'] );
313-
$this->assertEquals( 'math', $data['category'] );
310+
$this->assertSame( 'test/calculator', $data['name'] );
311+
$this->assertSame( 'Calculator', $data['label'] );
312+
$this->assertSame( 'Performs basic calculations', $data['description'] );
313+
$this->assertSame( 'math', $data['category'] );
314314
$this->assertArrayHasKey( 'input_schema', $data );
315315
$this->assertArrayHasKey( 'output_schema', $data );
316316
$this->assertArrayHasKey( 'meta', $data );
@@ -334,8 +334,8 @@ public function test_get_item_with_selected_fields(): void {
334334

335335
$data = $response->get_data();
336336
$this->assertCount( 2, $data, 'Response should only contain the requested fields.' );
337-
$this->assertEquals( 'test/calculator', $data['name'] );
338-
$this->assertEquals( 'Calculator', $data['label'] );
337+
$this->assertSame( 'test/calculator', $data['name'] );
338+
$this->assertSame( 'Calculator', $data['label'] );
339339
}
340340

341341
/**
@@ -355,9 +355,9 @@ public function test_get_item_with_embed_context(): void {
355355

356356
$data = $response->get_data();
357357
$this->assertCount( 3, $data, 'Response should only contain the fields for embed context.' );
358-
$this->assertEquals( 'test/calculator', $data['name'] );
359-
$this->assertEquals( 'Calculator', $data['label'] );
360-
$this->assertEquals( 'math', $data['category'] );
358+
$this->assertSame( 'test/calculator', $data['name'] );
359+
$this->assertSame( 'Calculator', $data['label'] );
360+
$this->assertSame( 'math', $data['category'] );
361361
}
362362

363363
/**
@@ -374,7 +374,7 @@ public function test_get_item_not_found(): void {
374374
$this->assertEquals( 404, $response->get_status() );
375375

376376
$data = $response->get_data();
377-
$this->assertEquals( 'rest_ability_not_found', $data['code'] );
377+
$this->assertSame( 'rest_ability_not_found', $data['code'] );
378378
}
379379

380380
/**
@@ -389,7 +389,7 @@ public function test_get_item_not_show_in_rest(): void {
389389
$this->assertEquals( 404, $response->get_status() );
390390

391391
$data = $response->get_data();
392-
$this->assertEquals( 'rest_ability_not_found', $data['code'] );
392+
$this->assertSame( 'rest_ability_not_found', $data['code'] );
393393
}
394394

395395
/**
@@ -581,8 +581,8 @@ public function test_get_schema(): void {
581581
$this->assertArrayHasKey( 'schema', $data );
582582
$schema = $data['schema'];
583583

584-
$this->assertEquals( 'ability', $schema['title'] );
585-
$this->assertEquals( 'object', $schema['type'] );
584+
$this->assertSame( 'ability', $schema['title'] );
585+
$this->assertSame( 'object', $schema['type'] );
586586
$this->assertArrayHasKey( 'properties', $schema );
587587

588588
$properties = $schema['properties'];
@@ -745,7 +745,7 @@ public function test_filter_by_category(): void {
745745

746746
// Should only have math category abilities
747747
foreach ( $data as $ability ) {
748-
$this->assertEquals( 'math', $ability['category'], 'All abilities should be in math category' );
748+
$this->assertSame( 'math', $ability['category'], 'All abilities should be in math category' );
749749
}
750750

751751
// Should at least contain the calculator

tests/phpunit/tests/rest-api/wpRestAbilitiesV1RunController.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,7 @@ public function test_execute_destructive_ability_delete(): void {
472472
$response = $this->server->dispatch( $request );
473473

474474
$this->assertEquals( 200, $response->get_status() );
475-
$this->assertEquals( 'User successfully deleted!', $response->get_data() );
475+
$this->assertSame( 'User successfully deleted!', $response->get_data() );
476476
}
477477

478478
/**
@@ -630,7 +630,7 @@ public function test_contextual_permission_check(): void {
630630

631631
$response = $this->server->dispatch( $request );
632632
$this->assertEquals( 200, $response->get_status() );
633-
$this->assertEquals( 'Success: test data', $response->get_data() );
633+
$this->assertSame( 'Success: test data', $response->get_data() );
634634
}
635635

636636
/**
@@ -646,8 +646,8 @@ public function test_do_not_show_in_rest(): void {
646646

647647
$this->assertEquals( 404, $response->get_status() );
648648
$data = $response->get_data();
649-
$this->assertEquals( 'rest_ability_not_found', $data['code'] );
650-
$this->assertEquals( 'Ability not found.', $data['message'] );
649+
$this->assertSame( 'rest_ability_not_found', $data['code'] );
650+
$this->assertSame( 'Ability not found.', $data['message'] );
651651
}
652652

653653
/**
@@ -679,8 +679,8 @@ public function test_wp_error_return_handling(): void {
679679

680680
$this->assertEquals( 500, $response->get_status() );
681681
$data = $response->get_data();
682-
$this->assertEquals( 'test_error', $data['code'] );
683-
$this->assertEquals( 'This is a test error', $data['message'] );
682+
$this->assertSame( 'test_error', $data['code'] );
683+
$this->assertSame( 'This is a test error', $data['message'] );
684684
}
685685

686686
/**
@@ -698,7 +698,7 @@ public function test_execute_non_existent_ability(): void {
698698

699699
$this->assertEquals( 404, $response->get_status() );
700700
$data = $response->get_data();
701-
$this->assertEquals( 'rest_ability_not_found', $data['code'] );
701+
$this->assertSame( 'rest_ability_not_found', $data['code'] );
702702
}
703703

704704
/**
@@ -714,8 +714,8 @@ public function test_run_endpoint_schema(): void {
714714
$this->assertArrayHasKey( 'schema', $data );
715715
$schema = $data['schema'];
716716

717-
$this->assertEquals( 'ability-execution', $schema['title'] );
718-
$this->assertEquals( 'object', $schema['type'] );
717+
$this->assertSame( 'ability-execution', $schema['title'] );
718+
$this->assertSame( 'object', $schema['type'] );
719719
$this->assertArrayHasKey( 'properties', $schema );
720720
$this->assertArrayHasKey( 'result', $schema['properties'] );
721721
}
@@ -761,7 +761,7 @@ public function test_get_request_with_nested_input_array(): void {
761761
$this->assertEquals( 200, $response->get_status() );
762762

763763
$data = $response->get_data();
764-
$this->assertEquals( 'nested', $data['level1']['level2']['value'] );
764+
$this->assertSame( 'nested', $data['level1']['level2']['value'] );
765765
$this->assertEquals( array( 1, 2, 3 ), $data['array'] );
766766
}
767767

0 commit comments

Comments
 (0)