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

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

249 lines 7.3 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;
11
12 use WebPConvert\WebPConvert;
13 use WebPConvert\Convert\Exceptions\ConversionFailed\ConverterNotOperationalException;
14 //use WebPConvert\Convert\Exceptions\ConversionFailed\InvalidInput\TargetNotFoundException;
15 use WebPConvert\Exceptions\InvalidInput\TargetNotFoundException;
16 use WebPConvert\Convert\Exceptions\ConversionFailed\FileSystemProblems\CreateDestinationFolderException;
17
18 use PHPUnit\Framework\TestCase;
19
20 class WebPConvertTest extends TestCase
21 {
22 /*
23 public function testPreferredConverters()
24 {
25 $this->assertEmpty(WebPConvert::$preferredConverters);
26 }
27
28 public function testExcludeDefaultBinaries()
29 {
30 $this->assertFalse(WebPConvert::$excludeDefaultBinaries);
31 }
32
33 public function testAllowedExtensions()
34 {
35 $allowed = ['jpg', 'jpeg', 'png'];
36
37 foreach ($allowed as $key) {
38 $this->assertContains($key, WebPConvert::$allowedExtensions);
39 }
40 }
41
42 public function testSetConverters()
43 {
44 $preferred = ['gd', 'cwebp'];
45 WebPConvert::setConverters($preferred);
46
47 $this->assertEquals($preferred, WebPConvert::$preferredConverters);
48 }
49 */
50 /**
51 * @expectedExceptio \Exception
52 */
53 /*
54 public function testIsValidTargetInvalid()
55 {
56 WebPConvert::isValidTarget('Invalid');
57 }
58
59 public function testIsValidTargetValid()
60 {
61 $this->assertTrue(WebPConvert::isValidTarget(__FILE__));
62 }
63 */
64 /**
65 * @expectedExceptio \Exception
66 */
67 /*
68 public function testIsAllowedExtensionInvalid()
69 {
70 $allowed = ['jpg', 'jpeg', 'png'];
71
72 foreach ($allowed as $key) {
73 WebPConvert::isAllowedExtension(__FILE__);
74 }
75 }
76
77 public function testIsAllowedExtensionValid()
78 {
79 $source = (__DIR__ . '/test.jpg');
80
81 $this->assertTrue(WebPConvert::isAllowedExtension($source));
82 }
83
84 public function testGetConvertersDefault()
85 {
86 $default = ['cwebp', 'ewww', 'gd', 'imagick'];
87
88 foreach ($default as $key) {
89 $this->assertContains($key, WebPConvert::getConverters());
90 }
91 }
92
93 public function testGetConvertersCustom()
94 {
95 WebPConvert::$preferredConverters = ['gd', 'cwebp'];
96 $custom = ['gd', 'cwebp', 'ewww', 'imagick'];
97
98 $this->assertEquals($custom, WebPConvert::getConverters());
99 }
100
101 public function testSetGetConverters()
102 {
103 $preferred = ['gd', 'cwebp'];
104 WebPConvert::setConverters($preferred, true);
105
106 $this->assertEquals($preferred, WebPConvert::getConverters());
107 }
108 */
109
110 /*
111 Idea: WebPConvert could throw custom exceptions, and we
112 could test these like this:
113 $this->expectException(InvalidArgumentException::class);
114 https://phpunit.readthedocs.io/en/7.1/writing-tests-for-phpunit.html#testing-exceptions
115 */
116
117 /**
118 * Test convert.
119 * - It must either make a successful conversion, or thwrow an exception
120 * - It must return boolean
121 *//*
122 public function testConvert()
123 {
124 $source = (__DIR__ . '/test.jpg');
125 $destination = (__DIR__ . '/test.webp');
126
127 $result = WebPConvert::convert($source, $destination);
128
129 $this->assertTrue(file_exists($destination));
130 $this->assertInternalType('boolean', $result);
131 }*/
132
133 /**
134 * Basically test what happens when no converters are able to do a conversion,
135 * WebPConvert::convert should in that case return false
136 */
137 public function testConvertWithNoConverters()
138 {
139 $this->expectException(ConverterNotOperationalException::class);
140 $source = __DIR__ . '/test.jpg';
141 $destination = __DIR__ . '/test.jpg.webp';
142 $result = WebPConvert::convert($source, $destination, array(
143 'converters' => array()
144 ));
145 //$this->assertFalse($result);
146 }
147
148
149 public function testTargetNotFound()
150 {
151 $this->expectException(TargetNotFoundException::class);
152
153 WebPConvert::convert(__DIR__ . '/i-dont-existno.jpg', __DIR__ . '/i-dont-exist.webp');
154 //$this->assertTrue($result);
155 }
156
157 public function warningHandler($errno, $errstr, $errfile, $errline)
158 {
159 //echo 'warning handler here';
160 //return false;
161 }
162
163 public function testInvalidDestinationFolder()
164 {
165
166 // Notice: mkdir emits a warning on failure.
167 // I have reconfigured php unit to not turn warnings into exceptions (phpunit.xml.dist)
168 // - if I did not do that, the exception would not be CreateDestinationFolderException
169
170 $isWindows = preg_match('/^win/i', PHP_OS);
171 if ($isWindows) {
172 // The test doesnt work on windows:
173 // Failed asserting that exception of type "WebPConvert\Convert\Exceptions\ConversionFailed\FileSystemProblems\CreateDestinationFolderException" is thrown.
174 // Maybe it did not get converted into an exception
175
176 $this->addToAssertionCount(1);
177 return;
178 }
179
180 $this->expectException(CreateDestinationFolderException::class);
181 //$this->expectException(\Exception::class);
182
183
184 // Set error handler in order to suppress warnings.
185 // (we probably get a warning because mkdir() does not have permission to create the dir it is asked to)
186 $handler = set_error_handler(
187 array($this, "warningHandler"),
188 E_WARNING | E_USER_WARNING | E_NOTICE | E_USER_NOTICE | E_USER_ERROR
189 );
190 //echo 'previously defined handler:' . print_r($handler, true);*/
191
192 /*
193 set_error_handler(
194 array($this, "warningHandler"),
195 E_ALL
196 );
197
198 chown();*/
199
200 // I here assume that no system grants write access to their root folder
201 // this is perhaps wrong to assume?
202 $destinationFolder = '/you-can-delete-me/';
203
204 WebPConvert::convert(__DIR__ . '/test.jpg', $destinationFolder . 'you-can-delete-me.webp');
205
206 restore_error_handler();
207 }
208
209 /**
210 * Test ConversionSkippedException by testing Gd.
211 */
212 /*
213 public function testDeclined()
214 {
215 // only try Gd
216 //WebPConvert::setConverterOrder(array('gd'), true);
217
218 // configure Gd not to convert pngs
219 //WebPConvert::setConverterOption('gd', 'convert_pngs', false);
220
221 $source = __DIR__ . '/test.png';
222 $destination = __DIR__ . '/test.png.webp';
223 $options = array(
224 'converters' => array(
225 array(
226 'converter' => 'gd',
227 'options' => array(
228 'skip-pngs' => true,
229 ),
230 ),
231 )
232 );
233 try {
234 WebPConvert::convert($source, $destination, $options);
235 } catch (\WebPConvert\Convert\Exceptions\SystemRequirementsNotMetException $e) {
236 // converter isn't operational, so we cannot make the unit test
237 return;
238 } catch (\WebPConvert\Convert\Exceptions\ConversionFailed\ConversionSkippedException $e) {
239 // Yeah, this is what we want to test.
240
241 $this->expectException(\WebPConvert\Convert\Exceptions\ConversionFailed\ConversionSkippedException::class);
242 WebPConvert::convert($source, $destination, $options);
243 }
244 }*/
245
246
247 // How to test CreateDestinationFileException ?
248 }
249