Skip to content

Commit e6c54b0

Browse files
committed
Extract common PHP into helper method
1 parent ce31697 commit e6c54b0

File tree

1 file changed

+23
-25
lines changed

1 file changed

+23
-25
lines changed

plugins/dominant-color-images/tests/test-dominant-color.php

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,7 @@ class Test_Dominant_Color extends TestCase {
1919
* @param string[] $expected_color Expected color.
2020
*/
2121
public function test_dominant_color_metadata( string $image_path, array $expected_color ): void {
22-
$mime_type = wp_check_filetype( $image_path )['type'];
23-
if ( ! wp_image_editor_supports( array( 'mime_type' => $mime_type ) ) ) {
24-
$this->markTestSkipped( "Mime type $mime_type is not supported." );
25-
}
26-
27-
// Non existing attachment.
28-
$dominant_color_metadata = dominant_color_metadata( array(), 1 );
29-
$this->assertEmpty( $dominant_color_metadata );
30-
31-
// Creating attachment.
32-
$attachment_id = self::factory()->attachment->create_upload_object( $image_path );
33-
wp_maybe_generate_attachment_metadata( get_post( $attachment_id ) );
34-
$dominant_color_metadata = dominant_color_metadata( array(), $attachment_id );
22+
$dominant_color_metadata = $this->get_transparency_metadata( $image_path );
3523
$this->assertArrayHasKey( 'dominant_color', $dominant_color_metadata );
3624
$this->assertNotEmpty( $dominant_color_metadata['dominant_color'] );
3725
$this->assertContains( $dominant_color_metadata['dominant_color'], $expected_color );
@@ -77,18 +65,7 @@ public function test_dominant_color_get_dominant_color( string $image_path, arra
7765
* @param bool $expected_transparency Expected transparency.
7866
*/
7967
public function test_has_transparency_metadata( string $image_path, array $expected_color, bool $expected_transparency ): void {
80-
$mime_type = wp_check_filetype( $image_path )['type'];
81-
if ( ! wp_image_editor_supports( array( 'mime_type' => $mime_type ) ) ) {
82-
$this->markTestSkipped( "Mime type $mime_type is not supported." );
83-
}
84-
85-
// Non-existing attachment.
86-
$transparency_metadata = dominant_color_metadata( array(), 1 );
87-
$this->assertEmpty( $transparency_metadata );
88-
89-
$attachment_id = self::factory()->attachment->create_upload_object( $image_path );
90-
wp_maybe_generate_attachment_metadata( get_post( $attachment_id ) );
91-
$transparency_metadata = dominant_color_metadata( array(), $attachment_id );
68+
$transparency_metadata = $this->get_transparency_metadata( $image_path );
9269
$this->assertArrayHasKey( 'has_transparency', $transparency_metadata );
9370
$this->assertSame( $expected_transparency, $transparency_metadata['has_transparency'] );
9471
}
@@ -527,4 +504,25 @@ public function test_dominant_color_prepare_attachment_for_js(): void {
527504
$this->assertEquals( 'ff0000', $response['dominantColor'] );
528505
$this->assertTrue( $response['hasTransparency'] );
529506
}
507+
508+
/**
509+
* Gets transparency metadata.
510+
*
511+
* @param string $image_path Image path.
512+
* @return array{ has_transparency?: bool, dominant_color?: string } Data.
513+
*/
514+
private function get_transparency_metadata( string $image_path ): array {
515+
$mime_type = wp_check_filetype( $image_path )['type'];
516+
if ( ! wp_image_editor_supports( array( 'mime_type' => $mime_type ) ) ) {
517+
$this->markTestSkipped( "Mime type $mime_type is not supported." );
518+
}
519+
520+
// Non-existing attachment.
521+
$transparency_metadata = dominant_color_metadata( array(), 1 );
522+
$this->assertEmpty( $transparency_metadata );
523+
524+
$attachment_id = self::factory()->attachment->create_upload_object( $image_path );
525+
wp_maybe_generate_attachment_metadata( get_post( $attachment_id ) );
526+
return dominant_color_metadata( array(), $attachment_id );
527+
}
530528
}

0 commit comments

Comments
 (0)