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
This commit is contained in:
Sergey Biryukov
2020-09-04 07:01:00 +00:00
parent 5e2a75ef29
commit 8be943d06e
105 changed files with 757 additions and 729 deletions
+1 -1
View File
@@ -68,7 +68,7 @@ class Tests_Post_GetPageChildren extends WP_UnitTestCase {
public function test_page_id_0_should_return_all_pages_in_tree_and_exclude_pages_not_in_tree() {
$expected = array( 100, 101, 102, 103, 105, 106, 107, 108 );
$actual = get_page_children( 0, $this->pages );
$this->assertEqualSets( $expected, wp_list_pluck( $actual, 'ID' ) );
$this->assertSameSets( $expected, wp_list_pluck( $actual, 'ID' ) );
}
public function test_hierarchical_order_should_be_respected_in_results() {
+21 -21
View File
@@ -124,7 +124,7 @@ class Tests_Post_getPages extends WP_UnitTestCase {
);
$cached_ids = wp_list_pluck( $cached, 'ID' );
$this->assertEqualSets( array( $posts[0] ), $cached_ids );
$this->assertSameSets( array( $posts[0] ), $cached_ids );
add_post_meta( $posts[1], 'foo', 'bar' );
@@ -136,7 +136,7 @@ class Tests_Post_getPages extends WP_UnitTestCase {
);
$found_ids = wp_list_pluck( $found, 'ID' );
$this->assertEqualSets( $posts, $found_ids );
$this->assertSameSets( $posts, $found_ids );
}
/**
@@ -161,7 +161,7 @@ class Tests_Post_getPages extends WP_UnitTestCase {
);
$cached_ids = wp_list_pluck( $cached, 'ID' );
$this->assertEqualSets( $posts, $cached_ids );
$this->assertSameSets( $posts, $cached_ids );
update_post_meta( $posts[1], 'foo', 'baz' );
@@ -173,7 +173,7 @@ class Tests_Post_getPages extends WP_UnitTestCase {
);
$found_ids = wp_list_pluck( $found, 'ID' );
$this->assertEqualSets( array( $posts[0] ), $found_ids );
$this->assertSameSets( array( $posts[0] ), $found_ids );
}
/**
@@ -198,7 +198,7 @@ class Tests_Post_getPages extends WP_UnitTestCase {
);
$cached_ids = wp_list_pluck( $cached, 'ID' );
$this->assertEqualSets( $posts, $cached_ids );
$this->assertSameSets( $posts, $cached_ids );
delete_post_meta( $posts[1], 'foo' );
@@ -210,7 +210,7 @@ class Tests_Post_getPages extends WP_UnitTestCase {
);
$found_ids = wp_list_pluck( $found, 'ID' );
$this->assertEqualSets( array( $posts[0] ), $found_ids );
$this->assertSameSets( array( $posts[0] ), $found_ids );
}
/**
@@ -235,7 +235,7 @@ class Tests_Post_getPages extends WP_UnitTestCase {
);
$cached_ids = wp_list_pluck( $cached, 'ID' );
$this->assertEqualSets( $posts, $cached_ids );
$this->assertSameSets( $posts, $cached_ids );
delete_post_meta_by_key( 'foo' );
@@ -247,7 +247,7 @@ class Tests_Post_getPages extends WP_UnitTestCase {
);
$found_ids = wp_list_pluck( $found, 'ID' );
$this->assertEqualSets( array(), $found_ids );
$this->assertSameSets( array(), $found_ids );
}
/**
@@ -340,7 +340,7 @@ class Tests_Post_getPages extends WP_UnitTestCase {
'hierarchical' => false,
)
);
$this->assertEqualSets( array( $page_id1 ), wp_list_pluck( $pages, 'ID' ) );
$this->assertSameSets( array( $page_id1 ), wp_list_pluck( $pages, 'ID' ) );
$pages = get_pages(
array(
@@ -348,7 +348,7 @@ class Tests_Post_getPages extends WP_UnitTestCase {
'hierarchical' => false,
)
);
$this->assertEqualSets( array( $page_id2, $page_id4 ), wp_list_pluck( $pages, 'ID' ) );
$this->assertSameSets( array( $page_id2, $page_id4 ), wp_list_pluck( $pages, 'ID' ) );
$pages = get_pages(
array(
@@ -356,16 +356,16 @@ class Tests_Post_getPages extends WP_UnitTestCase {
'hierarchical' => false,
)
);
$this->assertEqualSets( array( $page_id2, $page_id3, $page_id4 ), wp_list_pluck( $pages, 'ID' ) );
$this->assertSameSets( array( $page_id2, $page_id3, $page_id4 ), wp_list_pluck( $pages, 'ID' ) );
$pages = get_pages( array( 'parent' => 0 ) );
$this->assertEqualSets( array( $page_id1 ), wp_list_pluck( $pages, 'ID' ) );
$this->assertSameSets( array( $page_id1 ), wp_list_pluck( $pages, 'ID' ) );
$pages = get_pages( array( 'parent' => $page_id1 ) );
$this->assertEqualSets( array( $page_id2, $page_id4 ), wp_list_pluck( $pages, 'ID' ) );
$this->assertSameSets( array( $page_id2, $page_id4 ), wp_list_pluck( $pages, 'ID' ) );
$pages = get_pages( array( 'parent' => array( $page_id1, $page_id2 ) ) );
$this->assertEqualSets( array( $page_id2, $page_id3, $page_id4 ), wp_list_pluck( $pages, 'ID' ) );
$this->assertSameSets( array( $page_id2, $page_id3, $page_id4 ), wp_list_pluck( $pages, 'ID' ) );
}
/**
@@ -392,7 +392,7 @@ class Tests_Post_getPages extends WP_UnitTestCase {
'post_parent' => $post_id,
)
);
$this->assertEqualSets( $child_ids, $post_ids );
$this->assertSameSets( $child_ids, $post_ids );
}
/**
@@ -442,7 +442,7 @@ class Tests_Post_getPages extends WP_UnitTestCase {
* If it doesn't, they will be in the creation order, 1,2,3,4.
*/
$this->assertEqualSets( array( $page_1, $page_2, $page_4, $page_3 ), wp_list_pluck( $pages, 'ID' ) );
$this->assertSameSets( array( $page_1, $page_2, $page_4, $page_3 ), wp_list_pluck( $pages, 'ID' ) );
}
/**
@@ -488,7 +488,7 @@ class Tests_Post_getPages extends WP_UnitTestCase {
$found_pages = wp_list_filter( $pages, array( 'post_parent' => 0 ) );
$this->assertEqualSets( array( $page_1, $page_2 ), wp_list_pluck( $found_pages, 'ID' ) );
$this->assertSameSets( array( $page_1, $page_2 ), wp_list_pluck( $found_pages, 'ID' ) );
}
/**
@@ -524,7 +524,7 @@ class Tests_Post_getPages extends WP_UnitTestCase {
* in order of creation: 1, 2, 3, 4, regardless of parent.
*/
$this->assertEqualSets( array( $page_1, $page_2, $page_3, $page_4 ), wp_list_pluck( $pages, 'ID' ) );
$this->assertSameSets( array( $page_1, $page_2, $page_3, $page_4 ), wp_list_pluck( $pages, 'ID' ) );
}
/**
@@ -567,7 +567,7 @@ class Tests_Post_getPages extends WP_UnitTestCase {
* hierarchically in order of creation: 3, 4, 5.
*/
$this->assertEqualSets( array( $page_3, $page_4, $page_5 ), wp_list_pluck( $pages, 'ID' ) );
$this->assertSameSets( array( $page_3, $page_4, $page_5 ), wp_list_pluck( $pages, 'ID' ) );
}
/**
@@ -614,11 +614,11 @@ class Tests_Post_getPages extends WP_UnitTestCase {
* If hierarchical is false, and child_of is not empty, pages will (apparently) be returned
* hierarchically anyway in order of creation: 3, 4, 5.
*/
$this->assertEqualSets( array( $page_3, $page_4, $page_5 ), wp_list_pluck( $pages, 'ID' ) );
$this->assertSameSets( array( $page_3, $page_4, $page_5 ), wp_list_pluck( $pages, 'ID' ) );
// How it should work.
$found_pages = wp_list_filter( $pages, array( 'post_parent' => $page_1 ) );
$this->assertEqualSets( array( $page_3, $page_5 ), wp_list_pluck( $found_pages, 'ID' ) );
$this->assertSameSets( array( $page_3, $page_5 ), wp_list_pluck( $found_pages, 'ID' ) );
}
+10 -10
View File
@@ -106,35 +106,35 @@ class Test_Nav_Menus extends WP_UnitTestCase {
);
$tag_items = wp_get_associated_nav_menu_items( $tag_id, 'taxonomy', 'post_tag' );
$this->assertEqualSets( array( $tag_insert ), $tag_items );
$this->assertSameSets( array( $tag_insert ), $tag_items );
$cat_items = wp_get_associated_nav_menu_items( $cat_id, 'taxonomy', 'category' );
$this->assertEqualSets( array( $cat_insert ), $cat_items );
$this->assertSameSets( array( $cat_insert ), $cat_items );
$post_items = wp_get_associated_nav_menu_items( $post_id );
$this->assertEqualSets( array( $post_insert ), $post_items );
$this->assertSameSets( array( $post_insert ), $post_items );
$post_2_items = wp_get_associated_nav_menu_items( $post_2_id );
$this->assertEqualSets( array( $post_2_insert ), $post_2_items );
$this->assertSameSets( array( $post_2_insert ), $post_2_items );
$page_items = wp_get_associated_nav_menu_items( $page_id );
$this->assertEqualSets( array( $page_insert ), $page_items );
$this->assertSameSets( array( $page_insert ), $page_items );
wp_delete_term( $tag_id, 'post_tag' );
$tag_items = wp_get_associated_nav_menu_items( $tag_id, 'taxonomy', 'post_tag' );
$this->assertEqualSets( array(), $tag_items );
$this->assertSameSets( array(), $tag_items );
wp_delete_term( $cat_id, 'category' );
$cat_items = wp_get_associated_nav_menu_items( $cat_id, 'taxonomy', 'category' );
$this->assertEqualSets( array(), $cat_items );
$this->assertSameSets( array(), $cat_items );
wp_delete_post( $post_id, true );
$post_items = wp_get_associated_nav_menu_items( $post_id );
$this->assertEqualSets( array(), $post_items );
$this->assertSameSets( array(), $post_items );
wp_delete_post( $post_2_id, true );
$post_2_items = wp_get_associated_nav_menu_items( $post_2_id );
$this->assertEqualSets( array(), $post_2_items );
$this->assertSameSets( array(), $post_2_items );
wp_delete_post( $page_id, true );
$page_items = wp_get_associated_nav_menu_items( $page_id );
$this->assertEqualSets( array(), $page_items );
$this->assertSameSets( array(), $page_items );
}
/**
+2 -2
View File
@@ -519,7 +519,7 @@ class Tests_Post_Query extends WP_UnitTestCase {
)
);
$actual_posts = $q->get_posts();
$this->assertEqualSets( $requested, $actual_posts );
$this->assertSameSets( $requested, $actual_posts );
$requested = array( $post_ids[1], $post_ids[2] );
$q->query(
@@ -529,7 +529,7 @@ class Tests_Post_Query extends WP_UnitTestCase {
)
);
$actual_posts = $q->get_posts();
$this->assertEqualSets( $requested, $actual_posts );
$this->assertSameSets( $requested, $actual_posts );
}
/**
+3 -3
View File
@@ -413,7 +413,7 @@ class Tests_Post_Types extends WP_UnitTestCase {
)
);
$this->assertEqualSetsWithIndex(
$this->assertSameSetsWithIndex(
array(
'editor' => true,
'author' => true,
@@ -563,13 +563,13 @@ class Tests_Post_Types extends WP_UnitTestCase {
* @ticket 34010
*/
public function test_get_post_types_by_support_excluding_features() {
$this->assertEqualSets( array(), get_post_types_by_support( array( 'post-formats', 'page-attributes' ) ) );
$this->assertSameSets( array(), get_post_types_by_support( array( 'post-formats', 'page-attributes' ) ) );
}
/**
* @ticket 34010
*/
public function test_get_post_types_by_support_non_existant_feature() {
$this->assertEqualSets( array(), get_post_types_by_support( 'somefeature' ) );
$this->assertSameSets( array(), get_post_types_by_support( 'somefeature' ) );
}
}
+8 -8
View File
@@ -22,14 +22,14 @@ class Tests_WP_Post_Type extends WP_UnitTestCase {
$post_type_object->remove_supports();
$post_type_supports_after = get_all_post_type_supports( $post_type );
$this->assertEqualSets(
$this->assertSameSets(
array(
'title' => true,
'editor' => true,
),
$post_type_supports
);
$this->assertEqualSets( array(), $post_type_supports_after );
$this->assertSameSets( array(), $post_type_supports_after );
}
public function test_add_supports_custom() {
@@ -51,7 +51,7 @@ class Tests_WP_Post_Type extends WP_UnitTestCase {
$post_type_object->remove_supports();
$post_type_supports_after = get_all_post_type_supports( $post_type );
$this->assertEqualSets(
$this->assertSameSets(
array(
'editor' => true,
'comments' => true,
@@ -59,7 +59,7 @@ class Tests_WP_Post_Type extends WP_UnitTestCase {
),
$post_type_supports
);
$this->assertEqualSets( array(), $post_type_supports_after );
$this->assertSameSets( array(), $post_type_supports_after );
}
/**
@@ -88,7 +88,7 @@ class Tests_WP_Post_Type extends WP_UnitTestCase {
$post_type_object->remove_supports();
$post_type_supports_after = get_all_post_type_supports( $post_type );
$this->assertEqualSets(
$this->assertSameSets(
array(
'support_with_args' => array(
array(
@@ -100,7 +100,7 @@ class Tests_WP_Post_Type extends WP_UnitTestCase {
),
$post_type_supports
);
$this->assertEqualSets( array(), $post_type_supports_after );
$this->assertSameSets( array(), $post_type_supports_after );
}
public function test_does_not_add_query_var_if_not_public() {
@@ -213,7 +213,7 @@ class Tests_WP_Post_Type extends WP_UnitTestCase {
unset( $wp_post_types[ $post_type ] );
$this->assertEqualSets( array( 'post_tag' ), $taxonomies );
$this->assertEqualSets( array(), $taxonomies_after );
$this->assertSameSets( array( 'post_tag' ), $taxonomies );
$this->assertSameSets( array(), $taxonomies_after );
}
}