Make WordPress Core


Ignore:
Timestamp:
10/24/2023 10:57:03 AM (6 months ago)
Author:
hellofromTonya
Message:

Tests: Fix static property handling in r56991.

Fixes static property handling for WP_Duotone::$block_css_declarations in the Tests_Block_Supports_Duotone::test_css_declarations_are_generated_even_with_empty_block_content():

  • Fixes ReflectionProperty::setValue() to use an instance of WP_Duotone.
  • Adds an inline comment to explain why a static class (i.e. a class that is not intended to be an object by design as it only contains static properties and methods) needs an instance, i.e. needed for PHP 8.3 and higher.
  • Resets the static property's value to its original value, i.e. before the test started.

Follow-up to [56991].

Props costdev.
See #59694.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/block-supports/duotone.php

    r56991 r56996  
    113113     */
    114114    public function test_css_declarations_are_generated_even_with_empty_block_content() {
    115         $block                           = array(
     115        $block    = array(
    116116            'blockName' => 'core/image',
    117117            'attrs'     => array( 'style' => array( 'color' => array( 'duotone' => 'var:preset|duotone|blue-orange' ) ) ),
    118118        );
    119         $wp_block                        = new WP_Block( $block );
     119        $wp_block = new WP_Block( $block );
     120
     121        /*
     122         * Handling to access the static WP_Duotone::$block_css_declarations property.
     123         *
     124         * Why is an instance needed?
     125         * WP_Duotone is a static class by design, meaning it only contains static properties and methods.
     126         * In production, it should not be instantiated. However, as of PHP 8.3, ReflectionProperty::setValue()
     127         * needs an object.
     128         */
     129        $wp_duotone                      = new WP_Duotone();
    120130        $block_css_declarations_property = new ReflectionProperty( 'WP_Duotone', 'block_css_declarations' );
    121131        $block_css_declarations_property->setAccessible( true );
    122         $block_css_declarations_property->setValue( $wp_block, array() );
     132        $previous_value = $block_css_declarations_property->getValue();
     133        $block_css_declarations_property->setValue( $wp_duotone, array() );
    123134
    124135        WP_Duotone::render_duotone_support( '', $block, $wp_block );
    125136        $actual = $block_css_declarations_property->getValue();
    126         // Reset the property's visibility.
     137
     138        // Reset the property.
     139        $block_css_declarations_property->setValue( $wp_duotone, $previous_value );
    127140        $block_css_declarations_property->setAccessible( false );
    128141
Note: See TracChangeset for help on using the changeset viewer.