|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ImageOptimizer\Tests; |
|
4
|
|
|
|
|
5
|
|
|
use Mockery\Adapter\Phpunit\MockeryTestCase; |
|
6
|
|
|
use ImageOptimizer\{ImageOptimizer, ImagineDriver}; |
|
7
|
|
|
use Imagine\Gd\Imagine; |
|
8
|
|
|
use Mockery; |
|
9
|
|
|
use ImageOptimizer\Exception\{DirectoryNotFoundException, ImageNotFoundException}; |
|
10
|
|
|
|
|
11
|
|
|
class ImageOptimizerTest extends MockeryTestCase |
|
12
|
|
|
{ |
|
13
|
|
|
protected $resizedFolder; |
|
14
|
|
|
|
|
15
|
|
|
protected $appPath; |
|
16
|
|
|
|
|
17
|
|
|
public function setUp(): void |
|
18
|
|
|
{ |
|
19
|
|
|
$this->resizedFolder = '/resized_images'; |
|
20
|
|
|
$this->appPath = '/var/www/html'; |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @test |
|
25
|
|
|
*/ |
|
26
|
|
|
public function exception_is_thrown_if_file_not_exists() |
|
27
|
|
|
{ |
|
28
|
|
|
$driver = Mockery::mock(ImagineDriver::class); |
|
29
|
|
|
|
|
30
|
|
|
$service = $this->getService($driver); |
|
31
|
|
|
|
|
32
|
|
|
$this->fakeFileCheck($service, ['/var/www/html/image.png' => false]); |
|
33
|
|
|
|
|
34
|
|
|
$this->expectException(ImageNotFoundException::class); |
|
35
|
|
|
|
|
36
|
|
|
$service->getOptimizedImage('/image.png', 150, 100); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @test |
|
41
|
|
|
*/ |
|
42
|
|
|
public function exception_is_thrown_if_folder_cannot_be_created() |
|
43
|
|
|
{ |
|
44
|
|
|
$driver = Mockery::mock(ImagineDriver::class); |
|
45
|
|
|
|
|
46
|
|
|
$service = $this->getService($driver); |
|
47
|
|
|
|
|
48
|
|
|
$this->fakeFileCheck($service, [ |
|
49
|
|
|
'/var/www/html/image.png' => true, |
|
50
|
|
|
'/var/www/html/resized_images/image_150x100.png' => false, |
|
51
|
|
|
'/var/www/html/resized_images' => false, |
|
52
|
|
|
]); |
|
53
|
|
|
|
|
54
|
|
|
$service->shouldReceive('mkdir') |
|
55
|
|
|
->with('/var/www/html/resized_images') |
|
56
|
|
|
->andReturn(false); |
|
57
|
|
|
|
|
58
|
|
|
$this->expectException(DirectoryNotFoundException::class); |
|
59
|
|
|
|
|
60
|
|
|
$service->getOptimizedImage('/image.png', 150, 100); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @test |
|
65
|
|
|
*/ |
|
66
|
|
|
public function driver_gets_called() |
|
67
|
|
|
{ |
|
68
|
|
|
$driver = Mockery::mock(ImagineDriver::class); |
|
69
|
|
|
|
|
70
|
|
|
$service = $this->getService($driver); |
|
71
|
|
|
|
|
72
|
|
|
$this->fakeFileCheck($service, [ |
|
73
|
|
|
'/var/www/html/image.png' => true, |
|
74
|
|
|
'/var/www/html/resized_images/image_150x100.png' => false, |
|
75
|
|
|
'/var/www/html/resized_images' => false, |
|
76
|
|
|
]); |
|
77
|
|
|
|
|
78
|
|
|
$service->shouldReceive('mkdir') |
|
79
|
|
|
->with('/var/www/html/resized_images') |
|
80
|
|
|
->andReturn(true); |
|
81
|
|
|
|
|
82
|
|
|
$driver->shouldReceive('process')->once(); |
|
83
|
|
|
|
|
84
|
|
|
$image = $service->getOptimizedImage('/image.png', 150, 100); |
|
85
|
|
|
|
|
86
|
|
|
$this->assertEquals('/image.png', $image->originalUrl); |
|
87
|
|
|
$this->assertEquals('/resized_images/image_150x100.png', $image->resizedUrl); |
|
88
|
|
|
$this->assertEquals('/resized_images/image_150x100.png.webp', $image->resizedWebpUrl); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @test |
|
93
|
|
|
*/ |
|
94
|
|
|
public function driver_is_not_called_if_file_exists() |
|
95
|
|
|
{ |
|
96
|
|
|
$driver = Mockery::mock(ImagineDriver::class); |
|
97
|
|
|
|
|
98
|
|
|
$service = $this->getService($driver); |
|
99
|
|
|
|
|
100
|
|
|
$this->fakeFileCheck($service, [ |
|
101
|
|
|
'/var/www/html/image.png' => true, |
|
102
|
|
|
'/var/www/html/resized_images/image_150x100.png' => true, |
|
103
|
|
|
]); |
|
104
|
|
|
|
|
105
|
|
|
$service->shouldNotReceive('mkdir'); |
|
106
|
|
|
|
|
107
|
|
|
$driver->shouldNotReceive('process'); |
|
108
|
|
|
|
|
109
|
|
|
$image = $service->getOptimizedImage('/image.png', 150, 100); |
|
110
|
|
|
|
|
111
|
|
|
$this->assertEquals('/image.png', $image->originalUrl); |
|
112
|
|
|
$this->assertEquals('/resized_images/image_150x100.png', $image->resizedUrl); |
|
113
|
|
|
$this->assertEquals('/resized_images/image_150x100.png.webp', $image->resizedWebpUrl); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
protected function getService($driver) |
|
117
|
|
|
{ |
|
118
|
|
|
return Mockery::mock(ImageOptimizer::class, [$driver, $this->resizedFolder, $this->appPath]) |
|
119
|
|
|
->makePartial() |
|
120
|
|
|
->shouldAllowMockingProtectedMethods(); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
protected function fakeFileCheck($service, array $expectations): void |
|
124
|
|
|
{ |
|
125
|
|
|
foreach ($expectations as $file => $return) { |
|
126
|
|
|
$service->shouldReceive('fileExists') |
|
127
|
|
|
->with($file) |
|
128
|
|
|
->andReturn($return); |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
|