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.

git-svn-id: https://develop.svn.wordpress.org/trunk@56996 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Tonya Mork
2023-10-24 10:57:03 +00:00
parent ac8bca4fdf
commit 88ab0a2c57

View File

@@ -112,18 +112,31 @@ class Tests_Block_Supports_Duotone extends WP_UnitTestCase {
* @covers ::render_duotone_support
*/
public function test_css_declarations_are_generated_even_with_empty_block_content() {
$block = array(
$block = array(
'blockName' => 'core/image',
'attrs' => array( 'style' => array( 'color' => array( 'duotone' => 'var:preset|duotone|blue-orange' ) ) ),
);
$wp_block = new WP_Block( $block );
$wp_block = new WP_Block( $block );
/*
* Handling to access the static WP_Duotone::$block_css_declarations property.
*
* Why is an instance needed?
* WP_Duotone is a static class by design, meaning it only contains static properties and methods.
* In production, it should not be instantiated. However, as of PHP 8.3, ReflectionProperty::setValue()
* needs an object.
*/
$wp_duotone = new WP_Duotone();
$block_css_declarations_property = new ReflectionProperty( 'WP_Duotone', 'block_css_declarations' );
$block_css_declarations_property->setAccessible( true );
$block_css_declarations_property->setValue( $wp_block, array() );
$previous_value = $block_css_declarations_property->getValue();
$block_css_declarations_property->setValue( $wp_duotone, array() );
WP_Duotone::render_duotone_support( '', $block, $wp_block );
$actual = $block_css_declarations_property->getValue();
// Reset the property's visibility.
// Reset the property.
$block_css_declarations_property->setValue( $wp_duotone, $previous_value );
$block_css_declarations_property->setAccessible( false );
$this->assertNotEmpty( $actual );