wp-optimize
/
vendor
/
rosell-dk
/
webp-convert
/
tests
/
Convert
/
Converters
/
BaseTraits
/
AutoQualityTraitTest.php
AutoQualityTraitTest.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/BaseTraits/AutoQualityTraitTest.php
| 1 | <?php |
| 2 | |
| 3 | namespace WebPConvert\Tests\Convert\Converters\BaseTraits; |
| 4 | |
| 5 | //use WebPConvert\Convert\BaseConverters\AbstractCloudConverter; |
| 6 | use WebPConvert\Tests\Convert\TestConverters\SuccessGuaranteedConverter; |
| 7 | |
| 8 | use PHPUnit\Framework\TestCase; |
| 9 | |
| 10 | class AutoQualityTraitTest extends TestCase |
| 11 | { |
| 12 | |
| 13 | public static function getImageFolder() |
| 14 | { |
| 15 | return realpath(__DIR__ . '/../../../images'); |
| 16 | } |
| 17 | |
| 18 | public static function getImagePath($image) |
| 19 | { |
| 20 | return self::getImageFolder() . '/' . $image; |
| 21 | } |
| 22 | |
| 23 | public function testFixedQuality() |
| 24 | { |
| 25 | $converter = SuccessGuaranteedConverter::createInstance( |
| 26 | self::getImagePath('small-q61.jpg'), |
| 27 | self::getImagePath('small-q61.jpg.webp'), |
| 28 | [ |
| 29 | 'quality' => 77, |
| 30 | 'auto-limit' => false, |
| 31 | ] |
| 32 | ); |
| 33 | |
| 34 | $result = $converter->getCalculatedQuality(); |
| 35 | $this->assertSame(77, $result); |
| 36 | |
| 37 | $this->assertFalse($converter->isQualityDetectionRequiredButFailing()); |
| 38 | |
| 39 | // Test that it is still the same (testing caching) |
| 40 | $this->assertFalse($converter->isQualityDetectionRequiredButFailing()); |
| 41 | |
| 42 | } |
| 43 | |
| 44 | /* |
| 45 | public function testAutoQualityWhenQualityCannotBeDetected() |
| 46 | { |
| 47 | $converter = SuccessGuaranteedConverter::createInstance( |
| 48 | self::$imgDir . '/non-existant', |
| 49 | self::$imgDir . '/non-existant.webp', |
| 50 | [ |
| 51 | 'max-quality' => 80, |
| 52 | 'quality' => 'auto', |
| 53 | 'default-quality' => 70, |
| 54 | ] |
| 55 | ); |
| 56 | |
| 57 | $result = $converter->getCalculatedQuality(); |
| 58 | |
| 59 | $this->assertSame(70, $result); |
| 60 | }*/ |
| 61 | |
| 62 | public function testAutoQuality() |
| 63 | { |
| 64 | $converter = SuccessGuaranteedConverter::createInstance( |
| 65 | self::getImagePath('small-q61.jpg'), |
| 66 | self::getImagePath('small-q61.jpg.webp'), |
| 67 | [ |
| 68 | 'quality' => 61, |
| 69 | 'auto-limit' => true, |
| 70 | ] |
| 71 | ); |
| 72 | |
| 73 | $result = $converter->getCalculatedQuality(); |
| 74 | |
| 75 | // "Cheating" a bit here... |
| 76 | // - If quality detection fails, it will be 61 (because default-quality is set to 61) |
| 77 | // - If quality detection succeeds, it will also be 61 |
| 78 | $this->assertSame(61, $result); |
| 79 | } |
| 80 | |
| 81 | public function testAutoQualityDeprecatedOptions() |
| 82 | { |
| 83 | $converter = SuccessGuaranteedConverter::createInstance( |
| 84 | self::getImagePath('small-q61.jpg'), |
| 85 | self::getImagePath('small-q61.jpg.webp'), |
| 86 | [ |
| 87 | 'max-quality' => 80, |
| 88 | 'quality' => 'auto', |
| 89 | 'default-quality' => 61, |
| 90 | ] |
| 91 | ); |
| 92 | |
| 93 | $result = $converter->getCalculatedQuality(); |
| 94 | |
| 95 | // "Cheating" a bit here... |
| 96 | // - If quality detection fails, it will be 61 (because default-quality is set to 61) |
| 97 | // - If quality detection succeeds, it will also be 61 |
| 98 | $this->assertSame(61, $result); |
| 99 | } |
| 100 | |
| 101 | public function testAutoQualityMaxQuality() |
| 102 | { |
| 103 | $converter = SuccessGuaranteedConverter::createInstance( |
| 104 | self::getImagePath('small-q61.jpg'), |
| 105 | self::getImagePath('small-q61.jpg.webp'), |
| 106 | [ |
| 107 | 'max-quality' => 80, |
| 108 | 'quality' => 'auto', |
| 109 | 'default-quality' => 61, |
| 110 | ] |
| 111 | ); |
| 112 | |
| 113 | //$this->assertTrue(file_exists(self::$imgDir . '/small-q61.jpg')); |
| 114 | //$this->assertEquals('image/jpeg', $converter->getMimeTypeOfSource()); |
| 115 | |
| 116 | $this->assertSame(61, $converter->getCalculatedQuality()); |
| 117 | |
| 118 | // Test that it is still the same (testing caching) |
| 119 | $this->assertSame(61, $converter->getCalculatedQuality()); |
| 120 | } |
| 121 | |
| 122 | public function testAutoQualityMaxQualityOnNonJpeg() |
| 123 | { |
| 124 | $converter = SuccessGuaranteedConverter::createInstance( |
| 125 | self::getImagePath('test.png'), |
| 126 | self::getImagePath('test.png.webp'), |
| 127 | [ |
| 128 | 'max-quality' => 60, |
| 129 | 'quality' => 'auto', |
| 130 | 'default-quality' => 70, |
| 131 | ] |
| 132 | ); |
| 133 | |
| 134 | $this->assertSame(60, $converter->getCalculatedQuality()); |
| 135 | $this->assertFalse($converter->isQualityDetectionRequiredButFailing()); |
| 136 | } |
| 137 | /* |
| 138 | public function testAutoQualityOnQualityDetectionFail1() |
| 139 | { |
| 140 | $converter = SuccessGuaranteedConverter::createInstance( |
| 141 | self::$imgDir . '/non-existing.jpg', |
| 142 | self::$imgDir . '/non-existant.webp', |
| 143 | [ |
| 144 | 'max-quality' => 70, |
| 145 | 'quality' => 'auto', |
| 146 | 'default-quality' => 60, |
| 147 | ] |
| 148 | ); |
| 149 | |
| 150 | $this->assertFalse(file_exists(self::$imgDir . '/non-existing.jpg')); |
| 151 | |
| 152 | // MimeType guesser returns false when mime type cannot be established. |
| 153 | $this->assertEquals(false, $converter->getMimeTypeOfSource()); |
| 154 | |
| 155 | // - so this can actually not be used for testing isQualityDetectionRequiredButFailing |
| 156 | |
| 157 | //$this->assertSame(60, $converter->getCalculatedQuality()); |
| 158 | //$this->assertTrue($converter->isQualityDetectionRequiredButFailing()); |
| 159 | } |
| 160 | */ |
| 161 | public function testAutoQualityOnQualityDetectionFail2DeprecatedOptions() |
| 162 | { |
| 163 | $converter = SuccessGuaranteedConverter::createInstance( |
| 164 | self::getImagePath('text-with-jpg-extension.jpg'), |
| 165 | self::getImagePath('text-with-jpg-extension.jpg.webp'), |
| 166 | [ |
| 167 | 'max-quality' => 70, |
| 168 | 'quality' => 'auto', |
| 169 | 'default-quality' => 60, |
| 170 | ] |
| 171 | ); |
| 172 | |
| 173 | $this->assertFalse(file_exists(self::getImagePath('non-existing.jpg'))); |
| 174 | |
| 175 | // We are using the lenient MimeType guesser. |
| 176 | // So we get "image/jpeg" even though the file is not a jpeg file |
| 177 | $this->assertEquals('image/jpeg', $converter->getMimeTypeOfSource()); |
| 178 | |
| 179 | // Now we got a file that we should not be able to detect quality of |
| 180 | // lets validate that statement: |
| 181 | |
| 182 | $this->assertTrue($converter->isQualityDetectionRequiredButFailing()); |
| 183 | |
| 184 | // Test that it is still the same (testing caching) |
| 185 | $this->assertTrue($converter->isQualityDetectionRequiredButFailing()); |
| 186 | |
| 187 | $this->assertSame(60, $converter->getCalculatedQuality()); |
| 188 | } |
| 189 | |
| 190 | } |
| 191 |