-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageTest.php
More file actions
354 lines (298 loc) · 10.1 KB
/
ImageTest.php
File metadata and controls
354 lines (298 loc) · 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
<?php
/**
* @author : Jakiboy
* @package : FloatPHP
* @subpackage : Classes Filesystem Component Tests
* @version : 1.5.x
* @copyright : (c) 2018 - 2025 Jihad Sinnaour <me@jihadsinnaour.com>
* @link : https://floatphp.com
* @license : MIT
*
* This file is a part of FloatPHP Framework.
*/
declare(strict_types=1);
namespace FloatPHP\Tests\Classes\Filesystem;
use PHPUnit\Framework\TestCase;
use FloatPHP\Classes\Filesystem\Image;
use FloatPHP\Classes\Filesystem\File;
/**
* Image class tests.
*/
final class ImageTest extends TestCase
{
private static string $testDir;
private static string $testImage;
private static string $outputDir;
public static function setUpBeforeClass() : void
{
self::$testDir = sys_get_temp_dir() . '/floatphp_image_test_' . uniqid();
self::$outputDir = self::$testDir . '/output';
self::$testImage = self::$testDir . '/test.png';
mkdir(self::$testDir, 0777, true);
mkdir(self::$outputDir, 0777, true);
// Create a simple test image
self::createTestImage();
}
public static function tearDownAfterClass() : void
{
self::cleanupTestFiles();
}
/**
* Create a simple test image for testing.
*/
private static function createTestImage() : void
{
if ( !extension_loaded('gd') ) {
self::markTestSkipped('GD extension not available');
return;
}
$image = imagecreatetruecolor(200, 150);
$color = imagecolorallocate($image, 255, 0, 0); // Red
imagefill($image, 0, 0, $color);
imagepng($image, self::$testImage);
imagedestroy($image);
}
/**
* Clean up test files.
*/
private static function cleanupTestFiles() : void
{
if ( is_dir(self::$testDir) ) {
$files = scandir(self::$testDir);
foreach ($files as $file) {
if ( $file !== '.' && $file !== '..' ) {
$path = self::$testDir . '/' . $file;
if ( is_dir($path) ) {
$subFiles = scandir($path);
foreach ($subFiles as $subFile) {
if ( $subFile !== '.' && $subFile !== '..' ) {
unlink($path . '/' . $subFile);
}
}
rmdir($path);
} else {
unlink($path);
}
}
}
rmdir(self::$testDir);
}
}
/**
* Test image MIME validation.
*/
public function testIsMime() : void
{
if ( !extension_loaded('gd') ) {
$this->markTestSkipped('GD extension not available');
}
$this->assertTrue(Image::isMime(self::$testImage));
// Create a non-image file
$textFile = self::$testDir . '/test.txt';
file_put_contents($textFile, 'This is not an image');
$this->assertFalse(Image::isMime($textFile));
}
/**
* Test format support checking.
*/
public function testIsFormatSupported() : void
{
$this->assertTrue(Image::isFormatSupported('image/jpeg'));
$this->assertTrue(Image::isFormatSupported('image/png'));
$this->assertTrue(Image::isFormatSupported('image/gif'));
$this->assertFalse(Image::isFormatSupported('image/tiff'));
$this->assertFalse(Image::isFormatSupported('text/plain'));
}
/**
* Test getting supported formats.
*/
public function testGetSupportedFormats() : void
{
$formats = Image::getSupportedFormats();
$this->assertIsArray($formats);
$this->assertContains('image/jpeg', $formats);
$this->assertContains('image/png', $formats);
$this->assertContains('image/gif', $formats);
}
/**
* Test getting image information.
*/
public function testGetInfo() : void
{
if ( !extension_loaded('gd') ) {
$this->markTestSkipped('GD extension not available');
}
$info = Image::getInfo(self::$testImage);
$this->assertIsArray($info);
$this->assertEquals(200, $info['width']);
$this->assertEquals(150, $info['height']);
$this->assertEquals('image/png', $info['format']);
}
/**
* Test basic image resizing.
*/
public function testResize() : void
{
if ( !extension_loaded('gd') ) {
$this->markTestSkipped('GD extension not available');
}
$output = self::$outputDir . '/resized.png';
$result = Image::resize(self::$testImage, 100, 75, [
'output' => $output
]);
$this->assertTrue($result);
$this->assertTrue(file_exists($output));
$info = Image::getInfo($output);
$this->assertEquals(100, $info['width']);
$this->assertEquals(75, $info['height']);
}
/**
* Test thumbnail creation.
*/
public function testThumbnail() : void
{
if ( !extension_loaded('gd') ) {
$this->markTestSkipped('GD extension not available');
}
$output = self::$outputDir . '/thumbnail.png';
$result = Image::thumbnail(self::$testImage, 50, $output);
$this->assertTrue($result);
$this->assertTrue(file_exists($output));
$info = Image::getInfo($output);
$this->assertEquals(50, $info['width']);
$this->assertEquals(50, $info['height']);
}
/**
* Test image rotation.
*/
public function testRotate() : void
{
if ( !extension_loaded('gd') || !function_exists('imagerotate') ) {
$this->markTestSkipped('GD extension or imagerotate function not available');
}
$output = self::$outputDir . '/rotated.png';
$result = Image::rotate(self::$testImage, 90, $output);
$this->assertTrue($result);
$this->assertTrue(file_exists($output));
$info = Image::getInfo($output);
// After 90-degree rotation, dimensions should be swapped
$this->assertEquals(150, $info['width']);
$this->assertEquals(200, $info['height']);
}
/**
* Test image format conversion.
*/
public function testConvert() : void
{
if ( !extension_loaded('gd') ) {
$this->markTestSkipped('GD extension not available');
}
$output = self::$outputDir . '/converted.jpeg';
$result = Image::convert(self::$testImage, 'jpeg', $output);
$this->assertTrue($result);
$this->assertTrue(file_exists($output));
$info = Image::getInfo($output);
$this->assertEquals('image/jpeg', $info['format']);
}
/**
* Test invalid path handling.
*/
public function testInvalidPath() : void
{
$this->expectException(\InvalidArgumentException::class);
Image::resize('/nonexistent/path/image.jpg', 100, 100);
}
/**
* Test invalid dimensions.
*/
public function testInvalidDimensions() : void
{
if ( !extension_loaded('gd') ) {
$this->markTestSkipped('GD extension not available');
}
$this->expectException(\InvalidArgumentException::class);
Image::resize(self::$testImage, 0, 100);
}
/**
* Test dimensions too large.
*/
public function testDimensionsTooLarge() : void
{
if ( !extension_loaded('gd') ) {
$this->markTestSkipped('GD extension not available');
}
$this->expectException(\InvalidArgumentException::class);
Image::resize(self::$testImage, 20000, 20000);
}
/**
* Test unsupported format conversion.
*/
public function testUnsupportedFormatConversion() : void
{
if ( !extension_loaded('gd') ) {
$this->markTestSkipped('GD extension not available');
}
$this->expectException(\InvalidArgumentException::class);
Image::convert(self::$testImage, 'tiff');
}
/**
* Test resize with crop option.
*/
public function testResizeWithCrop() : void
{
if ( !extension_loaded('gd') ) {
$this->markTestSkipped('GD extension not available');
}
$output = self::$outputDir . '/cropped.png';
$result = Image::resize(self::$testImage, 100, 100, [
'crop' => true,
'output' => $output
]);
$this->assertTrue($result);
$this->assertTrue(file_exists($output));
$info = Image::getInfo($output);
$this->assertEquals(100, $info['width']);
$this->assertEquals(100, $info['height']);
}
/**
* Test resize with maintain aspect ratio.
*/
public function testResizeWithAspectRatio() : void
{
if ( !extension_loaded('gd') ) {
$this->markTestSkipped('GD extension not available');
}
$output = self::$outputDir . '/aspect.png';
$result = Image::resize(self::$testImage, 100, 100, [
'maintain_aspect' => true,
'output' => $output
]);
$this->assertTrue($result);
$this->assertTrue(file_exists($output));
$info = Image::getInfo($output);
// Should maintain aspect ratio, so not exactly 100x100
$this->assertTrue($info['width'] <= 100);
$this->assertTrue($info['height'] <= 100);
}
/**
* Test resize without upscaling.
*/
public function testResizeWithoutUpscaling() : void
{
if ( !extension_loaded('gd') ) {
$this->markTestSkipped('GD extension not available');
}
$output = self::$outputDir . '/no_upscale.png';
// Try to resize to larger dimensions with upscale disabled
$result = Image::resize(self::$testImage, 400, 300, [
'upscale' => false,
'output' => $output
]);
$this->assertTrue($result);
$this->assertTrue(file_exists($output));
$info = Image::getInfo($output);
// Should keep original dimensions since upscaling is disabled
$this->assertEquals(200, $info['width']);
$this->assertEquals(150, $info['height']);
}
}