Skip to content

Commit 8b7872f

Browse files
committed
Tests: Use assertContainsOnlyInstancesOf() over per-item loops.
Replace `foreach` loops that call `assertInstanceOf()` on each element with a single `assertContainsOnlyInstancesOf()` assertion. Developed in: WordPress#12515 Props mukesh27, soean, wildworks. See #64894. git-svn-id: https://develop.svn.wordpress.org/trunk@62751 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 05aa7d1 commit 8b7872f

6 files changed

Lines changed: 17 additions & 51 deletions

File tree

tests/phpunit/tests/post/getPages.php

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -55,28 +55,22 @@ public function test_get_pages_cache() {
5555
$time1 = wp_cache_get( 'last_changed', 'posts' );
5656
$this->assertNotEmpty( $time1 );
5757
$num_queries = get_num_queries();
58-
foreach ( $pages as $page ) {
59-
$this->assertInstanceOf( 'WP_Post', $page );
60-
}
58+
$this->assertContainsOnlyInstancesOf( 'WP_Post', $pages );
6159

6260
// Again. num_queries and last_changed should remain the same.
6361
$pages = get_pages();
6462
$this->assertCount( 3, $pages );
6563
$this->assertSame( $time1, wp_cache_get( 'last_changed', 'posts' ) );
6664
$this->assertSame( $num_queries, get_num_queries() );
67-
foreach ( $pages as $page ) {
68-
$this->assertInstanceOf( 'WP_Post', $page );
69-
}
65+
$this->assertContainsOnlyInstancesOf( 'WP_Post', $pages );
7066

7167
// Again with different args. last_changed should not increment because of
7268
// different args to get_pages(). num_queries should bump by 1.
7369
$pages = get_pages( array( 'number' => 2 ) );
7470
$this->assertCount( 2, $pages );
7571
$this->assertSame( $time1, wp_cache_get( 'last_changed', 'posts' ) );
7672
$this->assertSame( $num_queries + 1, get_num_queries() );
77-
foreach ( $pages as $page ) {
78-
$this->assertInstanceOf( 'WP_Post', $page );
79-
}
73+
$this->assertContainsOnlyInstancesOf( 'WP_Post', $pages );
8074

8175
$num_queries = get_num_queries();
8276

@@ -85,18 +79,14 @@ public function test_get_pages_cache() {
8579
$this->assertCount( 2, $pages );
8680
$this->assertSame( $time1, wp_cache_get( 'last_changed', 'posts' ) );
8781
$this->assertSame( $num_queries, get_num_queries() );
88-
foreach ( $pages as $page ) {
89-
$this->assertInstanceOf( 'WP_Post', $page );
90-
}
82+
$this->assertContainsOnlyInstancesOf( 'WP_Post', $pages );
9183

9284
// Do the first query again. The interim queries should not affect it.
9385
$pages = get_pages();
9486
$this->assertCount( 3, $pages );
9587
$this->assertSame( $time1, wp_cache_get( 'last_changed', 'posts' ) );
9688
$this->assertSame( $num_queries, get_num_queries() );
97-
foreach ( $pages as $page ) {
98-
$this->assertInstanceOf( 'WP_Post', $page );
99-
}
89+
$this->assertContainsOnlyInstancesOf( 'WP_Post', $pages );
10090

10191
// Force last_changed to increment.
10292
clean_post_cache( $pages[0]->ID );
@@ -109,9 +99,7 @@ public function test_get_pages_cache() {
10999
$this->assertCount( 2, $pages );
110100
$this->assertSame( $time2, wp_cache_get( 'last_changed', 'posts' ) );
111101
$this->assertSame( $num_queries + 1, get_num_queries() );
112-
foreach ( $pages as $page ) {
113-
$this->assertInstanceOf( 'WP_Post', $page );
114-
}
102+
$this->assertContainsOnlyInstancesOf( 'WP_Post', $pages );
115103

116104
$last_changed = wp_cache_get( 'last_changed', 'posts' );
117105

@@ -129,9 +117,7 @@ public function test_get_pages_cache() {
129117
$this->assertCount( 2, $pages );
130118
$this->assertSame( $last_changed, wp_cache_get( 'last_changed', 'posts' ) );
131119
$this->assertSame( $num_queries + 1, get_num_queries() );
132-
foreach ( $pages as $page ) {
133-
$this->assertInstanceOf( 'WP_Post', $page );
134-
}
120+
$this->assertContainsOnlyInstancesOf( 'WP_Post', $pages );
135121
}
136122

137123
/**

tests/phpunit/tests/post/wpPostType.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@ public function test_instances() {
99

1010
$this->assertNotEmpty( $wp_post_types );
1111

12-
foreach ( $wp_post_types as $post_type ) {
13-
$this->assertInstanceOf( 'WP_Post_Type', $post_type );
14-
}
12+
$this->assertContainsOnlyInstancesOf( 'WP_Post_Type', $wp_post_types );
1513
}
1614

1715
public function test_add_supports_defaults() {

tests/phpunit/tests/term/getTerms.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2876,9 +2876,7 @@ public function test_should_return_wp_term_objects() {
28762876

28772877
$this->assertNotEmpty( $found );
28782878

2879-
foreach ( $found as $term ) {
2880-
$this->assertInstanceOf( 'WP_Term', $term );
2881-
}
2879+
$this->assertContainsOnlyInstancesOf( 'WP_Term', $found );
28822880
}
28832881

28842882
/**
@@ -2914,9 +2912,7 @@ public function test_should_return_wp_term_objects_when_pulling_from_the_cache()
29142912

29152913
$this->assertNotEmpty( $found );
29162914

2917-
foreach ( $found as $term ) {
2918-
$this->assertInstanceOf( 'WP_Term', $term );
2919-
}
2915+
$this->assertContainsOnlyInstancesOf( 'WP_Term', $found );
29202916
}
29212917

29222918
/**

tests/phpunit/tests/term/wpGetObjectTerms.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -781,9 +781,7 @@ public function test_should_return_wp_term_objects_for_fields_all() {
781781
);
782782

783783
$this->assertNotEmpty( $found );
784-
foreach ( $found as $f ) {
785-
$this->assertInstanceOf( 'WP_Term', $f );
786-
}
784+
$this->assertContainsOnlyInstancesOf( 'WP_Term', $found );
787785
}
788786

789787
/**
@@ -804,9 +802,7 @@ public function test_should_return_wp_term_objects_for_fields_all_with_object_id
804802
);
805803

806804
$this->assertNotEmpty( $found );
807-
foreach ( $found as $f ) {
808-
$this->assertInstanceOf( 'WP_Term', $f );
809-
}
805+
$this->assertContainsOnlyInstancesOf( 'WP_Term', $found );
810806
}
811807

812808
/**

tests/phpunit/tests/term/wpTaxonomy.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@ public function test_instances() {
99

1010
$this->assertNotEmpty( $wp_taxonomies );
1111

12-
foreach ( $wp_taxonomies as $taxonomy ) {
13-
$this->assertInstanceOf( 'WP_Taxonomy', $taxonomy );
14-
}
12+
$this->assertContainsOnlyInstancesOf( 'WP_Taxonomy', $wp_taxonomies );
1513
}
1614

1715
public function test_does_not_add_query_var_if_not_public() {

tests/phpunit/tests/user/query.php

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,7 @@ public function test_get_all() {
138138

139139
// +1 for the default user created during installation.
140140
$this->assertCount( 13, $users );
141-
foreach ( $users as $user ) {
142-
$this->assertInstanceOf( 'WP_User', $user );
143-
}
141+
$this->assertContainsOnlyInstancesOf( 'WP_User', $users );
144142

145143
$users = new WP_User_Query(
146144
array(
@@ -150,9 +148,7 @@ public function test_get_all() {
150148
);
151149
$users = $users->get_results();
152150
$this->assertCount( 13, $users );
153-
foreach ( $users as $user ) {
154-
$this->assertInstanceOf( 'WP_User', $user );
155-
}
151+
$this->assertContainsOnlyInstancesOf( 'WP_User', $users );
156152
}
157153

158154
/**
@@ -1416,9 +1412,7 @@ public function test_get_multiple_roles_should_only_match_users_who_have_each_ro
14161412

14171413
$this->assertCount( 2, $users );
14181414

1419-
foreach ( $users as $user ) {
1420-
$this->assertInstanceOf( 'WP_User', $user );
1421-
}
1415+
$this->assertContainsOnlyInstancesOf( 'WP_User', $users );
14221416
}
14231417

14241418
/**
@@ -1430,9 +1424,7 @@ public function test_get_multiple_roles_or() {
14301424

14311425
// +1 for the default user created during installation.
14321426
$this->assertCount( 8, $users );
1433-
foreach ( $users as $user ) {
1434-
$this->assertInstanceOf( 'WP_User', $user );
1435-
}
1427+
$this->assertContainsOnlyInstancesOf( 'WP_User', $users );
14361428
}
14371429

14381430
/**

0 commit comments

Comments
 (0)