From 18abf85c71494f8aca592599507e0b65f3e8b6df Mon Sep 17 00:00:00 2001 From: robelsust Date: Mon, 9 Nov 2026 14:04:53 +0600 Subject: [PATCH] Tests: Add helpers for accessing protected properties in image editor test cases. Introduces `get_property_value()` and `set_property_value()` helper methods on `WP_Image_UnitTestCase` to eliminate repeated `ReflectionProperty` boilerplate and handle PHP 8.1+ / PHP 8.5+ deprecation-safe accessibility across image editor test classes. Refactors `Tests_Image_Editor_GD`, `Tests_Image_Editor_Imagick`, and `Tests_Image_Editor` to use the new helpers. Props therssoftware. See #66584. --- tests/phpunit/tests/image/base.php | 32 +++++++++++++++++++++ tests/phpunit/tests/image/editor.php | 23 ++++----------- tests/phpunit/tests/image/editorGd.php | 18 +++--------- tests/phpunit/tests/image/editorImagick.php | 18 +++--------- 4 files changed, 46 insertions(+), 45 deletions(-) diff --git a/tests/phpunit/tests/image/base.php b/tests/phpunit/tests/image/base.php index 496eac18a3792..b70c02eaad4ed 100644 --- a/tests/phpunit/tests/image/base.php +++ b/tests/phpunit/tests/image/base.php @@ -87,4 +87,36 @@ protected function assertImageDimensions( $filename, $width, $height ) { $this->assertSame( $width, $detected_width ); $this->assertSame( $height, $detected_height ); } + + /** + * Gets the value of a protected or private property from an image editor instance. + * + * @param WP_Image_Editor|object $editor The image editor instance. + * @param string $property_name Optional. The property name. Default 'image'. + * @return mixed The value of the property. + */ + protected function get_property_value( $editor, $property_name = 'image' ) { + $property = new ReflectionProperty( $editor, $property_name ); + if ( PHP_VERSION_ID < 80100 ) { + $property->setAccessible( true ); + } + + return $property->getValue( $editor ); + } + + /** + * Sets the value of a protected or private property on an image editor instance. + * + * @param WP_Image_Editor|object $editor The image editor instance. + * @param string $property_name The property name. + * @param mixed $value The value to set. + */ + protected function set_property_value( $editor, $property_name, $value ) { + $property = new ReflectionProperty( $editor, $property_name ); + if ( PHP_VERSION_ID < 80100 ) { + $property->setAccessible( true ); + } + + $property->setValue( $editor, $value ); + } } diff --git a/tests/phpunit/tests/image/editor.php b/tests/phpunit/tests/image/editor.php index 90a04a7f7f988..596788e99b1fe 100644 --- a/tests/phpunit/tests/image/editor.php +++ b/tests/phpunit/tests/image/editor.php @@ -207,12 +207,9 @@ public function test_generate_filename() { // Get an editor. $editor = wp_get_image_editor( DIR_TESTDATA . '/images/canola.jpg' ); - $property = new ReflectionProperty( $editor, 'size' ); - if ( PHP_VERSION_ID < 80100 ) { - $property->setAccessible( true ); - } - $property->setValue( + $this->set_property_value( $editor, + 'size', array( 'height' => 50, 'width' => 100, @@ -251,15 +248,11 @@ public function test_get_size() { $this->assertNull( $editor->get_size() ); // Set a size. - $size = array( + $size = array( 'height' => 50, 'width' => 100, ); - $property = new ReflectionProperty( $editor, 'size' ); - if ( PHP_VERSION_ID < 80100 ) { - $property->setAccessible( true ); - } - $property->setValue( $editor, $size ); + $this->set_property_value( $editor, 'size', $size ); $this->assertSame( $size, $editor->get_size() ); } @@ -276,15 +269,11 @@ public function test_get_suffix() { $this->assertFalse( $editor->get_suffix() ); // Set a size. - $size = array( + $size = array( 'height' => 50, 'width' => 100, ); - $property = new ReflectionProperty( $editor, 'size' ); - if ( PHP_VERSION_ID < 80100 ) { - $property->setAccessible( true ); - } - $property->setValue( $editor, $size ); + $this->set_property_value( $editor, 'size', $size ); $this->assertSame( '100x50', $editor->get_suffix() ); } diff --git a/tests/phpunit/tests/image/editorGd.php b/tests/phpunit/tests/image/editorGd.php index 1df7f5a570be0..a514c275b7b54 100644 --- a/tests/phpunit/tests/image/editorGd.php +++ b/tests/phpunit/tests/image/editorGd.php @@ -554,16 +554,11 @@ public function test_rotate() { $gd_image_editor = new WP_Image_Editor_GD( $file ); $gd_image_editor->load(); - $property = new ReflectionProperty( $gd_image_editor, 'image' ); - if ( PHP_VERSION_ID < 80100 ) { - $property->setAccessible( true ); - } - - $color_top_left = imagecolorat( $property->getValue( $gd_image_editor ), 0, 0 ); + $color_top_left = imagecolorat( $this->get_property_value( $gd_image_editor ), 0, 0 ); $gd_image_editor->rotate( 180 ); - $this->assertSame( $color_top_left, imagecolorat( $property->getValue( $gd_image_editor ), 99, 99 ) ); + $this->assertSame( $color_top_left, imagecolorat( $this->get_property_value( $gd_image_editor ), 99, 99 ) ); } /** @@ -575,16 +570,11 @@ public function test_flip() { $gd_image_editor = new WP_Image_Editor_GD( $file ); $gd_image_editor->load(); - $property = new ReflectionProperty( $gd_image_editor, 'image' ); - if ( PHP_VERSION_ID < 80100 ) { - $property->setAccessible( true ); - } - - $color_top_left = imagecolorat( $property->getValue( $gd_image_editor ), 0, 0 ); + $color_top_left = imagecolorat( $this->get_property_value( $gd_image_editor ), 0, 0 ); $gd_image_editor->flip( true, false ); - $this->assertSame( $color_top_left, imagecolorat( $property->getValue( $gd_image_editor ), 0, 99 ) ); + $this->assertSame( $color_top_left, imagecolorat( $this->get_property_value( $gd_image_editor ), 0, 99 ) ); } /** diff --git a/tests/phpunit/tests/image/editorImagick.php b/tests/phpunit/tests/image/editorImagick.php index bd7beac8e1ee4..9fc3adb6f06fb 100644 --- a/tests/phpunit/tests/image/editorImagick.php +++ b/tests/phpunit/tests/image/editorImagick.php @@ -439,16 +439,11 @@ public function test_rotate() { $imagick_image_editor = new WP_Image_Editor_Imagick( $file ); $imagick_image_editor->load(); - $property = new ReflectionProperty( $imagick_image_editor, 'image' ); - if ( PHP_VERSION_ID < 80100 ) { - $property->setAccessible( true ); - } - - $color_top_left = $property->getValue( $imagick_image_editor )->getImagePixelColor( 0, 0 )->getColor(); + $color_top_left = $this->get_property_value( $imagick_image_editor )->getImagePixelColor( 0, 0 )->getColor(); $imagick_image_editor->rotate( 180 ); - $this->assertSame( $color_top_left, $property->getValue( $imagick_image_editor )->getImagePixelColor( 99, 99 )->getColor() ); + $this->assertSame( $color_top_left, $this->get_property_value( $imagick_image_editor )->getImagePixelColor( 99, 99 )->getColor() ); } /** @@ -460,16 +455,11 @@ public function test_flip() { $imagick_image_editor = new WP_Image_Editor_Imagick( $file ); $imagick_image_editor->load(); - $property = new ReflectionProperty( $imagick_image_editor, 'image' ); - if ( PHP_VERSION_ID < 80100 ) { - $property->setAccessible( true ); - } - - $color_top_left = $property->getValue( $imagick_image_editor )->getImagePixelColor( 0, 0 )->getColor(); + $color_top_left = $this->get_property_value( $imagick_image_editor )->getImagePixelColor( 0, 0 )->getColor(); $imagick_image_editor->flip( true, false ); - $this->assertSame( $color_top_left, $property->getValue( $imagick_image_editor )->getImagePixelColor( 0, 99 )->getColor() ); + $this->assertSame( $color_top_left, $this->get_property_value( $imagick_image_editor )->getImagePixelColor( 0, 99 )->getColor() ); } /**