|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Utility - Collection of various PHP utility functions. |
|
7
|
|
|
* |
|
8
|
|
|
* @author Eric Sizemore <[email protected]> |
|
9
|
|
|
* |
|
10
|
|
|
* @version 2.0.0 |
|
11
|
|
|
* |
|
12
|
|
|
* @copyright (C) 2017 - 2024 Eric Sizemore |
|
13
|
|
|
* @license The MIT License (MIT) |
|
14
|
|
|
* |
|
15
|
|
|
* Copyright (C) 2017 - 2024 Eric Sizemore <https://www.secondversion.com>. |
|
16
|
|
|
* |
|
17
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
|
18
|
|
|
* of this software and associated documentation files (the "Software"), to |
|
19
|
|
|
* deal in the Software without restriction, including without limitation the |
|
20
|
|
|
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
|
21
|
|
|
* sell copies of the Software, and to permit persons to whom the Software is |
|
22
|
|
|
* furnished to do so, subject to the following conditions: |
|
23
|
|
|
* |
|
24
|
|
|
* The above copyright notice and this permission notice shall be included in |
|
25
|
|
|
* all copies or substantial portions of the Software. |
|
26
|
|
|
* |
|
27
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
28
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
29
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
30
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
31
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
32
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|
33
|
|
|
* THE SOFTWARE. |
|
34
|
|
|
*/ |
|
35
|
|
|
|
|
36
|
|
|
namespace Esi\Utility\Tests; |
|
37
|
|
|
|
|
38
|
|
|
use Esi\Utility\Image; |
|
39
|
|
|
use InvalidArgumentException; |
|
40
|
|
|
use PHPUnit\Framework\Attributes\CoversClass; |
|
41
|
|
|
use PHPUnit\Framework\TestCase; |
|
42
|
|
|
use RuntimeException; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Image utility test. |
|
46
|
|
|
* |
|
47
|
|
|
* @internal |
|
48
|
|
|
*/ |
|
49
|
|
|
#[CoversClass(Image::class)] |
|
50
|
|
|
class ImageTest extends TestCase |
|
51
|
|
|
{ |
|
52
|
|
|
protected string $resourceDir; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @var array<string> |
|
56
|
|
|
*/ |
|
57
|
|
|
protected array $resources; |
|
58
|
|
|
|
|
59
|
|
|
#[\Override] |
|
60
|
|
|
protected function setUp(): void |
|
61
|
|
|
{ |
|
62
|
|
|
$this->resourceDir = dirname(__FILE__, 2) . DIRECTORY_SEPARATOR . 'resources/'; |
|
63
|
|
|
|
|
64
|
|
|
$this->resources = [ |
|
65
|
|
|
'image/jpeg' => $this->resourceDir . 'testImage.jpg', |
|
66
|
|
|
'image/png' => $this->resourceDir . 'testImage.png', |
|
67
|
|
|
'image/gif' => $this->resourceDir . 'testImage.gif', |
|
68
|
|
|
'image/webp' => $this->resourceDir . 'testImage.webp', |
|
69
|
|
|
]; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
public function testGuessImageType(): void |
|
73
|
|
|
{ |
|
74
|
|
|
foreach ($this->resources as $key => $val) { |
|
75
|
|
|
$result = Image::guessImageType($val); |
|
76
|
|
|
|
|
77
|
|
|
self::assertIsString($result); |
|
78
|
|
|
self::assertSame($key, $result); |
|
79
|
|
|
|
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
public function testGuessImageTypeInvalidFile(): void |
|
84
|
|
|
{ |
|
85
|
|
|
$this->expectException(InvalidArgumentException::class); |
|
86
|
|
|
Image::guessImageType(''); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
public function testIsJpg(): void |
|
90
|
|
|
{ |
|
91
|
|
|
self::assertTrue(Image::isJpg($this->resources['image/jpeg'])); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
public function testIsJpgInvalidFile(): void |
|
95
|
|
|
{ |
|
96
|
|
|
$this->expectException(InvalidArgumentException::class); |
|
97
|
|
|
Image::isJpg($this->resourceDir . 'doesNotExist.jpg'); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
public function testIsJpgInvalidImageFile(): void |
|
101
|
|
|
{ |
|
102
|
|
|
$this->expectException(RuntimeException::class); |
|
103
|
|
|
Image::isJpg($this->resourceDir . 'notAnImage.txt'); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
public function testIsGif(): void |
|
107
|
|
|
{ |
|
108
|
|
|
self::assertTrue(Image::isGif($this->resources['image/gif'])); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
public function testIsGifInvalidFile(): void |
|
112
|
|
|
{ |
|
113
|
|
|
$this->expectException(InvalidArgumentException::class); |
|
114
|
|
|
Image::isGif($this->resourceDir . 'doesNotExist.gif'); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
public function testIsGifInvalidImageFile(): void |
|
118
|
|
|
{ |
|
119
|
|
|
$this->expectException(RuntimeException::class); |
|
120
|
|
|
Image::isGif($this->resourceDir . 'notAnImage.txt'); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
public function testIsPng(): void |
|
124
|
|
|
{ |
|
125
|
|
|
self::assertTrue(Image::isPng($this->resources['image/png'])); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
public function testIsPngInvalidImageFile(): void |
|
129
|
|
|
{ |
|
130
|
|
|
$this->expectException(RuntimeException::class); |
|
131
|
|
|
Image::isPng($this->resourceDir . 'notAnImage.txt'); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
public function testIsPngInvalidFile(): void |
|
135
|
|
|
{ |
|
136
|
|
|
$this->expectException(InvalidArgumentException::class); |
|
137
|
|
|
Image::isPng($this->resourceDir . 'doesNotExist.png'); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
public function testIsWebp(): void |
|
141
|
|
|
{ |
|
142
|
|
|
self::assertTrue(Image::isWebp($this->resources['image/webp'])); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
public function testIsWebpInvalidImageFile(): void |
|
146
|
|
|
{ |
|
147
|
|
|
$this->expectException(RuntimeException::class); |
|
148
|
|
|
Image::isWebp($this->resourceDir . 'notAnImage.txt'); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
public function testIsWebpInvalidFile(): void |
|
152
|
|
|
{ |
|
153
|
|
|
$this->expectException(InvalidArgumentException::class); |
|
154
|
|
|
Image::isWebp($this->resourceDir . 'doesNotExist.webp'); |
|
155
|
|
|
} |
|
156
|
|
|
} |
|
157
|
|
|
|