PluginProbe
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance / 3.2.5
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance v3.2.5
4.7.0 4.6.1 4.6.0 4.5.5 4.5.4 4.5.3 4.5.2 3.2.20 3.2.21 3.2.22 3.2.3 3.2.5 3.2.6 3.2.7 3.2.9 3.3.0 3.3.1 3.3.2 3.4.0 3.4.1 3.4.2 3.5.0 3.6.0 3.7.0 3.7.1 All 111 releases
wp-optimize / vendor / rosell-dk / webp-convert / tests / Convert / Converters / WPCTest.php

WPCTest.php in WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance 3.2.5, at vendor/rosell-dk/webp-convert/tests/Convert/Converters/WPCTest.php

298 lines 8.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * WebPConvert - Convert JPEG & PNG to WebP with PHP
4 *
5 * @link https://github.com/rosell-dk/webp-convert
6 * @license MIT
7 */
8
9 namespace WebPConvert\Tests\Convert\Converters;
10
11 use WebPConvert\Convert\Converters\Wpc;
12 use WebPConvert\Convert\Exceptions\ConversionFailedException;
13 use WebPConvert\Convert\Exceptions\ConversionFailed\ConverterNotOperationalException;
14 use WebPConvert\Convert\Exceptions\ConversionFailed\ConverterNotOperational\SystemRequirementsNotMetException;
15 use WebPConvert\Convert\Exceptions\ConversionFailed\ConverterNotOperational\InvalidApiKeyException;
16 use WebPConvert\Loggers\BufferLogger;
17
18 use WebPConvert\Tests\CompatibleTestCase;
19
20 use PHPUnit\Framework\TestCase;
21
22 /**
23 * @coversDefaultClass WebPConvert\Convert\Converters\Wpc
24 * @covers WebPConvert\Convert\Converters\Wpc
25 */
26 class WPCTest extends CompatibleTestCase
27 {
28
29 public function getImageFolder()
30 {
31 return realpath(__DIR__ . '/../../images');
32 }
33
34 public function getImagePath($image)
35 {
36 return $this->getImageFolder() . '/' . $image;
37 }
38
39 /* public function testApi0()
40 {
41 if (!empty(getenv('WEBPCONVERT_WPC_API_URL_API0'))) {
42 $source = $this->imageDir . '/test.png';
43 Wpc::convert($source, $source . '.webp', [
44 'api-version' => 0,
45 'api-url' => getenv('WEBPCONVERT_WPC_API_URL_API0')
46 ]);
47 }
48 }
49 */
50
51 private static function tryThis($test, $source, $options)
52 {
53 $bufferLogger = new BufferLogger();
54
55 try {
56 Wpc::convert($source, $source . '.webp', $options, $bufferLogger);
57 } catch (ConversionFailedException $e) {
58
59 // we accept this failure that seems to happen when WPC gets stressed:
60 if (strpos($e->getMessage(), 'unable to open image') !== false) {
61 return true;
62 }
63
64 // we also accept this failure that also seems to happen when WPC gets stressed:
65 if (strpos($e->getMessage(), 'We got nothing back') !== false) {
66 return true;
67 }
68
69 if ($e->getMessage() == 'Error saving file. Check file permissions') {
70 throw new ConversionFailedException(
71 'Failed saving file. Here is the log:' . $bufferLogger->getText()
72 );
73 }
74
75 throw $e;
76 }
77 return true;
78 }
79
80 public function testWarnIfNotOperational()
81 {
82 ConverterTestHelper::warnIfNotOperational('Wpc');
83 $this->addToAssertionCount(1);
84 }
85
86 public function testApi0()
87 {
88 if (empty(getenv('WEBPCONVERT_WPC_API_URL'))) {
89 echo "\n" . 'NOTICE: WPC is not operational. It needs api-key and api-url. ';
90 echo 'You can set this up by setting environment varibles WEBPCONVERT_WPC_API_URL_API and WEBPCONVERT_WPC_API_KEY. ';
91 echo 'To also test old api=0, use WEBPCONVERT_WPC_API_URL_API0.';
92 echo "\n";
93 } else {
94 if (empty(getenv('WEBPCONVERT_WPC_API_URL_API0'))) {
95 echo "\n" . 'NOTICE: WPC is not tested with api-version=0. To test this, you must set environment varibles WEBPCONVERT_WPC_API_URL_API0 and WEBPCONVERT_WPC_API_KEY' . "\n";
96 }
97 }
98 if (empty(getenv('WEBPCONVERT_WPC_API_URL_API0'))) {
99 $this->addToAssertionCount(1);
100 return;
101 }
102
103 $source = $this->getImagePath('test.png');
104 $options = [
105 'api-version' => 0,
106 'api-url' => getenv('WEBPCONVERT_WPC_API_URL_API0'),
107 'lossless' => true,
108 ];
109
110 $this->assertTrue(self::tryThis($this, $source, $options));
111
112
113 }
114
115 public function testApi1()
116 {
117 if (empty(getenv('WEBPCONVERT_WPC_API_URL'))) {
118 $this->addToAssertionCount(1);
119 return;
120 }
121
122 $source = $this->getImagePath('test.png');
123 $options = [
124 'api-version' => 1,
125 'crypt-api-key-in-transfer' => true,
126 'lossless' => true,
127 ];
128
129 $this->assertTrue(self::tryThis($this, $source, $options));
130 }
131
132 public function testApi2()
133 {
134 if (empty(getenv('WEBPCONVERT_WPC_API_URL'))) {
135 $this->addToAssertionCount(1);
136 return;
137 }
138
139 $source = $this->getImagePath('test.png');
140 $options = [
141 'api-version' => 2,
142 'crypt-api-key-in-transfer' => true,
143 'lossless' => true,
144 ];
145
146 $this->assertTrue(self::tryThis($this, $source, $options));
147 }
148
149 public function testWrongSecretButRightUrl()
150 {
151 if (empty(getenv('WEBPCONVERT_WPC_API_URL'))) {
152 return;
153 }
154
155 $source = $this->getImagePath('test.png');
156 $options = [
157 'api-version' => 1,
158 'crypt-api-key-in-transfer' => true,
159 'api-key' => 'wrong!',
160 ];
161
162 $this->expectException(InvalidApiKeyException::class);
163 $this->assertTrue(self::tryThis($this, $source, $options));
164 }
165
166 public function testBadURL()
167 {
168 $this->expectException(ConverterNotOperationalException::class);
169
170 Wpc::convert(
171 $this->getImagePath('test.png'),
172 $this->getImagePath('test.webp'),
173 [
174 'api-url' => 'badurl!',
175 'secret' => 'bad dog!',
176 ]
177 );
178 }
179
180 public function test404()
181 {
182 //$this->expectException(ConversionFailedException::class);
183
184 try {
185 Wpc::convert(
186 $this->getImagePath('test.png'),
187 $this->getImagePath('test.webp'),
188 [
189 'api-url' => 'https://google.com/hello',
190 'secret' => 'bad dog!',
191 ]
192 );
193 $this->fail('Expected an exception');
194
195 } catch (ConversionFailedException $e) {
196 // this is expected!
197 $this->addToAssertionCount(1);
198
199 $this->assertMatchesRegularExpression2('#we got a 404 response#', $e->getMessage());
200 }
201
202 }
203
204 public function testUnexpectedResponse()
205 {
206 //$this->expectException(ConversionFailedException::class);
207
208 try {
209 Wpc::convert(
210 $this->getImagePath('test.png'),
211 $this->getImagePath('test.webp'),
212 [
213 'api-url' => 'https://www.google.com/',
214 'secret' => 'bad dog!',
215 ]
216 );
217 $this->fail('Expected an exception');
218
219 } catch (ConversionFailedException $e) {
220 // this is expected!
221 $this->addToAssertionCount(1);
222
223 $this->assertMatchesRegularExpression2('#We did not receive an image#', $e->getMessage());
224 }
225 }
226
227
228 /*
229 HMM.. Apparently wpc can't handle much stress.
230 The runAllConvertTests often results in an error like this:
231
232 'WPC failed converting image: "unable to open image '../conversions/80c80b20834edd62456fe9e6da4d24d64be51dc1.jpg': No such file or directory @ error/blob.c/OpenBlob/3489"'
233
234 public function testApi0()
235 {
236 if (!empty(getenv('WEBPCONVERT_WPC_API_URL_API0'))) {
237 ConverterTestHelper::runAllConvertTests($this, 'Wpc', [
238 'api-version' => 0,
239 'api-url' => getenv('WEBPCONVERT_WPC_API_URL_API0')
240 ]);
241 }
242 }
243
244 public function testApi1()
245 {
246 if (empty(getenv('WEBPCONVERT_WPC_API_URL')) || empty(getenv('WEBPCONVERT_WPC_API_KEY'))) {
247 return;
248 }
249
250 ConverterTestHelper::runAllConvertTests($this, 'Wpc', [
251 'api-version' => 1,
252 'crypt-api-key-in-transfer' => true
253 ]);
254
255 // TODO: Also test without crypt
256 }
257 */
258
259 /*
260 public function testMissingURL()
261 {
262 $this->expectException(ConverterNotOperationalException::class);
263
264 Wpc::convert($this->imageDir . '/test.png', $this->imageDir . '/test.webp', [
265 'api-url' => '',
266 'secret' => 'bad dog!',
267 ]);
268 }*/
269
270
271 /*
272 public function testWrongSecretButRightUrl()
273 {
274 if (empty(getenv('WEBPCONVERT_WPC_API_URL'))) {
275 return;
276 }
277
278 $this->expectException(InvalidApiKeyException::class);
279
280 Wpc::convert($this->imageDir . '/test.png', $this->imageDir . '/test.webp', [
281 'api-version' => 0,
282 'api-url' => getenv('WEBPCONVERT_WPC_API_URL'),
283 'secret' => 'purposely-wrong-secret!'
284 ]);
285 }
286
287 public function testBadURL()
288 {
289 $this->expectException(ConverterNotOperationalException::class);
290
291 Wpc::convert($this->imageDir . '/test.png', $this->imageDir . '/test.webp', [
292 'api-url' => 'badurl!',
293 'secret' => 'bad dog!',
294 ]);
295 }*/
296
297 }
298