@@ -19,19 +19,7 @@ class Test_Dominant_Color extends TestCase {
19
19
* @param string[] $expected_color Expected color.
20
20
*/
21
21
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 );
35
23
$ this ->assertArrayHasKey ( 'dominant_color ' , $ dominant_color_metadata );
36
24
$ this ->assertNotEmpty ( $ dominant_color_metadata ['dominant_color ' ] );
37
25
$ 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
77
65
* @param bool $expected_transparency Expected transparency.
78
66
*/
79
67
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 );
92
69
$ this ->assertArrayHasKey ( 'has_transparency ' , $ transparency_metadata );
93
70
$ this ->assertSame ( $ expected_transparency , $ transparency_metadata ['has_transparency ' ] );
94
71
}
@@ -527,4 +504,25 @@ public function test_dominant_color_prepare_attachment_for_js(): void {
527
504
$ this ->assertEquals ( 'ff0000 ' , $ response ['dominantColor ' ] );
528
505
$ this ->assertTrue ( $ response ['hasTransparency ' ] );
529
506
}
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
+ }
530
528
}
0 commit comments