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 / CwebpTest.php

CwebpTest.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/CwebpTest.php

323 lines 10.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * WebPConvert - Convert JPEG & PNG to WebP with PHP
5 *
6 * @link https://github.com/rosell-dk/webp-convert
7 * @license MIT
8 */
9
10 namespace WebPConvert\Tests\Convert\Converters;
11
12 use WebPConvert\Convert\Converters\Cwebp;
13 use WebPConvert\Convert\Exceptions\ConversionFailedException;
14 use WebPConvert\Convert\Exceptions\ConversionFailed\ConverterNotOperationalException;
15
16 use WebPConvert\Tests\CompatibleTestCase;
17 use WebPConvert\Tests\Convert\Exposers\CwebpExposer;
18
19 use PHPUnit\Framework\TestCase;
20
21 /**
22 * @coversDefaultClass WebPConvert\Convert\Converters\Cwebp
23 * @covers WebPConvert\Convert\Converters\Cwebp
24 */
25 class CwebpTest extends CompatibleTestCase
26 {
27
28 public static function getImageFolder()
29 {
30 return realpath(__DIR__ . '/../../images');
31 }
32
33 public static function getImagePath($image)
34 {
35 return self::getImageFolder() . '/' . $image;
36 }
37
38 public function testConvert()
39 {
40 ConverterTestHelper::runAllConvertTests($this, 'Cwebp');
41 }
42
43 public function testSource()
44 {
45 $source = self::getImagePath('test.png');
46 $cwebp = new Cwebp($source, $source . '.webp');
47 $cwebpExposer = new CwebpExposer($cwebp);
48
49 $this->assertEquals($source, $cwebpExposer->getSource());
50 $this->assertTrue(file_exists($source), 'source does not exist');
51 }
52
53
54 /**
55 * @covers ::createCommandLineOptions
56 */
57 public function testCreateCommandLineOptions()
58 {
59 $source = self::getImagePath('test.png');
60 $options = [
61 'quality' => 'auto',
62 'method' => 3,
63 'command-line-options' => '-sharpness 5 -crop 10 10 40 40 -low_memory',
64 ];
65 $cwebp = new Cwebp($source, $source . '.webp', $options);
66 $cwebpExposer = new CwebpExposer($cwebp);
67
68 //$cwebpExposer->prepareOptions();
69
70 $commandLineOptions = $cwebpExposer->createCommandLineOptions();
71 //$this->assertEquals('e', $commandLineOption); // use this to quickly see it...
72
73 // Per default we have no preset set
74 $this->assertDoesNotMatchRegularExpression2('#-preset#', $commandLineOptions);
75
76 // Metadata is per default none
77 $this->assertMatchesRegularExpression2('#-metadata none#', $commandLineOptions);
78
79 // We passed the method option and set it to 3
80 $this->assertMatchesRegularExpression2('#-m 3#', $commandLineOptions);
81
82 // There must be an output option, and it must be quoted
83 $this->assertMatchesRegularExpression2('#-o [\'"]#', $commandLineOptions);
84
85 // There must be a quality option, and it must be digits
86 $this->assertMatchesRegularExpression2('#-q \\d+#', $commandLineOptions);
87
88 // -sharpness '5'
89 $this->assertMatchesRegularExpression2('#-sharpness [\'"]5[\'"]#', $commandLineOptions);
90
91 // Extra command line option with multiple values. Each are escapeshellarg'ed
92 $this->assertMatchesRegularExpression2(
93 '#-crop [\'"]10[\'"] [\'"]10[\'"] [\'"]40[\'"] [\'"]40[\'"]#',
94 $commandLineOptions
95 );
96
97 // Command line option (flag)
98 $this->assertMatchesRegularExpression2('#-low_memory#', $commandLineOptions);
99
100 // -sharpness '5'
101 $this->assertMatchesRegularExpression2('#-sharpness [\'"]5[\'"]#', $commandLineOptions);
102 }
103
104 /**
105 * @covers ::createCommandLineOptions
106 */
107 public function testCreateCommandLineOptions2()
108 {
109 $source = self::getImagePath('test.png');
110 $options = [
111 'quality' => 70,
112 'method' => 3,
113 'size-in-percentage' => 55,
114 'preset' => 'picture'
115 ];
116 $cwebp = new Cwebp($source, $source . '.webp', $options);
117 $cwebpExposer = new CwebpExposer($cwebp);
118
119 //$cwebpExposer->prepareOptions();
120
121 $commandLineOptions = $cwebpExposer->createCommandLineOptions();
122
123 // Preset
124 // Note that escapeshellarg uses doublequotes on Windows
125 $this->assertMatchesRegularExpression2("#-preset ['\"]picture['\"]#", $commandLineOptions);
126
127 // Size
128 $fileSizeInBytes = floor($options['size-in-percentage']/100 * filesize($source));
129 $this->assertEquals(1714, $fileSizeInBytes);
130 $this->assertMatchesRegularExpression2('#-size ' . $fileSizeInBytes . '#', $commandLineOptions);
131
132 // There must be no quality option, because -size overrules it.
133 $this->assertDoesNotMatchRegularExpression2('#-q \\d+#', $commandLineOptions);
134 }
135
136 /**
137 * @covers ::createCommandLineOptions
138 */
139 public function testCreateCommandLineOptions3()
140 {
141 $source = self::getImagePath('test.png');
142 $options = [
143 'encoding' => 'lossless',
144 'near-lossless' => 75,
145 'auto-filter' => true,
146 ];
147 $cwebp = new Cwebp($source, $source . '.webp', $options);
148 $cwebpExposer = new CwebpExposer($cwebp);
149
150 $commandLineOptions = $cwebpExposer->createCommandLineOptions();
151
152 // near-lossless
153 $this->assertMatchesRegularExpression2('#-near_lossless 75#', $commandLineOptions);
154
155 // There must be no -lossless option, because -near-lossless overrules it.
156 $this->assertDoesNotMatchRegularExpression2('#-lossless#', $commandLineOptions);
157
158 // auto-filter
159 $this->assertMatchesRegularExpression2('#-af#', $commandLineOptions);
160
161 // no low-memory
162 $this->assertDoesNotMatchRegularExpression2('#-low_memory#', $commandLineOptions);
163 }
164
165 /**
166 * @covers ::createCommandLineOptions
167 */
168 public function testCreateCommandLineOptions4()
169 {
170 $source = self::getImagePath('test.png');
171 $options = [
172 'encoding' => 'lossless',
173 'near-lossless' => 100,
174 'low-memory' => true,
175 ];
176 $cwebp = new Cwebp($source, $source . '.webp', $options);
177 $cwebpExposer = new CwebpExposer($cwebp);
178
179 $commandLineOptions = $cwebpExposer->createCommandLineOptions();
180
181 // lossless
182 $this->assertMatchesRegularExpression2('#-lossless#', $commandLineOptions);
183
184 // There must be no -near_lossless option, because -lossless overrules it.
185 $this->assertDoesNotMatchRegularExpression2('#-near_lossless#', $commandLineOptions);
186
187 // low-memory
188 $this->assertMatchesRegularExpression2('#-low_memory#', $commandLineOptions);
189
190 // No auto-filter
191 $this->assertDoesNotMatchRegularExpression2('#-af#', $commandLineOptions);
192 }
193
194 /**
195 * @covers ::checkOperationality
196 */
197 public function testOperatinalityException()
198 {
199 $source = self::getImagePath('test.png');
200 $options = [
201 'try-cwebp' => false,
202 'try-supplied-binary-for-os' => false,
203 'try-common-system-paths' => false,
204 'try-discovering-cwebp' => false,
205 ];
206 $this->expectException(ConverterNotOperationalException::class);
207 //$cwebp = new Cwebp($source, $source . '.webp', $options);
208 Cwebp::convert($source, $source . '.webp', $options);
209 }
210
211 public function testUsingSuppliedBinaryForOS()
212 {
213 $source = self::getImagePath('test.png');
214 $options = [
215 'try-cwebp' => false,
216 'try-supplied-binary-for-os' => true,
217 'try-common-system-paths' => false,
218 'try-discovering-cwebp' => false,
219 ];
220 //$this->expectException(ConverterNotOperationalException::class);
221 //$cwebp = new Cwebp($source, $source . '.webp', $options);
222 try {
223 Cwebp::convert($source, $source . '.webp', $options);
224 } catch (ConversionFailedException $e) {
225 // this is ok.
226 // - but other exceptions are not!
227 }
228 $this->addToAssertionCount(1);
229 }
230
231 public function testUsingCommonSystemPaths()
232 {
233 $source = self::getImagePath('test.png');
234 $options = [
235 'try-cwebp' => false,
236 'try-supplied-binary-for-os' => false,
237 'try-common-system-paths' => true,
238 'try-discovering-cwebp' => false,
239 ];
240 //$this->expectException(ConverterNotOperationalException::class);
241 //$cwebp = new Cwebp($source, $source . '.webp', $options);
242 try {
243 Cwebp::convert($source, $source . '.webp', $options);
244 } catch (ConversionFailedException $e) {
245 // this is ok.
246 // - but other exceptions are not!
247 }
248 $this->addToAssertionCount(1);
249
250 }
251
252 /*
253 public function testCwebpDefaultPaths()
254 {
255 $default = [
256 '/usr/bin/cwebp',
257 '/usr/local/bin/cwebp',
258 '/usr/gnu/bin/cwebp',
259 '/usr/syno/bin/cwebp'
260 ];
261
262 foreach ($default as $key) {
263 $this->assertContains($key, Cwebp::$cwebpDefaultPaths);
264 }
265 }*/
266
267 /**
268 * @expectedException \Exception
269 */
270 /*
271 public function testUpdateBinariesInvalidFile()
272 {
273 $array = [];
274
275 Cwebp::updateBinaries('InvalidFile', 'Hash', $array);
276 }*/
277
278 /**
279 * @expectedException \Exception
280 */
281 /*
282 public function testUpdateBinariesInvalidHash()
283 {
284 $array = [];
285
286 Cwebp::updateBinaries('cwebp-linux', 'InvalidHash', $array);
287 }
288
289 public function testUpdateBinaries()
290 {
291 $file = 'cwebp.exe';
292 $filePath = realpath(__DIR__ . '/../../Converters/Binaries/' . $file);
293 $hash = hash_file('sha256', $filePath);
294 $array = [];
295
296 $this->assertContains($filePath, Cwebp::updateBinaries($file, $hash, $array));
297 }
298
299 public function testEscapeFilename()
300 {
301 $wrong = '/path/to/file Na<>me."ext"';
302 $right = '/path/to/file\\\ Name.\&#34;ext\&#34;';
303
304 $this->assertEquals($right, Cwebp::escapeFilename($wrong));
305 }
306
307 public function testHasNiceSupport()
308 {
309 $this->assertNotNull(Cwebp::hasNiceSupport());
310 }*/
311 /*
312 public function testConvert()
313 {
314 $source = realpath(__DIR__ . '/../test.jpg');
315 $destination = realpath(__DIR__ . '/../test.webp');
316 $quality = 85;
317 $stripMetadata = true;
318
319 $this->assertTrue(Cwebp::convert($source, $destination, $quality, $stripMetadata));
320 }*/
321
322 }
323