Skip to content

Commit

Permalink
Tests: Reduce usage of assertEquals
Browse files Browse the repository at this point in the history
Replaces assertSame with assertCount in a number of tests.

Props ayeshrajans, jorbin.
See #58956.


git-svn-id: https://develop.svn.wordpress.org/trunk@56746 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information
aaronjorbin committed Sep 29, 2023
1 parent 1f914ec commit 702e2c7
Show file tree
Hide file tree
Showing 16 changed files with 75 additions and 91 deletions.
2 changes: 1 addition & 1 deletion tests/phpunit/tests/blocks/getBlockTemplates.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ public function test_get_block_templates_returns_unique_entities( $template_type
$this->assertNotEmpty( $block_templates, 'get_block_templates() must return a non-empty value.' );

$block_template_ids = wp_list_pluck( $block_templates, 'id' );
$this->assertSame( count( array_unique( $block_template_ids ) ), count( $block_template_ids ), $error_message );
$this->assertCount( count( array_unique( $block_template_ids ) ), $block_template_ids, $error_message );
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/phpunit/tests/customize/widgets.php
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ public function test_get_selective_refreshable_widgets_when_theme_supports() {

$selective_refreshable_widgets = $this->manager->widgets->get_selective_refreshable_widgets();
$this->assertIsArray( $selective_refreshable_widgets );
$this->assertSame( count( $wp_widget_factory->widgets ), count( $selective_refreshable_widgets ) );
$this->assertCount( count( $wp_widget_factory->widgets ), $selective_refreshable_widgets );
$this->assertArrayHasKey( 'text', $selective_refreshable_widgets );
$this->assertTrue( $selective_refreshable_widgets['text'] );
$this->assertArrayHasKey( 'search', $selective_refreshable_widgets );
Expand Down
6 changes: 3 additions & 3 deletions tests/phpunit/tests/date/query.php
Original file line number Diff line number Diff line change
Expand Up @@ -1184,7 +1184,7 @@ public function test_relation_in_query_and() {

$parts = mb_split( '\)\s+AND\s+\(', $sql );
$this->assertIsArray( $parts, 'SQL query cannot be split into multiple parts using operator AND.' );
$this->assertSame( 2, count( $parts ), 'SQL query does not contain correct number of AND operators.' );
$this->assertCount( 2, $parts, 'SQL query does not contain correct number of AND operators.' );

$this->assertStringNotContainsString( 'OR', $sql, 'SQL query contains conditions joined by operator OR.' );
}
Expand Down Expand Up @@ -1231,7 +1231,7 @@ public function test_relation_in_query_or() {

$parts = mb_split( '\)\s+OR\s+\(', $sql );
$this->assertIsArray( $parts, 'SQL query cannot be split into multiple parts using operator OR.' );
$this->assertSame( 2, count( $parts ), 'SQL query does not contain correct number of OR operators.' );
$this->assertCount( 2, $parts, 'SQL query does not contain correct number of OR operators.' );

// Checking number of occurrences of AND while skipping the one at the beginning.
$this->assertSame( 2, substr_count( substr( $sql, 5 ), 'AND' ), 'SQL query does not contain expected number conditions joined by operator AND.' );
Expand Down Expand Up @@ -1277,7 +1277,7 @@ public function test_relation_in_query_unsupported() {

$parts = mb_split( '\)\s+AND\s+\(', $sql );
$this->assertIsArray( $parts, 'SQL query cannot be split into multiple parts using operator AND.' );
$this->assertSame( 2, count( $parts ), 'SQL query does not contain correct number of AND operators.' );
$this->assertCount( 2, $parts, 'SQL query does not contain correct number of AND operators.' );

$this->assertStringNotContainsString( 'OR', $sql, 'SQL query contains conditions joined by operator OR.' );
}
Expand Down
2 changes: 1 addition & 1 deletion tests/phpunit/tests/feed/rss2.php
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ public function test_item_elements() {
}
$cats = array_filter( $cats );
// Should be the same number of categories.
$this->assertSame( count( $cats ), count( $categories ) );
$this->assertCount( count( $cats ), $categories );

// ..with the same names.
foreach ( $cats as $id => $cat ) {
Expand Down
62 changes: 28 additions & 34 deletions tests/phpunit/tests/meta.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,56 +151,50 @@ public function test_user_metadata_not_exists() {
$this->assertNotEquals( $this->author->user_login, $u[0]->user_login );

// Test EXISTS and NOT EXISTS together, no users should be found.
$this->assertSame(
$this->assertCount(
0,
count(
get_users(
array(
'meta_query' => array(
array(
'key' => 'meta_key',
'compare' => 'NOT EXISTS',
),
array(
'key' => 'delete_meta_key',
'compare' => 'EXISTS',
),
get_users(
array(
'meta_query' => array(
array(
'key' => 'meta_key',
'compare' => 'NOT EXISTS',
),
array(
'key' => 'delete_meta_key',
'compare' => 'EXISTS',
),
)
),
)
)
);

$this->assertSame(
$this->assertCount(
2,
count(
get_users(
array(
'meta_query' => array(
array(
'key' => 'non_existing_meta',
'compare' => 'NOT EXISTS',
),
get_users(
array(
'meta_query' => array(
array(
'key' => 'non_existing_meta',
'compare' => 'NOT EXISTS',
),
)
),
)
)
);

delete_metadata( 'user', $this->author->ID, 'meta_key' );

$this->assertSame(
$this->assertCount(
2,
count(
get_users(
array(
'meta_query' => array(
array(
'key' => 'meta_key',
'compare' => 'NOT EXISTS',
),
get_users(
array(
'meta_query' => array(
array(
'key' => 'meta_key',
'compare' => 'NOT EXISTS',
),
)
),
)
)
);
Expand Down
24 changes: 10 additions & 14 deletions tests/phpunit/tests/post/getPages.php
Original file line number Diff line number Diff line change
Expand Up @@ -274,25 +274,21 @@ public function test_get_pages_meta() {
add_post_meta( $posts[1], 'some-meta-key', '' );
add_post_meta( $posts[2], 'some-meta-key', '1' );

$this->assertSame(
$this->assertCount(
1,
count(
get_pages(
array(
'meta_key' => 'some-meta-key',
'meta_value' => '0',
)
get_pages(
array(
'meta_key' => 'some-meta-key',
'meta_value' => '0',
)
)
);
$this->assertSame(
$this->assertCount(
1,
count(
get_pages(
array(
'meta_key' => 'some-meta-key',
'meta_value' => '1',
)
get_pages(
array(
'meta_key' => 'some-meta-key',
'meta_value' => '1',
)
)
);
Expand Down
6 changes: 3 additions & 3 deletions tests/phpunit/tests/rest-api/rest-categories-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ public function test_get_items_parent_zero_arg() {
'parent' => 0,
);
$categories = get_terms( 'category', $args );
$this->assertSame( count( $categories ), count( $data ) );
$this->assertCount( count( $categories ), $data );
}

public function test_get_items_parent_zero_arg_string() {
Expand Down Expand Up @@ -255,7 +255,7 @@ public function test_get_items_parent_zero_arg_string() {
'parent' => 0,
);
$categories = get_terms( 'category', $args );
$this->assertSame( count( $categories ), count( $data ) );
$this->assertCount( count( $categories ), $data );
}

public function test_get_items_by_parent_non_found() {
Expand Down Expand Up @@ -1189,7 +1189,7 @@ protected function check_get_taxonomy_terms_response( $response ) {
'hide_empty' => false,
);
$categories = get_terms( 'category', $args );
$this->assertSame( count( $categories ), count( $data ) );
$this->assertCount( count( $categories ), $data );
$this->assertSame( $categories[0]->term_id, $data[0]['id'] );
$this->assertSame( $categories[0]->name, $data[0]['name'] );
$this->assertSame( $categories[0]->slug, $data[0]['slug'] );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function test_get_items() {

$data = $response->get_data();
$post_types = get_post_types( array( 'show_in_rest' => true ), 'objects' );
$this->assertSame( count( $post_types ), count( $data ) );
$this->assertCount( count( $post_types ), $data );
$this->assertSame( $post_types['post']->name, $data['post']['slug'] );
$this->check_post_type_obj( 'view', $post_types['post'], $data['post'], $data['post']['_links'] );
$this->assertSame( $post_types['page']->name, $data['page']['slug'] );
Expand Down
2 changes: 1 addition & 1 deletion tests/phpunit/tests/rest-api/rest-tags-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -1409,7 +1409,7 @@ protected function check_get_taxonomy_terms_response( $response ) {
'hide_empty' => false,
);
$tags = get_terms( 'post_tag', $args );
$this->assertSame( count( $tags ), count( $data ) );
$this->assertCount( count( $tags ), $data );
$this->assertSame( $tags[0]->term_id, $data[0]['id'] );
$this->assertSame( $tags[0]->name, $data[0]['name'] );
$this->assertSame( $tags[0]->slug, $data[0]['slug'] );
Expand Down
6 changes: 3 additions & 3 deletions tests/phpunit/tests/rest-api/rest-taxonomies-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function test_get_items() {
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
$taxonomies = $this->get_public_taxonomies( get_taxonomies( '', 'objects' ) );
$this->assertSame( count( $taxonomies ), count( $data ) );
$this->assertCount( count( $taxonomies ), $data );
$this->assertSame( 'Categories', $data['category']['name'] );
$this->assertSame( 'category', $data['category']['slug'] );
$this->assertTrue( $data['category']['hierarchical'] );
Expand All @@ -69,7 +69,7 @@ public function test_get_items_context_edit() {
$taxonomies = get_taxonomies( '', 'objects' );
unset( $taxonomies['nav_menu'] ); // Menus are not editable by contributors.
$taxonomies = $this->get_public_taxonomies( $taxonomies );
$this->assertSame( count( $taxonomies ), count( $data ) );
$this->assertCount( count( $taxonomies ), $data );
$this->assertSame( 'Categories', $data['category']['name'] );
$this->assertSame( 'category', $data['category']['slug'] );
$this->assertTrue( $data['category']['hierarchical'] );
Expand Down Expand Up @@ -286,7 +286,7 @@ protected function check_taxonomies_for_type_response( $type, $response ) {
$this->assertSame( 200, $response->get_status() );
$data = $response->get_data();
$taxonomies = $this->get_public_taxonomies( get_object_taxonomies( $type, 'objects' ) );
$this->assertSame( count( $taxonomies ), count( $data ) );
$this->assertCount( count( $taxonomies ), $data );
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/phpunit/tests/rest-api/wpRestMenuItemsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -743,7 +743,7 @@ public function test_get_item_schema() {
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
$properties = $data['schema']['properties'];
$this->assertSame( 18, count( $properties ) );
$this->assertCount( 18, $properties );
$this->assertArrayHasKey( 'type_label', $properties );
$this->assertArrayHasKey( 'attr_title', $properties );
$this->assertArrayHasKey( 'classes', $properties );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ public function test_get_item_schema() {
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
$properties = $data['schema']['properties'];
$this->assertSame( 3, count( $properties ) );
$this->assertCount( 3, $properties );
$this->assertArrayHasKey( 'name', $properties );
$this->assertArrayHasKey( 'description', $properties );
$this->assertArrayHasKey( 'menu', $properties );
Expand Down
4 changes: 2 additions & 2 deletions tests/phpunit/tests/rest-api/wpRestMenusController.php
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ public function test_get_item_schema() {
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
$properties = $data['schema']['properties'];
$this->assertSame( 7, count( $properties ) );
$this->assertCount( 7, $properties );
$this->assertArrayHasKey( 'id', $properties );
$this->assertArrayHasKey( 'description', $properties );
$this->assertArrayHasKey( 'meta', $properties );
Expand Down Expand Up @@ -573,7 +573,7 @@ protected function check_get_taxonomy_terms_response( $response ) {
'hide_empty' => false,
);
$tags = get_terms( self::TAXONOMY, $args );
$this->assertSame( count( $tags ), count( $data ) );
$this->assertCount( count( $tags ), $data );
$this->assertSame( $tags[0]->term_id, $data[0]['id'] );
$this->assertSame( $tags[0]->name, $data[0]['name'] );
$this->assertSame( $tags[0]->slug, $data[0]['slug'] );
Expand Down
2 changes: 1 addition & 1 deletion tests/phpunit/tests/term/getTerms.php
Original file line number Diff line number Diff line change
Expand Up @@ -3172,7 +3172,7 @@ public function test_cache_key_generation( $args_1, $args_2 ) {
$num_queries_1 = get_num_queries();
$query2 = get_terms( $args_2 );
$this->assertSame( $num_queries_1, get_num_queries() );
$this->assertSame( count( $query1 ), count( $query2 ) );
$this->assertCount( count( $query1 ), $query2 );
}

/**
Expand Down
40 changes: 17 additions & 23 deletions tests/phpunit/tests/user.php
Original file line number Diff line number Diff line change
Expand Up @@ -2083,15 +2083,13 @@ public function test_filter_wp_privacy_additional_user_profile_data() {
$this->assertCount( 12, $actual['data'][0]['data'] );

// Check that the item added by the filter was retained.
$this->assertSame(
$this->assertCount(
1,
count(
wp_list_filter(
$actual['data'][0]['data'],
array(
'name' => 'Test Additional Data Name',
'value' => 'Test Additional Data Value',
)
wp_list_filter(
$actual['data'][0]['data'],
array(
'name' => 'Test Additional Data Name',
'value' => 'Test Additional Data Value',
)
)
);
Expand All @@ -2115,28 +2113,24 @@ public function test_filter_wp_privacy_additional_user_profile_data() {
$this->assertCount( 12, $actual['data'][0]['data'] );

// Check that the duplicate 'name' => 'User ID' was stripped.
$this->assertSame(
$this->assertCount(
1,
count(
wp_list_filter(
$actual['data'][0]['data'],
array(
'name' => 'User ID',
)
wp_list_filter(
$actual['data'][0]['data'],
array(
'name' => 'User ID',
)
)
);

// Check that the item added by the filter was retained.
$this->assertSame(
$this->assertCount(
1,
count(
wp_list_filter(
$actual['data'][0]['data'],
array(
'name' => 'Test Additional Data Name',
'value' => 'Test Additional Data Value',
)
wp_list_filter(
$actual['data'][0]['data'],
array(
'name' => 'Test Additional Data Name',
'value' => 'Test Additional Data Value',
)
)
);
Expand Down
2 changes: 1 addition & 1 deletion tests/phpunit/tests/widgets/wpWidgetMedia.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public function test_constructor() {
),
array_keys( $widget->l10n )
);
$this->assertSame( count( $widget->l10n ), count( array_filter( $widget->l10n ) ), 'Expected all translation strings to be defined.' );
$this->assertCount( count( $widget->l10n ), array_filter( $widget->l10n ), 'Expected all translation strings to be defined.' );
$this->assertSame( 10, has_action( 'admin_print_scripts-widgets.php', array( $widget, 'enqueue_admin_scripts' ) ) );
$this->assertFalse( has_action( 'wp_enqueue_scripts', array( $widget, 'enqueue_preview_scripts' ) ), 'Did not expect preview scripts to be enqueued when not in customize preview context.' );
$this->assertSame( 10, has_action( 'admin_footer-widgets.php', array( $widget, 'render_control_template_scripts' ) ) );
Expand Down

0 comments on commit 702e2c7

Please sign in to comment.