Skip to content

Commit

Permalink
Tests: replace assertRegExp() with assertMatchesRegularExpression()
Browse files Browse the repository at this point in the history
The `assertRegExp()` and `assertNotRegExp()` methods were hard deprecated in PHPUnit 9.1 and the functionality will be removed in PHPUnit 10.0.0.

The `assertMatchesRegularExpression()` and `assertDoesNotMatchRegularExpression()` methods were introduced as a replacement in PHPUnit 9.1.

Ref:
* https://github.com/sebastianbergmann/phpunit/blob/9.1.5/ChangeLog-9.1.md#910---2020-04-03
* sebastianbergmann/phpunit#4085
* sebastianbergmann/phpunit#4088
  • Loading branch information
jrfnl committed Aug 16, 2020
1 parent c56dbb6 commit 1d47254
Show file tree
Hide file tree
Showing 17 changed files with 40 additions and 40 deletions.
2 changes: 1 addition & 1 deletion tests/phpunit/tests/ajax/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,6 @@ public function test_response_charset_in_xml() {

// Check the XML tag.
$contents = ob_get_clean();
$this->assertRegExp( '/<\?xml\s+version=\'1.0\'\s+encoding=\'' . preg_quote( get_option( 'blog_charset' ) ) . '\'\s+standalone=\'yes\'\?>/', $contents );
$this->assertMatchesRegularExpression( '/<\?xml\s+version=\'1.0\'\s+encoding=\'' . preg_quote( get_option( 'blog_charset' ) ) . '\'\s+standalone=\'yes\'\?>/', $contents );
}
}
2 changes: 1 addition & 1 deletion tests/phpunit/tests/avatar.php
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ public function test_get_avatar_data_should_return_gravatar_url_when_input_avata
$actual_data = get_avatar_data( $comment );

$this->assertTrue( is_avatar_comment_type( $comment_type ) );
$this->assertRegexp( '|^http?://[0-9]+.gravatar.com/avatar/[0-9a-f]{32}\?|', $actual_data['url'] );
$this->assertMatchesRegularExpression( '|^http?://[0-9]+.gravatar.com/avatar/[0-9a-f]{32}\?|', $actual_data['url'] );
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/phpunit/tests/basic.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ function test_package_json_node_engine( $package_json ) {
$this->assertArrayHasKey( 'engines', $package_json );
$this->assertArrayHasKey( 'node', $package_json['engines'] );
$node = $package_json['engines']['node'];
$this->assertRegExp( '~^=?\d+\.\d+\.\d+$~', $node, "package.json's node version cannot be a range." );
$this->assertMatchesRegularExpression( '~^=?\d+\.\d+\.\d+$~', $node, "package.json's node version cannot be a range." );
}

// Test some helper utility functions.
Expand Down
2 changes: 1 addition & 1 deletion tests/phpunit/tests/category/wpDropdownCategories.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ public function test_required_true_should_add_required_attribute() {
$dropdown_categories = wp_dropdown_categories( $args );

// Test to see if it contains the "required" attribute.
$this->assertRegExp( '/<select[^>]+required/', $dropdown_categories );
$this->assertMatchesRegularExpression( '/<select[^>]+required/', $dropdown_categories );
}

/**
Expand Down
12 changes: 6 additions & 6 deletions tests/phpunit/tests/category/wpListCategories.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function test_class_containing_current_cat() {
);

$this->assertNotRegExp( '/class="[^"]*cat-item-' . $c1 . '[^"]*current-cat[^"]*"/', $found );
$this->assertRegExp( '/class="[^"]*cat-item-' . $c2 . '[^"]*current-cat[^"]*"/', $found );
$this->assertMatchesRegularExpression( '/class="[^"]*cat-item-' . $c2 . '[^"]*current-cat[^"]*"/', $found );
}

public function test_class_containing_current_cat_parent() {
Expand All @@ -49,7 +49,7 @@ public function test_class_containing_current_cat_parent() {
)
);

$this->assertRegExp( '/class="[^"]*cat-item-' . $c1 . '[^"]*current-cat-parent[^"]*"/', $found );
$this->assertMatchesRegularExpression( '/class="[^"]*cat-item-' . $c1 . '[^"]*current-cat-parent[^"]*"/', $found );
$this->assertNotRegExp( '/class="[^"]*cat-item-' . $c2 . '[^"]*current-cat-parent[^"]*"/', $found );
}

Expand All @@ -67,9 +67,9 @@ public function test_current_category_should_accept_an_array_of_ids() {
)
);

$this->assertRegExp( '/class="[^"]*cat-item-' . $cats[0] . '[^"]*current-cat[^"]*"/', $found );
$this->assertMatchesRegularExpression( '/class="[^"]*cat-item-' . $cats[0] . '[^"]*current-cat[^"]*"/', $found );
$this->assertNotRegExp( '/class="[^"]*cat-item-' . $cats[1] . '[^"]*current[^"]*"/', $found );
$this->assertRegExp( '/class="[^"]*cat-item-' . $cats[2] . '[^"]*current-cat[^"]*"/', $found );
$this->assertMatchesRegularExpression( '/class="[^"]*cat-item-' . $cats[2] . '[^"]*current-cat[^"]*"/', $found );
}

/**
Expand Down Expand Up @@ -650,8 +650,8 @@ public function test_class_containing_current_cat_ancestor() {
)
);

$this->assertRegExp( '/class="[^"]*cat-item-' . $parent . '[^"]*current-cat-ancestor[^"]*"/', $actual );
$this->assertRegExp( '/class="[^"]*cat-item-' . $child . '[^"]*current-cat-ancestor[^"]*"/', $actual );
$this->assertMatchesRegularExpression( '/class="[^"]*cat-item-' . $parent . '[^"]*current-cat-ancestor[^"]*"/', $actual );
$this->assertMatchesRegularExpression( '/class="[^"]*cat-item-' . $child . '[^"]*current-cat-ancestor[^"]*"/', $actual );
$this->assertNotRegExp( '/class="[^"]*cat-item-' . $grandchild . '[^"]*current-cat-ancestor[^"]*"/', $actual );
$this->assertNotRegExp( '/class="[^"]*cat-item-' . $child2 . '[^"]*current-cat-ancestor[^"]*"/', $actual );
}
Expand Down
8 changes: 4 additions & 4 deletions tests/phpunit/tests/comment/commentForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function test_default_markup_for_submit_button_and_wrapper() {

$button = '<input name="foo-name" type="submit" id="foo-id" class="foo-class" value="foo-label" />';
$hidden = get_comment_id_fields( $p );
$this->assertRegExp( '|<p class="form\-submit">\s*' . $button . '\s*' . $hidden . '\s*|', $form );
$this->assertMatchesRegularExpression( '|<p class="form\-submit">\s*' . $button . '\s*' . $hidden . '\s*|', $form );
}

public function test_custom_submit_button() {
Expand Down Expand Up @@ -53,7 +53,7 @@ public function test_custom_submit_field() {

$button = '<input name="foo-name" type="submit" id="foo-id" class="foo-class" value="foo-label" />';
$hidden = get_comment_id_fields( $p );
$this->assertRegExp( '|<p class="my\-custom\-submit\-field">\s*' . $button . '\s*' . $hidden . '\s*|', $form );
$this->assertMatchesRegularExpression( '|<p class="my\-custom\-submit\-field">\s*' . $button . '\s*' . $hidden . '\s*|', $form );
}

/**
Expand All @@ -75,7 +75,7 @@ public function test_submit_button_and_submit_field_should_fall_back_on_defaults

$button = '<input name="foo-name" type="submit" id="foo-id" class="foo-class" value="foo-label" />';
$hidden = get_comment_id_fields( $p );
$this->assertRegExp( '|<p class="form\-submit">\s*' . $button . '\s*' . $hidden . '\s*|', $form );
$this->assertMatchesRegularExpression( '|<p class="form\-submit">\s*' . $button . '\s*' . $hidden . '\s*|', $form );
}

public function filter_comment_form_defaults( $defaults ) {
Expand All @@ -102,7 +102,7 @@ public function test_fields_should_include_cookies_consent() {

remove_filter( 'option_show_comments_cookies_opt_in', '__return_true' );

$this->assertRegExp( '|<p class="comment\-form\-cookies\-consent">.*?</p>|', $form );
$this->assertMatchesRegularExpression( '|<p class="comment\-form\-cookies\-consent">.*?</p>|', $form );
}

/**
Expand Down
8 changes: 4 additions & 4 deletions tests/phpunit/tests/customize/nav-menus.php
Original file line number Diff line number Diff line change
Expand Up @@ -770,7 +770,7 @@ function test_available_items_template() {
if ( $post_types ) {
foreach ( $post_types as $type ) {
$this->assertStringContainsString( 'available-menu-items-post_type-' . esc_attr( $type->name ), $template );
$this->assertRegExp( '#<h4 class="accordion-section-title".*>\s*' . esc_html( $type->labels->name ) . '#', $template );
$this->assertMatchesRegularExpression( '#<h4 class="accordion-section-title".*>\s*' . esc_html( $type->labels->name ) . '#', $template );
$this->assertStringContainsString( 'data-type="post_type"', $template );
$this->assertStringContainsString( 'data-object="' . esc_attr( $type->name ) . '"', $template );
$this->assertStringContainsString( 'data-type_label="' . esc_attr( $type->labels->singular_name ) . '"', $template );
Expand All @@ -781,15 +781,15 @@ function test_available_items_template() {
if ( $taxonomies ) {
foreach ( $taxonomies as $tax ) {
$this->assertStringContainsString( 'available-menu-items-taxonomy-' . esc_attr( $tax->name ), $template );
$this->assertRegExp( '#<h4 class="accordion-section-title".*>\s*' . esc_html( $tax->labels->name ) . '#', $template );
$this->assertMatchesRegularExpression( '#<h4 class="accordion-section-title".*>\s*' . esc_html( $tax->labels->name ) . '#', $template );
$this->assertStringContainsString( 'data-type="taxonomy"', $template );
$this->assertStringContainsString( 'data-object="' . esc_attr( $tax->name ) . '"', $template );
$this->assertStringContainsString( 'data-type_label="' . esc_attr( $tax->labels->singular_name ) . '"', $template );
}
}

$this->assertStringContainsString( 'available-menu-items-custom_type', $template );
$this->assertRegExp( '#<h4 class="accordion-section-title".*>\s*Custom#', $template );
$this->assertMatchesRegularExpression( '#<h4 class="accordion-section-title".*>\s*Custom#', $template );
$this->assertStringContainsString( 'data-type="custom_type"', $template );
$this->assertStringContainsString( 'data-object="custom_object"', $template );
$this->assertStringContainsString( 'data-type_label="Custom Type"', $template );
Expand Down Expand Up @@ -1013,7 +1013,7 @@ function test_save_nav_menus_created_posts() {
$this->assertEquals( $save_action_count + 1, did_action( 'customize_save_nav_menus_created_posts' ) );
foreach ( $drafted_post_ids as $post_id ) {
$this->assertEquals( 'publish', get_post_status( $post_id ) );
$this->assertRegExp( '/^auto-draft-\d+$/', get_post( $post_id )->post_name );
$this->assertMatchesRegularExpression( '/^auto-draft-\d+$/', get_post( $post_id )->post_name );
$this->assertEmpty( get_post_meta( $post_id, '_customize_draft_post_name', true ) );
}

Expand Down
4 changes: 2 additions & 2 deletions tests/phpunit/tests/customize/widgets.php
Original file line number Diff line number Diff line change
Expand Up @@ -426,8 +426,8 @@ function test_wp_widget_form_customize_control_json() {
$params = $control->json();

$this->assertEquals( 'widget_form', $params['type'] );
$this->assertRegExp( '#^<li[^>]+>\s*</li>$#', $params['content'] );
$this->assertRegExp( '#^<div[^>]*class=\'widget\'[^>]*#s', $params['widget_control'] );
$this->assertMatchesRegularExpression( '#^<li[^>]+>\s*</li>$#', $params['content'] );
$this->assertMatchesRegularExpression( '#^<div[^>]*class=\'widget\'[^>]*#s', $params['widget_control'] );
$this->assertStringContainsString( '<div class="widget-content"></div>', $params['widget_control'] );
$this->assertStringNotContainsString( '<input class="widefat"', $params['widget_control'] );
$this->assertStringContainsString( '<input class="widefat"', $params['widget_content'] );
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 @@ -704,7 +704,7 @@ public function test_build_time_query_hour_minute() {

// $compare value is floating point - use regex to account for
// varying precision on different PHP installations.
$this->assertRegExp( "/DATE_FORMAT\( post_date, '%H\.%i' \) = 5\.150*/", $wpdb->remove_placeholder_escape( $found ) );
$this->assertMatchesRegularExpression( "/DATE_FORMAT\( post_date, '%H\.%i' \) = 5\.150*/", $wpdb->remove_placeholder_escape( $found ) );
}

public function test_build_time_query_hour_minute_second() {
Expand All @@ -715,7 +715,7 @@ public function test_build_time_query_hour_minute_second() {

// $compare value is floating point - use regex to account for
// varying precision on different PHP installations.
$this->assertRegExp( "/DATE_FORMAT\( post_date, '%H\.%i%s' \) = 5\.15350*/", $wpdb->remove_placeholder_escape( $found ) );
$this->assertMatchesRegularExpression( "/DATE_FORMAT\( post_date, '%H\.%i%s' \) = 5\.15350*/", $wpdb->remove_placeholder_escape( $found ) );
}

public function test_build_time_query_minute_second() {
Expand All @@ -726,7 +726,7 @@ public function test_build_time_query_minute_second() {

// $compare value is floating point - use regex to account for
// varying precision on different PHP installations.
$this->assertRegExp( "/DATE_FORMAT\( post_date, '0\.%i%s' \) = 0\.15350*/", $wpdb->remove_placeholder_escape( $found ) );
$this->assertMatchesRegularExpression( "/DATE_FORMAT\( post_date, '0\.%i%s' \) = 0\.15350*/", $wpdb->remove_placeholder_escape( $found ) );
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/phpunit/tests/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -1189,7 +1189,7 @@ function test_wp_unique_id() {
$ids = array();
for ( $i = 0; $i < 20; $i += 1 ) {
$id = wp_unique_id( 'foo-' );
$this->assertRegExp( '/^foo-\d+$/', $id );
$this->assertMatchesRegularExpression( '/^foo-\d+$/', $id );
$ids[] = $id;
}
$this->assertEquals( $ids, array_unique( $ids ) );
Expand Down
2 changes: 1 addition & 1 deletion tests/phpunit/tests/meta/query.php
Original file line number Diff line number Diff line change
Expand Up @@ -926,7 +926,7 @@ public function test_empty_compare() {
$sql = $query->get_sql( 'post', $wpdb->posts, 'ID', $this );

// Use regex because we don't care about the whitespace before OR.
$this->assertRegExp( "/{$wpdb->postmeta}\.meta_key = \'exclude\'\s+OR/", $sql['where'] );
$this->assertMatchesRegularExpression( "/{$wpdb->postmeta}\.meta_key = \'exclude\'\s+OR/", $sql['where'] );
$this->assertStringNotContainsString( "{$wpdb->postmeta}.post_id IS NULL", $sql['where'] );
}

Expand Down
2 changes: 1 addition & 1 deletion tests/phpunit/tests/post/getPostsByAuthorSql.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function test_multiple_post_types() {

public function test_full_true() {
$maybe_string = get_posts_by_author_sql( 'post', true );
$this->assertRegExp( '/^WHERE /', $maybe_string );
$this->assertMatchesRegularExpression( '/^WHERE /', $maybe_string );
}

public function test_full_false() {
Expand Down
4 changes: 2 additions & 2 deletions tests/phpunit/tests/post/nav-menu.php
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ function test_wp_nav_menu_whitespace_options() {
);

// The markup should include whitespace between <li>'s.
$this->assertRegExp( '/\s<li.*>|<\/li>\s/U', $menu );
$this->assertMatchesRegularExpression( '/\s<li.*>|<\/li>\s/U', $menu );
$this->assertNotRegExp( '/<\/li><li.*>/U', $menu );

// Whitespace suppressed.
Expand All @@ -474,7 +474,7 @@ function test_wp_nav_menu_whitespace_options() {

// The markup should not include whitespace around <li>'s.
$this->assertNotRegExp( '/\s<li.*>|<\/li>\s/U', $menu );
$this->assertRegExp( '/><li.*>|<\/li></U', $menu );
$this->assertMatchesRegularExpression( '/><li.*>|<\/li></U', $menu );
}

/*
Expand Down
12 changes: 6 additions & 6 deletions tests/phpunit/tests/post/template.php
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ public function test_wp_dropdown_pages_should_obey_class_parameter() {
)
);

$this->assertRegExp( '/<select[^>]+class=\'bar\'/', $found );
$this->assertMatchesRegularExpression( '/<select[^>]+class=\'bar\'/', $found );
}

/**
Expand Down Expand Up @@ -419,13 +419,13 @@ public function test_wp_page_menu_wp_nav_menu_fallback() {
$menu = wp_nav_menu( array( 'echo' => false ) );

// After falling back, the 'before' argument should be set and output as '<ul>'.
$this->assertRegExp( '/<div class="menu"><ul>/', $menu );
$this->assertMatchesRegularExpression( '/<div class="menu"><ul>/', $menu );

// After falling back, the 'after' argument should be set and output as '</ul>'.
$this->assertRegExp( '/<\/ul><\/div>/', $menu );
$this->assertMatchesRegularExpression( '/<\/ul><\/div>/', $menu );

// After falling back, the markup should include whitespace around <li>'s.
$this->assertRegExp( '/\s<li.*>|<\/li>\s/U', $menu );
$this->assertMatchesRegularExpression( '/\s<li.*>|<\/li>\s/U', $menu );
$this->assertNotRegExp( '/><li.*>|<\/li></U', $menu );

// No menus + wp_nav_menu() falls back to wp_page_menu(), this time without a container.
Expand All @@ -437,7 +437,7 @@ public function test_wp_page_menu_wp_nav_menu_fallback() {
);

// After falling back, the empty 'container' argument should still return a container element.
$this->assertRegExp( '/<div class="menu">/', $menu );
$this->assertMatchesRegularExpression( '/<div class="menu">/', $menu );

// No menus + wp_nav_menu() falls back to wp_page_menu(), this time without white-space.
$menu = wp_nav_menu(
Expand All @@ -449,7 +449,7 @@ public function test_wp_page_menu_wp_nav_menu_fallback() {

// After falling back, the markup should not include whitespace around <li>'s.
$this->assertNotRegExp( '/\s<li.*>|<\/li>\s/U', $menu );
$this->assertRegExp( '/><li.*>|<\/li></U', $menu );
$this->assertMatchesRegularExpression( '/><li.*>|<\/li></U', $menu );

}
}
2 changes: 1 addition & 1 deletion tests/phpunit/tests/query/results.php
Original file line number Diff line number Diff line change
Expand Up @@ -795,7 +795,7 @@ function test_exclude_from_search_empty() {
$posts = $this->q->query( array( 'post_type' => 'any' ) );

$this->assertEmpty( $posts );
$this->assertRegExp( '#AND 1=0#', $this->q->request );
$this->assertMatchesRegularExpression( '#AND 1=0#', $this->q->request );

foreach ( array_keys( $wp_post_types ) as $slug ) {
$wp_post_types[ $slug ]->exclude_from_search = false;
Expand Down
8 changes: 4 additions & 4 deletions tests/phpunit/tests/term/wpGenerateTagCloud.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,8 @@ function test_hide_empty_false_format_list() {
)
);

$this->assertRegExp( "|^<ul class='wp-tag-cloud' role='list'>|", $found );
$this->assertRegExp( "|</ul>\n|", $found );
$this->assertMatchesRegularExpression( "|^<ul class='wp-tag-cloud' role='list'>|", $found );
$this->assertMatchesRegularExpression( "|</ul>\n|", $found );
$this->assertStringContainsString( '>' . $tags[0]->name . '<', $found );
}

Expand Down Expand Up @@ -184,8 +184,8 @@ function test_hide_empty_false_multi_format_list() {
)
);

$this->assertRegExp( "|^<ul class='wp-tag-cloud' role='list'>|", $found );
$this->assertRegExp( "|</ul>\n|", $found );
$this->assertMatchesRegularExpression( "|^<ul class='wp-tag-cloud' role='list'>|", $found );
$this->assertMatchesRegularExpression( "|</ul>\n|", $found );

foreach ( $tags as $tag ) {
$this->assertStringContainsString( '>' . $tag->name . '<', $found );
Expand Down
2 changes: 1 addition & 1 deletion tests/phpunit/tests/widgets.php
Original file line number Diff line number Diff line change
Expand Up @@ -695,7 +695,7 @@ function test_the_widget_custom_before_title_arg() {

unregister_widget( 'WP_Widget_Text' );

$this->assertRegExp( '/<span class="special widget_text">/', $actual );
$this->assertMatchesRegularExpression( '/<span class="special widget_text">/', $actual );

}

Expand Down

0 comments on commit 1d47254

Please sign in to comment.