Skip to content

Commit 12d4ae7

Browse files
committed
Consolidate assertions
1 parent 3c77325 commit 12d4ae7

File tree

1 file changed

+79
-70
lines changed

1 file changed

+79
-70
lines changed

tests/phpunit/tests/template.php

Lines changed: 79 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1475,14 +1475,14 @@ public function test_wp_load_classic_theme_block_styles_on_demand( string $theme
14751475
/**
14761476
* Data provider.
14771477
*
1478-
* @return array<string, array{set_up: Closure|null, theme_supports: string[], expected_head: string[], expected_footer: string[]}>
1478+
* @return array<string, array{set_up: Closure|null, theme_supports: string[], expected_styles: array{ HEAD: string[], BODY: string[] }}>
14791479
*/
14801480
public function data_wp_hoist_late_printed_styles(): array {
14811481
$theme_supports = array(
14821482
'wp-block-styles',
14831483
);
14841484

1485-
$expected_head_styles = array(
1485+
$common_expected_head_styles = array(
14861486
'wp-img-auto-sizes-contain-inline-css',
14871487
'early-css',
14881488
'early-inline-css',
@@ -1505,8 +1505,10 @@ public function data_wp_hoist_late_printed_styles(): array {
15051505
'standard_classic_theme_config' => array(
15061506
'set_up' => null,
15071507
'theme_supports' => $theme_supports,
1508-
'expected_head' => $expected_head_styles,
1509-
'expected_footer' => array(),
1508+
'expected_styles' => array(
1509+
'HEAD' => $common_expected_head_styles,
1510+
'BODY' => array(),
1511+
),
15101512
),
15111513
'standard_classic_theme_config_extra_block_library_inline_style' => array(
15121514
'set_up' => static function () {
@@ -1518,76 +1520,88 @@ static function () {
15181520
);
15191521
},
15201522
'theme_supports' => $theme_supports,
1521-
'expected_head' => ( function ( $expected_styles ) {
1522-
// Insert 'wp-block-library-inline-css' right after 'wp-block-library-css'.
1523-
$i = array_search( 'wp-block-library-css', $expected_styles, true );
1524-
$this->assertIsInt( $i, 'Expected wp-block-library-css to be among the styles.' );
1525-
array_splice( $expected_styles, $i + 1, 0, 'wp-block-library-inline-css' );
1526-
return $expected_styles;
1527-
} )( $expected_head_styles ),
1528-
'expected_footer' => array(),
1523+
'expected_styles' => array(
1524+
'HEAD' => ( function ( $expected_styles ) {
1525+
// Insert 'wp-block-library-inline-css' right after 'wp-block-library-css'.
1526+
$i = array_search( 'wp-block-library-css', $expected_styles, true );
1527+
$this->assertIsInt( $i, 'Expected wp-block-library-css to be among the styles.' );
1528+
array_splice( $expected_styles, $i + 1, 0, 'wp-block-library-inline-css' );
1529+
return $expected_styles;
1530+
} )( $common_expected_head_styles ),
1531+
'BODY' => array(),
1532+
),
15291533
),
15301534
'classic_theme_opt_out_separate_block_styles' => array(
15311535
'set_up' => static function () {
15321536
add_filter( 'should_load_separate_core_block_assets', '__return_false' );
15331537
},
15341538
'theme_supports' => $theme_supports,
1535-
'expected_head' => array(
1536-
'wp-img-auto-sizes-contain-inline-css',
1537-
'early-css',
1538-
'early-inline-css',
1539-
'wp-emoji-styles-inline-css',
1540-
'wp-block-library-css',
1541-
'wp-block-library-theme-css',
1542-
'classic-theme-styles-css',
1543-
'global-styles-inline-css',
1544-
'normal-css',
1545-
'normal-inline-css',
1546-
'wp-custom-css',
1547-
),
1548-
'expected_footer' => array(
1549-
'late-css',
1550-
'late-inline-css',
1551-
'core-block-supports-inline-css',
1539+
'expected_styles' => array(
1540+
'HEAD' => array(
1541+
'wp-img-auto-sizes-contain-inline-css',
1542+
'early-css',
1543+
'early-inline-css',
1544+
'wp-emoji-styles-inline-css',
1545+
'wp-block-library-css',
1546+
'wp-block-library-theme-css',
1547+
'classic-theme-styles-css',
1548+
'global-styles-inline-css',
1549+
'normal-css',
1550+
'normal-inline-css',
1551+
'wp-custom-css',
1552+
),
1553+
'BODY' => array(
1554+
'late-css',
1555+
'late-inline-css',
1556+
'core-block-supports-inline-css',
1557+
),
15521558
),
15531559
),
15541560
'wp_block_styles_not_supported' => array(
15551561
'set_up' => null,
15561562
'theme_supports' => array(),
1557-
'expected_head' => array_values(
1558-
array_diff(
1559-
$expected_head_styles,
1560-
array(
1561-
'wp-block-separator-theme-css',
1563+
'expected_styles' => array(
1564+
'HEAD' => array_values(
1565+
array_diff(
1566+
$common_expected_head_styles,
1567+
array(
1568+
'wp-block-separator-theme-css',
1569+
)
15621570
)
1563-
)
1571+
),
1572+
'BODY' => array(),
15641573
),
1565-
'expected_footer' => array(),
15661574
),
15671575
'_wp_footer_scripts_removed' => array(
15681576
'set_up' => static function () {
15691577
remove_action( 'wp_print_footer_scripts', '_wp_footer_scripts' );
15701578
},
15711579
'theme_supports' => $theme_supports,
1572-
'expected_head' => $expected_head_styles,
1573-
'expected_footer' => array(),
1580+
'expected_styles' => array(
1581+
'HEAD' => $common_expected_head_styles,
1582+
'BODY' => array(),
1583+
),
15741584
),
15751585
'wp_print_footer_scripts_removed' => array(
15761586
'set_up' => static function () {
15771587
remove_action( 'wp_footer', 'wp_print_footer_scripts', 20 );
15781588
},
15791589
'theme_supports' => $theme_supports,
1580-
'expected_head' => $expected_head_styles,
1581-
'expected_footer' => array(),
1590+
'expected_styles' => array(
1591+
'HEAD' => $common_expected_head_styles,
1592+
'BODY' => array(),
1593+
),
15821594
),
15831595
'both_actions_removed' => array(
15841596
'set_up' => static function () {
15851597
remove_action( 'wp_print_footer_scripts', '_wp_footer_scripts' );
15861598
remove_action( 'wp_footer', 'wp_print_footer_scripts' );
15871599
},
15881600
'theme_supports' => $theme_supports,
1589-
'expected_head' => $expected_head_styles,
1590-
'expected_footer' => array(),
1601+
'expected_styles' => array(
1602+
'HEAD' => $common_expected_head_styles,
1603+
'BODY' => array(),
1604+
),
15911605
),
15921606
'disable_block_library' => array(
15931607
'set_up' => static function () {
@@ -1601,21 +1615,23 @@ function (): void {
16011615
add_filter( 'should_load_separate_core_block_assets', '__return_false' );
16021616
},
16031617
'theme_supports' => array(),
1604-
'expected_head' => array(
1605-
'wp-img-auto-sizes-contain-inline-css',
1606-
'early-css',
1607-
'early-inline-css',
1608-
'wp-emoji-styles-inline-css',
1609-
'classic-theme-styles-css',
1610-
'global-styles-inline-css',
1611-
'normal-css',
1612-
'normal-inline-css',
1613-
'wp-custom-css',
1614-
),
1615-
'expected_footer' => array(
1616-
'late-css',
1617-
'late-inline-css',
1618-
'core-block-supports-inline-css',
1618+
'expected_styles' => array(
1619+
'HEAD' => array(
1620+
'wp-img-auto-sizes-contain-inline-css',
1621+
'early-css',
1622+
'early-inline-css',
1623+
'wp-emoji-styles-inline-css',
1624+
'classic-theme-styles-css',
1625+
'global-styles-inline-css',
1626+
'normal-css',
1627+
'normal-inline-css',
1628+
'wp-custom-css',
1629+
),
1630+
'BODY' => array(
1631+
'late-css',
1632+
'late-inline-css',
1633+
'core-block-supports-inline-css',
1634+
),
16191635
),
16201636
),
16211637
);
@@ -1630,7 +1646,7 @@ function (): void {
16301646
*
16311647
* @dataProvider data_wp_hoist_late_printed_styles
16321648
*/
1633-
public function test_wp_hoist_late_printed_styles( ?Closure $set_up, array $theme_supports, array $expected_head, array $expected_footer ): void {
1649+
public function test_wp_hoist_late_printed_styles( ?Closure $set_up, array $theme_supports, array $expected_styles ): void {
16341650
switch_theme( 'default' );
16351651
global $wp_styles;
16361652
$wp_styles = null;
@@ -1732,14 +1748,9 @@ static function () {
17321748
}
17331749

17341750
$this->assertSame(
1735-
$expected_head,
1736-
$found_styles['HEAD'],
1737-
'Expected the same styles in the HEAD. Snapshot: ' . self::get_array_snapshot_export( $found_styles['HEAD'] )
1738-
);
1739-
$this->assertSame(
1740-
$expected_footer,
1741-
$found_styles['BODY'],
1742-
'Expected the same styles in the BODY. Snapshot: ' . self::get_array_snapshot_export( $found_styles['BODY'] )
1751+
$expected_styles,
1752+
$found_styles,
1753+
'Expected the same styles. Snapshot: ' . self::get_array_snapshot_export( $found_styles )
17431754
);
17441755
}
17451756

@@ -1774,17 +1785,15 @@ public function assertTemplateHierarchy( $url, array $expected, $message = '' )
17741785
* 'c',
17751786
* )
17761787
*
1777-
* This does not currently support nested arrays.
17781788
*
17791789
* @param array $snapshot Snapshot.
17801790
* @return string Snapshot export.
17811791
*/
17821792
private static function get_array_snapshot_export( array $snapshot ): string {
17831793
$export = var_export( $snapshot, true );
17841794
$export = preg_replace( '/\barray \($/m', 'array(', $export );
1785-
if ( array_is_list( $snapshot ) ) {
1786-
$export = preg_replace( '/^(\s+)\d+\s+=>\s+/m', '$1', $export );
1787-
}
1795+
$export = preg_replace( '/^(\s+)\d+\s+=>\s+/m', '$1', $export );
1796+
$export = preg_replace( '/=> *\n +/', '=> ', $export );
17881797
return preg_replace_callback(
17891798
'/(^ +)/m',
17901799
static function ( $matches ) {

0 commit comments

Comments
 (0)