5
5
* @since 1.2.0
6
6
*
7
7
* @package dominant-color-images
8
+ * @noinspection PhpComposerExtensionStubsInspection
9
+ * @noinspection PhpUnhandledExceptionInspection
8
10
*/
9
11
10
12
use Dominant_Color_Images \Tests \TestCase ;
11
13
14
+ /**
15
+ * @coversDefaultClass Dominant_Color_Image_Editor_Imagick
16
+ */
12
17
class Test_Dominant_Color_Image_Editor_Imagick extends TestCase {
13
18
14
19
/**
@@ -33,4 +38,164 @@ static function ( $editor ): bool {
33
38
}
34
39
);
35
40
}
41
+
42
+ /**
43
+ * @covers ::get_dominant_color
44
+ */
45
+ public function test_failure_handling (): void {
46
+ $ editor = new Dominant_Color_Image_Editor_Imagick ( '/image.jpg ' );
47
+ $ result = $ editor ->get_dominant_color ();
48
+ $ this ->assertWPError ( $ result );
49
+ $ this ->assertEquals ( 'image_editor_dominant_color_error_no_image ' , $ result ->get_error_code () );
50
+ }
51
+
52
+ /**
53
+ * @covers ::get_dominant_color
54
+ */
55
+ public function test_get_dominant_color_no_image (): void {
56
+ $ editor = new Dominant_Color_Image_Editor_Imagick ( null );
57
+ $ result = $ editor ->get_dominant_color ();
58
+
59
+ $ this ->assertWPError ( $ result );
60
+ $ this ->assertEquals ( 'image_editor_dominant_color_error_no_image ' , $ result ->get_error_code () );
61
+ }
62
+
63
+ /**
64
+ * @covers ::get_dominant_color
65
+ */
66
+ public function test_get_dominant_color_exception (): void {
67
+ // Creating mock that will throw an exception.
68
+ $ mock = $ this ->getMockBuilder ( Dominant_Color_Image_Editor_Imagick::class )
69
+ ->onlyMethods ( array ( '__construct ' ) )
70
+ ->disableOriginalConstructor ()
71
+ ->getMock ();
72
+
73
+ $ reflection = new ReflectionClass ( $ mock );
74
+ $ property = $ reflection ->getProperty ( 'image ' );
75
+ $ property ->setAccessible ( true );
76
+ $ property ->setValue ( $ mock , new Imagick () );
77
+
78
+ $ result = $ mock ->get_dominant_color ();
79
+
80
+ $ this ->assertWPError ( $ result );
81
+ $ this ->assertEquals ( 'image_editor_dominant_color_error ' , $ result ->get_error_code () );
82
+ }
83
+
84
+ /**
85
+ * @covers ::has_transparency
86
+ */
87
+ public function test_has_transparency_no_image (): void {
88
+ $ editor = new Dominant_Color_Image_Editor_Imagick ( null );
89
+ $ result = $ editor ->has_transparency ();
90
+
91
+ $ this ->assertWPError ( $ result );
92
+ $ this ->assertEquals ( 'image_editor_has_transparency_error_no_image ' , $ result ->get_error_code () );
93
+ }
94
+
95
+ /**
96
+ * @covers ::has_transparency
97
+ */
98
+ public function test_has_no_transparency (): void {
99
+ $ imagick = new Imagick ();
100
+ $ imagick ->newImage ( 1 , 1 , new ImagickPixel ( 'red ' ) );
101
+
102
+ $ editor = new Dominant_Color_Image_Editor_Imagick ( null );
103
+ $ reflection = new ReflectionClass ( $ editor );
104
+ $ property = $ reflection ->getProperty ( 'image ' );
105
+ $ property ->setAccessible ( true );
106
+ $ property ->setValue ( $ editor , $ imagick );
107
+
108
+ $ result = $ editor ->has_transparency ();
109
+
110
+ $ this ->assertFalse ( $ result );
111
+ }
112
+
113
+ /**
114
+ * @covers ::has_transparency
115
+ */
116
+ public function test_has_transparency_exception (): void {
117
+ // Creating mock that will throw an exception.
118
+ $ mock = $ this ->getMockBuilder ( Dominant_Color_Image_Editor_Imagick::class )
119
+ ->onlyMethods ( array ( '__construct ' ) )
120
+ ->disableOriginalConstructor ()
121
+ ->getMock ();
122
+
123
+ $ reflection = new ReflectionClass ( $ mock );
124
+ $ property = $ reflection ->getProperty ( 'image ' );
125
+ $ property ->setAccessible ( true );
126
+ $ property ->setValue ( $ mock , new Imagick () );
127
+
128
+ $ result = $ mock ->has_transparency ();
129
+
130
+ $ this ->assertWPError ( $ result );
131
+ $ this ->assertEquals ( 'image_editor_has_transparency_error ' , $ result ->get_error_code () );
132
+ }
133
+
134
+ /**
135
+ * @covers ::get_dominant_color
136
+ */
137
+ public function test_get_dominant_color_success (): void {
138
+ // Create a red test image.
139
+ $ imagick = new Imagick ();
140
+ $ imagick ->newImage ( 1 , 1 , new ImagickPixel ( '#FF0000 ' ) );
141
+
142
+ $ editor = new Dominant_Color_Image_Editor_Imagick ( null );
143
+ $ reflection = new ReflectionClass ( $ editor );
144
+ $ property = $ reflection ->getProperty ( 'image ' );
145
+ $ property ->setAccessible ( true );
146
+ $ property ->setValue ( $ editor , $ imagick );
147
+
148
+ $ result = $ editor ->get_dominant_color ();
149
+
150
+ $ this ->assertEquals ( 'ff0000 ' , $ result );
151
+ }
152
+
153
+ /**
154
+ * @covers ::has_transparency
155
+ */
156
+ public function test_has_transparency_with_transparency (): void {
157
+ // Create an image with transparency.
158
+ $ imagick = new Imagick ();
159
+ $ imagick ->newImage ( 1 , 1 , new ImagickPixel ( 'rgba(255,0,0,0.5) ' ) );
160
+
161
+ $ editor = new Dominant_Color_Image_Editor_Imagick ( null );
162
+ $ reflection = new ReflectionClass ( $ editor );
163
+ $ property = $ reflection ->getProperty ( 'image ' );
164
+ $ property ->setAccessible ( true );
165
+ $ property ->setValue ( $ editor , $ imagick );
166
+
167
+ $ result = $ editor ->has_transparency ();
168
+
169
+ $ this ->assertTrue ( $ result );
170
+ }
171
+
172
+ /**
173
+ * @covers ::has_transparency
174
+ */
175
+ public function test_has_transparency_no_alpha_channel_method (): void {
176
+ // Mock the Imagick object to simulate when getImageAlphaChannel method doesn't exist.
177
+ $ imagick_mock = $ this ->getMockBuilder ( Imagick::class )
178
+ ->disableOriginalConstructor ()
179
+ ->getMock ();
180
+
181
+ $ imagick_mock ->method ( 'getImageWidth ' )->willReturn ( 1 );
182
+ $ imagick_mock ->method ( 'getImageHeight ' )->willReturn ( 1 );
183
+
184
+ $ pixel = $ this ->getMockBuilder ( ImagickPixel::class )
185
+ ->disableOriginalConstructor ()
186
+ ->getMock ();
187
+ $ pixel ->method ( 'getColor ' )->willReturn ( array ( 'a ' => 0 ) );
188
+
189
+ $ imagick_mock ->method ( 'getImagePixelColor ' )->willReturn ( $ pixel );
190
+
191
+ $ editor = new Dominant_Color_Image_Editor_Imagick ( null );
192
+ $ reflection = new ReflectionClass ( $ editor );
193
+ $ property = $ reflection ->getProperty ( 'image ' );
194
+ $ property ->setAccessible ( true );
195
+ $ property ->setValue ( $ editor , $ imagick_mock );
196
+
197
+ $ result = $ editor ->has_transparency ();
198
+
199
+ $ this ->assertFalse ( $ result );
200
+ }
36
201
}
0 commit comments