Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions tests/phpunit/tests/image/base.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}
}
23 changes: 6 additions & 17 deletions tests/phpunit/tests/image/editor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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() );
}
Expand All @@ -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() );
}
Expand Down
18 changes: 4 additions & 14 deletions tests/phpunit/tests/image/editorGd.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) );
}

/**
Expand All @@ -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 ) );
}

/**
Expand Down
18 changes: 4 additions & 14 deletions tests/phpunit/tests/image/editorImagick.php
Original file line number Diff line number Diff line change
Expand Up @@ -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() );
}

/**
Expand All @@ -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() );
}

/**
Expand Down
Loading