wp-optimize
/
vendor
/
rosell-dk
/
webp-convert
/
tests
/
Convert
/
Converters
/
ConverterTestHelper.php
ConverterTestHelper.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/ConverterTestHelper.php
| 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\ConverterFactory; |
| 13 | use WebPConvert\Convert\Exceptions\ConversionFailedException; |
| 14 | use WebPConvert\Convert\Exceptions\ConversionFailed\ConversionSkippedException; |
| 15 | use WebPConvert\Convert\Exceptions\ConversionFailed\UnhandledException; |
| 16 | use WebPConvert\Convert\Exceptions\ConversionFailed\ConverterNotOperationalException; |
| 17 | use WebPConvert\Convert\Exceptions\ConversionFailed\ConverterNotOperational\SystemRequirementsNotMetException; |
| 18 | use WebPConvert\Convert\Exceptions\ConversionFailed\FileSystemProblems\CreateDestinationFolderException; |
| 19 | //use WebPConvert\Convert\Exceptions\ConversionFailed\InvalidInput\TargetNotFoundException; |
| 20 | use WebPConvert\Exceptions\InvalidInput\TargetNotFoundException; |
| 21 | |
| 22 | class ConverterTestHelper |
| 23 | { |
| 24 | |
| 25 | /* |
| 26 | public function testPNGDeclined() |
| 27 | { |
| 28 | try { |
| 29 | $source = __DIR__ . '/../test.png'; |
| 30 | $destination = __DIR__ . '/../test.png.webp'; |
| 31 | Gd::convert($source, $destination, array( |
| 32 | 'skip-pngs' => true, |
| 33 | )); |
| 34 | $testCase->fail('The conversion should have failed, because PNG should have been skipped'); |
| 35 | |
| 36 | } catch (SystemRequirementsNotMetException $e) { |
| 37 | // System requirements are not met, so could not make the test |
| 38 | return; |
| 39 | } catch (ConversionSkippedException $e) { |
| 40 | // Yeah, this is what we wanted to test. And it went well! |
| 41 | $testCase->assertTrue(true); |
| 42 | } catch (ConversionFailedException $e) { |
| 43 | $testCase->fail("A ConversionFailedException was thrown (and it was not the SystemRequirementsNotMetException)"); |
| 44 | } catch (\Exception $e) { |
| 45 | $testCase->fail("An unexpected exception was thrown"); |
| 46 | } |
| 47 | } |
| 48 | */ |
| 49 | public static function getImageFolder() |
| 50 | { |
| 51 | return realpath(__DIR__ . '/../../images'); |
| 52 | } |
| 53 | |
| 54 | public static function getImagePath($image) |
| 55 | { |
| 56 | return self::getImageFolder() . '/' . $image; |
| 57 | } |
| 58 | |
| 59 | private static function callConvert($converterClassName, $source, $destination, $converterOptions) |
| 60 | { |
| 61 | return call_user_func( |
| 62 | ['WebPConvert\\Convert\\Converters\\' . $converterClassName, 'convert'], |
| 63 | $source, |
| 64 | $destination, |
| 65 | $converterOptions |
| 66 | ); |
| 67 | //$logger |
| 68 | |
| 69 | /* |
| 70 | TODO: Consider using mikey179/vfsStream |
| 71 | https://github.com/mikey179/vfsStream |
| 72 | https://phpunit.de/manual/6.5/en/test-doubles.html#test-doubles.mocking-the-filesystem |
| 73 | */ |
| 74 | } |
| 75 | |
| 76 | public static function testInvalidDestinationFolder($testCase, $converterClassName, $converterOptions) |
| 77 | { |
| 78 | $testCase->expectException(CreateDestinationFolderException::class); |
| 79 | |
| 80 | try { |
| 81 | $source = self::getImagePath('test.jpg'); |
| 82 | $destination = '/you-can-delete-me/'; |
| 83 | $result = self::callConvert($converterClassName, $source, $destination); |
| 84 | } catch (ConverterNotOperationalException $e) { |
| 85 | // Converter not operational, and that is ok! |
| 86 | // We shall pretend that the expected exception was thrown, by throwing it! |
| 87 | throw new CreateDestinationFolderException(); |
| 88 | } |
| 89 | /* |
| 90 | try { |
| 91 | // We can only do this test, if the converter is operational. |
| 92 | // In order to test that, we first do a normal conversion |
| 93 | $source = (__DIR__ . '/../../test.jpg'); |
| 94 | $destination = (__DIR__ . '/../../test.webp'); |
| 95 | |
| 96 | Imagick::convert($source, $destination); |
| 97 | |
| 98 | // if we are here, it means that the converter is operational. |
| 99 | // Now do something that tests that the converter fails the way it should, |
| 100 | // when it cannot create the destination file |
| 101 | |
| 102 | $this->expectException(\WebPConvert\Convert\Exceptions\ConverterFailedException::class); |
| 103 | |
| 104 | // I here assume that no system grants write access to their root folder |
| 105 | // this is perhaps wrong to assume? |
| 106 | $destinationFolder = '/you-can-delete-me/'; |
| 107 | |
| 108 | Imagick::convert(__DIR__ . '/../test.jpg', $destinationFolder . 'you-can-delete-me.webp'); |
| 109 | } catch (\Exception $e) { |
| 110 | // its ok... |
| 111 | }*/ |
| 112 | } |
| 113 | |
| 114 | public static function testTargetNotFound($testCase, $converterClassName, $converterOptions) |
| 115 | { |
| 116 | $testCase->expectException(TargetNotFoundException::class); |
| 117 | |
| 118 | try { |
| 119 | $result = self::callConvert( |
| 120 | $converterClassName, |
| 121 | __DIR__ . '/i-dont-exist.jpg', |
| 122 | __DIR__ . '/i-dont-exist.webp', |
| 123 | $converterOptions |
| 124 | ); |
| 125 | } catch (ConverterNotOperationalException $e) { |
| 126 | // Converter not operational, and that is ok! |
| 127 | // We shall pretend that the expected exception was thrown, by throwing it! |
| 128 | throw new TargetNotFoundException(); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Test convert. |
| 134 | * - It must either make a successful conversion, or throw the SystemRequirementsNotMetException |
| 135 | * Other exceptions are unexpected and will result in test failure |
| 136 | * - It must not return anything (as of 2.0, there is no return value) |
| 137 | * - If conversion is successful, there must be a file at the destination |
| 138 | */ |
| 139 | public static function testConvert($src, $testCase, $converterClassName, $converterOptions) |
| 140 | { |
| 141 | |
| 142 | try { |
| 143 | $source = self::getImagePath($src); |
| 144 | $destination = self::getImagePath($src . '.webp'); |
| 145 | |
| 146 | $result = self::callConvert($converterClassName, $source, $destination, $converterOptions); |
| 147 | |
| 148 | // Conversion was successful. |
| 149 | |
| 150 | // make sure the function did not return anything (as of 2.0) |
| 151 | $testCase->assertEmpty($result, 'The doActualConvert() method returned something. As of 2.0, converters should never return anything'); |
| 152 | |
| 153 | // verify that there indeed is a file |
| 154 | $testCase->assertTrue(file_exists($destination), 'There is not a converted file at the destinaiton'); |
| 155 | |
| 156 | } catch (ConverterNotOperationalException $e) { |
| 157 | // Converter not operational, and that is ok! |
| 158 | // (ie if system requirements are not met, or the quota of a cloud converter is used up) |
| 159 | |
| 160 | } catch (UnhandledException $e) { |
| 161 | // Handle the UnhandledException specially, so we can display the original error |
| 162 | $prevEx = $e->getPrevious(); |
| 163 | $testCase->fail( |
| 164 | 'An UnhandledException was thrown: ' . |
| 165 | get_class($prevEx). '. ' . |
| 166 | $prevEx->getMessage() . '. ' . |
| 167 | $prevEx->getFile() . ', line:' . $prevEx->getLine() |
| 168 | //'Trace:' . $prevEx->getTraceAsString() |
| 169 | ); |
| 170 | } catch (ConversionFailedException $e) { |
| 171 | $testCase->fail( |
| 172 | "A ConversionFailedException was thrown (and it was not a ConverterNotOperationalException). The exception was: " . |
| 173 | get_class($e) . |
| 174 | ". The message was: '" . $e->getMessage() . "'"); |
| 175 | } catch (\Exception $e) { |
| 176 | $testCase->fail("An unexpected exception was thrown:" . get_class($e) . '. Message:' . $e->getMessage()); |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | public static function warnIfNotOperational($converterClassName) |
| 181 | { |
| 182 | $converter = ConverterFactory::makeConverterFromClassname( |
| 183 | 'WebPConvert\\Convert\\Converters\\' . $converterClassName, |
| 184 | $source = self::getImagePath('test.jpg'), |
| 185 | $destination = self::getImagePath('test.jpg.webp') |
| 186 | ); |
| 187 | try { |
| 188 | $converter->checkOperationality(); |
| 189 | //echo "\n" . $converterClassName . ' is operational.' . "\n"; |
| 190 | } catch (\Exception $e) { |
| 191 | echo "\n" . 'NOTICE: ' . $converterClassName . ' is not operational: ' . $e->getMessage() . "\n"; |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | public static function runAllConvertTests($testCase, $converterClassName, $converterOptions = []) |
| 196 | { |
| 197 | self::warnIfNotOperational($converterClassName); |
| 198 | |
| 199 | $converterOptions['encoding'] = 'auto'; |
| 200 | self::testConvert('test.jpg', $testCase, $converterClassName, $converterOptions); |
| 201 | self::testConvert('test.png', $testCase, $converterClassName, $converterOptions); |
| 202 | //self::testConvert('not-true-color.png', $testCase, $converterClassName, $converterOptions); |
| 203 | |
| 204 | self::testTargetNotFound($testCase, $converterClassName, $converterOptions); |
| 205 | self::testInvalidDestinationFolder($testCase, $converterClassName, $converterOptions); |
| 206 | } |
| 207 | } |
| 208 |