Make WordPress Core


Ignore:
Timestamp:
07/28/2022 10:37:00 PM (3 years ago)
Author:
SergeyBiryukov
Message:

Tests: Simplify some assertions in Tests_Media.

A number of assertions are checking for the number of times a hard-coded substring existed in a larger $haystack.

These assertions were using preg_match_all() to do so, but without actually using a regex.

In these cases, it is more performant (and simpler) to use the PHP native substr_count() function, which will yield the same result, without the overhead of regex parsing.

Reference: PHP Manual: substr_count()

Follow-up to [711/tests], [38838], [42694], [53558].

Props jrf.
See #55652.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/media.php

    r53751 r53790  
    158158        );
    159159
    160         $this->assertSame( 2, preg_match_all( '/wp-caption/', $result, $_r ) );
    161         $this->assertSame( 1, preg_match_all( '/alignnone/', $result, $_r ) );
    162         $this->assertSame( 1, preg_match_all( '/' . self::CAPTION . '/', $result, $_r ) );
     160        $this->assertSame( 2, substr_count( $result, 'wp-caption' ) );
     161        $this->assertSame( 1, substr_count( $result, 'alignnone' ) );
     162        $this->assertSame( 1, substr_count( $result, self::CAPTION ) );
    163163
    164164        if ( current_theme_supports( 'html5', 'caption' ) ) {
    165             $this->assertSame( 1, preg_match_all( '/width: 20/', $result, $_r ) );
     165            $this->assertSame( 1, substr_count( $result, 'width: 20' ) );
    166166        } else {
    167             $this->assertSame( 1, preg_match_all( '/width: 30/', $result, $_r ) );
     167            $this->assertSame( 1, substr_count( $result, 'width: 30' ) );
    168168        }
    169169    }
     
    178178            )
    179179        );
    180         $this->assertSame( 1, preg_match_all( '/wp-caption &myAlignment/', $result, $_r ) );
    181         $this->assertSame( 1, preg_match_all( '/id="myId"/', $result, $_r ) );
    182         $this->assertSame( 1, preg_match_all( '/' . self::CAPTION . '/', $result, $_r ) );
     180        $this->assertSame( 1, substr_count( $result, 'wp-caption &myAlignment' ) );
     181        $this->assertSame( 1, substr_count( $result, 'id="myId"' ) );
     182        $this->assertSame( 1, substr_count( $result, self::CAPTION ) );
    183183    }
    184184
     
    191191            )
    192192        );
    193         $this->assertSame( 1, preg_match_all( '/wp-caption alignnone some-class another-class/', $result, $_r ) );
     193        $this->assertSame( 1, substr_count( $result, 'wp-caption alignnone some-class another-class' ) );
    194194
    195195    }
    196196
    197197    public function test_new_img_caption_shortcode_with_html_caption() {
    198         $result   = img_caption_shortcode(
     198        $result = img_caption_shortcode(
    199199            array(
    200200                'width'   => 20,
     
    202202            )
    203203        );
    204         $our_preg = preg_quote( self::HTML_CONTENT );
    205 
    206         $this->assertSame( 1, preg_match_all( "~{$our_preg}~", $result, $_r ) );
     204
     205        $this->assertSame( 1, substr_count( $result, self::HTML_CONTENT ) );
    207206    }
    208207
     
    257256        );
    258257
    259         $this->assertSame( 1, preg_match_all( '/aria-describedby="caption-myId"/', $result, $_r ) );
     258        $this->assertSame( 1, substr_count( $result, 'aria-describedby="caption-myId"' ) );
    260259    }
    261260
Note: See TracChangeset for help on using the changeset viewer.