| 1 |
<?php |
| 2 |
|
| 3 |
namespace WebPConvert\Tests\Serve; |
| 4 |
|
| 5 |
use ImageMimeTypeGuesser\ImageMimeTypeGuesser; |
| 6 |
use WebPConvert\Exceptions\InvalidInputException; |
| 7 |
use WebPConvert\Exceptions\InvalidInput\InvalidImageTypeException; |
| 8 |
use WebPConvert\Exceptions\InvalidInput\TargetNotFoundException; |
| 9 |
use WebPConvert\Serve\ServeConvertedWebP; |
| 10 |
use WebPConvert\Serve\MockedHeader; |
| 11 |
use WebPConvert\Serve\Exceptions\ServeFailedException; |
| 12 |
|
| 13 |
use WebPConvert\Tests\CompatibleTestCase; |
| 14 |
|
| 15 |
use ServeConvertedWebPExposer; |
| 16 |
|
| 17 |
use PHPUnit\Framework\TestCase; |
| 18 |
|
| 19 |
/** |
| 20 |
* @coversDefaultClass WebPConvert\Serve\ServeConvertedWebP |
| 21 |
* @covers WebPConvert\Serve\ServeConvertedWebP |
| 22 |
*/ |
| 23 |
class ServeConvertedWebPTest extends CompatibleTestCase |
| 24 |
{ |
| 25 |
|
| 26 |
public function getImageFolder() |
| 27 |
{ |
| 28 |
return realpath(__DIR__ . '/../images'); |
| 29 |
} |
| 30 |
|
| 31 |
public function getImagePath($image) |
| 32 |
{ |
| 33 |
return $this->getImageFolder() . '/' . $image; |
| 34 |
} |
| 35 |
|
| 36 |
|
| 37 |
/** |
| 38 |
* Call to serve and return result or exception |
| 39 |
* |
| 40 |
* The method takes care of closing output buffer in case of exception |
| 41 |
* |
| 42 |
* @return array First item: the output, second item: Exception, if thrown |
| 43 |
*/ |
| 44 |
public static function callServe($filename, $destination, $options) |
| 45 |
{ |
| 46 |
ob_start(); |
| 47 |
try { |
| 48 |
ServeConvertedWebP::serve($filename, $destination, $options); |
| 49 |
} catch (\Exception $e) { |
| 50 |
return [ob_get_clean(), $e]; |
| 51 |
} catch (\Throwable $e) { |
| 52 |
return [ob_get_clean(), $e]; |
| 53 |
} |
| 54 |
return [ob_get_clean(), null]; |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Call to serve and return result or exception |
| 59 |
* |
| 60 |
* The method takes care of closing output buffer in case of exception |
| 61 |
* |
| 62 |
* @return string the output |
| 63 |
*/ |
| 64 |
public static function callServeWithThrow($filename, $destination, $options) |
| 65 |
{ |
| 66 |
ob_start(); |
| 67 |
try { |
| 68 |
ServeConvertedWebP::serve($filename, $destination, $options); |
| 69 |
} catch (\Exception $e) { |
| 70 |
ob_get_clean(); |
| 71 |
throw($e); |
| 72 |
} catch (\Throwable $e) { |
| 73 |
ob_get_clean(); |
| 74 |
throw($e); |
| 75 |
} |
| 76 |
return ob_get_clean(); |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* Call to serve and return result or exception |
| 81 |
* |
| 82 |
* The method takes care of closing output buffer in case of exception |
| 83 |
* |
| 84 |
* @return string the output |
| 85 |
*/ |
| 86 |
public static function callServeOriginalWithThrow($filename, $options) |
| 87 |
{ |
| 88 |
ob_start(); |
| 89 |
try { |
| 90 |
ServeConvertedWebP::serveOriginal($filename, $options); |
| 91 |
} catch (\Exception $e) { |
| 92 |
ob_get_clean(); |
| 93 |
throw($e); |
| 94 |
} catch (\Throwable $e) { |
| 95 |
ob_get_clean(); |
| 96 |
throw($e); |
| 97 |
} |
| 98 |
return ob_get_clean(); |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* @covers ::serveOriginal |
| 103 |
*/ |
| 104 |
public function testServeOriginal() |
| 105 |
{ |
| 106 |
$source = $this->getImagePath('test.png'); |
| 107 |
$this->assertTrue(file_exists($source), 'source file does not exist:' . $source); |
| 108 |
|
| 109 |
$destination = $source . '.webp'; |
| 110 |
|
| 111 |
$options = [ |
| 112 |
//'serve-original' => true, |
| 113 |
//'reconvert' => true, |
| 114 |
'convert' => [ |
| 115 |
'converters' => [ |
| 116 |
'\\WebPConvert\\Tests\\Convert\\TestConverters\\SuccessGuaranteedConverter' |
| 117 |
] |
| 118 |
] |
| 119 |
]; |
| 120 |
$result = self::callServeOriginalWithThrow($source, $options); |
| 121 |
|
| 122 |
// Test that headers were set as expected |
| 123 |
//$this->assertTrue(MockedHeader::hasHeaderContaining('X-WebP-Convert-Action:')); |
| 124 |
|
| 125 |
$this->assertTrue(MockedHeader::hasHeader('Content-Type: image/png')); |
| 126 |
$this->assertFalse(MockedHeader::hasHeader('Vary: Accept')); |
| 127 |
$this->assertTrue(MockedHeader::hasHeaderContaining('Last-Modified:')); |
| 128 |
$this->assertFalse(MockedHeader::hasHeaderContaining('Cache-Control:')); |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
/** |
| 133 |
* @covers ::serveOriginal |
| 134 |
*/ |
| 135 |
public function testServeOriginalNotAnImage() |
| 136 |
{ |
| 137 |
//$this->expectException(InvalidImageTypeException::class); |
| 138 |
$this->expectException(ServeFailedException::class); |
| 139 |
|
| 140 |
$source =$this->getImagePath('text.txt'); |
| 141 |
$this->assertTrue(file_exists($source), 'source file does not exist'); |
| 142 |
|
| 143 |
$contentType = ImageMimeTypeGuesser::lenientGuess($source); |
| 144 |
$this->assertSame(false, $contentType); |
| 145 |
|
| 146 |
$options = [ |
| 147 |
//'serve-original' => true, |
| 148 |
//'reconvert' => true, |
| 149 |
'convert' => [ |
| 150 |
'converters' => [ |
| 151 |
'\\WebPConvert\\Tests\\Convert\\TestConverters\\SuccessGuaranteedConverter' |
| 152 |
] |
| 153 |
] |
| 154 |
]; |
| 155 |
$result = self::callServeOriginalWithThrow($source, []); |
| 156 |
$this->assertEquals('', $result); |
| 157 |
} |
| 158 |
|
| 159 |
/** |
| 160 |
* @covers ::serveOriginal |
| 161 |
*/ |
| 162 |
public function testServeOriginalNotAnImage2() |
| 163 |
{ |
| 164 |
//$this->expectException(InvalidImageTypeException::class); |
| 165 |
$this->expectException(ServeFailedException::class); |
| 166 |
|
| 167 |
$source = $this->getImagePath('text'); |
| 168 |
$this->assertTrue(file_exists($source), 'source file does not exist'); |
| 169 |
|
| 170 |
$contentType = ImageMimeTypeGuesser::lenientGuess($source); |
| 171 |
$this->assertSame(null, $contentType); |
| 172 |
|
| 173 |
$options = [ |
| 174 |
//'serve-original' => true, |
| 175 |
//'reconvert' => true, |
| 176 |
'convert' => [ |
| 177 |
'converters' => [ |
| 178 |
'\\WebPConvert\\Tests\\Convert\\TestConverters\\SuccessGuaranteedConverter' |
| 179 |
] |
| 180 |
] |
| 181 |
]; |
| 182 |
$result = self::callServeOriginalWithThrow($source, $options); |
| 183 |
$this->assertEquals('', $result); |
| 184 |
} |
| 185 |
|
| 186 |
/** |
| 187 |
* @covers ::serve |
| 188 |
*/ |
| 189 |
public function testServeReconvert() |
| 190 |
{ |
| 191 |
$source = $this->getImagePath('test.png'); |
| 192 |
$this->assertTrue(file_exists($source)); |
| 193 |
|
| 194 |
$destination = $source . '.webp'; |
| 195 |
|
| 196 |
$options = [ |
| 197 |
//'serve-original' => true, |
| 198 |
'reconvert' => true, |
| 199 |
'convert' => [ |
| 200 |
'converters' => [ |
| 201 |
'\\WebPConvert\\Tests\\Convert\\TestConverters\\SuccessGuaranteedConverter' |
| 202 |
] |
| 203 |
] |
| 204 |
]; |
| 205 |
$result = self::callServeWithThrow($source, $destination, $options); |
| 206 |
|
| 207 |
// Test that headers were set as expected |
| 208 |
//$this->assertTrue(MockedHeader::hasHeaderContaining('X-WebP-Convert-Action:')); |
| 209 |
|
| 210 |
$this->assertTrue(MockedHeader::hasHeader('Content-Type: image/webp')); |
| 211 |
$this->assertFalse(MockedHeader::hasHeader('Vary: Accept')); |
| 212 |
$this->assertTrue(MockedHeader::hasHeaderContaining('Last-Modified:')); |
| 213 |
$this->assertFalse(MockedHeader::hasHeaderContaining('Cache-Control:')); |
| 214 |
} |
| 215 |
|
| 216 |
/** |
| 217 |
* @covers ::serve |
| 218 |
*/ |
| 219 |
public function testServeServeOriginal() |
| 220 |
{ |
| 221 |
$source = $this->getImagePath('test.png'); |
| 222 |
$this->assertTrue(file_exists($source)); |
| 223 |
|
| 224 |
$destination = $source . '.webp'; |
| 225 |
|
| 226 |
$options = [ |
| 227 |
'serve-original' => true, |
| 228 |
//'reconvert' => true, |
| 229 |
'convert' => [ |
| 230 |
'converters' => [ |
| 231 |
'\\WebPConvert\\Tests\\Convert\\TestConverters\\SuccessGuaranteedConverter' |
| 232 |
] |
| 233 |
] |
| 234 |
]; |
| 235 |
$result = self::callServeWithThrow($source, $destination, $options); |
| 236 |
|
| 237 |
// Test that headers were set as expected |
| 238 |
//$this->assertTrue(MockedHeader::hasHeaderContaining('X-WebP-Convert-Action:')); |
| 239 |
|
| 240 |
$this->assertTrue(MockedHeader::hasHeader('Content-Type: image/png')); |
| 241 |
$this->assertFalse(MockedHeader::hasHeader('Vary: Accept')); |
| 242 |
$this->assertTrue(MockedHeader::hasHeaderContaining('Last-Modified:')); |
| 243 |
$this->assertFalse(MockedHeader::hasHeaderContaining('Cache-Control:')); |
| 244 |
} |
| 245 |
|
| 246 |
/** |
| 247 |
* Testing when the "cached" image can be served |
| 248 |
* @covers ::serve |
| 249 |
*/ |
| 250 |
public function testServeDestination() |
| 251 |
{ |
| 252 |
$source = $this->getImagePath('/test.png'); |
| 253 |
$this->assertTrue(file_exists($source)); |
| 254 |
|
| 255 |
// create fake webp at destination |
| 256 |
$destination = $source . '.webp'; |
| 257 |
file_put_contents($destination, '1234'); |
| 258 |
$this->assertTrue(file_exists($destination)); |
| 259 |
|
| 260 |
$options = [ |
| 261 |
//'serve-original' => true, |
| 262 |
//'reconvert' => true, |
| 263 |
'convert' => [ |
| 264 |
'converters' => [ |
| 265 |
'\\WebPConvert\\Tests\\Convert\\TestConverters\\SuccessGuaranteedConverter' |
| 266 |
] |
| 267 |
] |
| 268 |
]; |
| 269 |
$result = self::callServeWithThrow($source, $destination, $options); |
| 270 |
|
| 271 |
|
| 272 |
// Check that destination is output (it has the content "1234") |
| 273 |
$this->assertEquals('1234', $result); |
| 274 |
|
| 275 |
// Test that headers were set as expected |
| 276 |
//$this->assertTrue(MockedHeader::hasHeaderContaining('X-WebP-Convert-Action:')); |
| 277 |
|
| 278 |
$this->assertTrue(MockedHeader::hasHeader('Content-Type: image/webp')); |
| 279 |
} |
| 280 |
|
| 281 |
/** |
| 282 |
* @covers ::serve |
| 283 |
*/ |
| 284 |
public function testEmptySourceArg() |
| 285 |
{ |
| 286 |
$this->expectException(InvalidInputException::class); |
| 287 |
|
| 288 |
$options = [ |
| 289 |
//'serve-original' => true, |
| 290 |
//'reconvert' => true, |
| 291 |
'convert' => [ |
| 292 |
'converters' => [ |
| 293 |
'\\WebPConvert\\Tests\\Convert\\TestConverters\\SuccessGuaranteedConverter' |
| 294 |
] |
| 295 |
] |
| 296 |
]; |
| 297 |
|
| 298 |
$source = ''; |
| 299 |
$this->assertEmpty($source); |
| 300 |
$result = self::callServeWithThrow($source, $this->getImagePath('test.png.webp'), $options); |
| 301 |
} |
| 302 |
|
| 303 |
/** |
| 304 |
* @covers ::serve |
| 305 |
*/ |
| 306 |
public function testEmptyDestinationArg() |
| 307 |
{ |
| 308 |
$this->expectException(InvalidInputException::class); |
| 309 |
|
| 310 |
$source = $this->getImagePath('test.png'); |
| 311 |
$this->assertTrue(file_exists($source)); |
| 312 |
|
| 313 |
$destination = ''; |
| 314 |
|
| 315 |
$options = [ |
| 316 |
//'serve-original' => true, |
| 317 |
//'reconvert' => true, |
| 318 |
'convert' => [ |
| 319 |
'converters' => [ |
| 320 |
'\\WebPConvert\\Tests\\Convert\\TestConverters\\SuccessGuaranteedConverter' |
| 321 |
] |
| 322 |
] |
| 323 |
]; |
| 324 |
$result = self::callServeWithThrow($source, $destination, $options); |
| 325 |
} |
| 326 |
|
| 327 |
/** |
| 328 |
* @covers ::serve |
| 329 |
*/ |
| 330 |
public function testNoFileAtSource() |
| 331 |
{ |
| 332 |
$this->expectException(TargetNotFoundException::class); |
| 333 |
|
| 334 |
$source = $this->getImagePath('i-do-not-exist.png'); |
| 335 |
$this->assertFalse(file_exists($source)); |
| 336 |
|
| 337 |
$destination = ''; |
| 338 |
|
| 339 |
$options = [ |
| 340 |
//'serve-original' => true, |
| 341 |
//'reconvert' => true, |
| 342 |
'convert' => [ |
| 343 |
'converters' => [ |
| 344 |
'\\WebPConvert\\Tests\\Convert\\TestConverters\\SuccessGuaranteedConverter' |
| 345 |
] |
| 346 |
] |
| 347 |
]; |
| 348 |
$result = self::callServeWithThrow($source, $destination, $options); |
| 349 |
} |
| 350 |
|
| 351 |
/** |
| 352 |
* @covers ::serve |
| 353 |
*/ |
| 354 |
public function testServeReport() |
| 355 |
{ |
| 356 |
$source = $this->getImagePath('test.png'); |
| 357 |
$this->assertTrue(file_exists($source)); |
| 358 |
$destination = $source . '.webp'; |
| 359 |
|
| 360 |
$options = [ |
| 361 |
//'serve-original' => true, |
| 362 |
//'reconvert' => true, |
| 363 |
'show-report' => true, |
| 364 |
'convert' => [ |
| 365 |
'converters' => [ |
| 366 |
'\\WebPConvert\\Tests\\Convert\\TestConverters\\SuccessGuaranteedConverter' |
| 367 |
] |
| 368 |
] |
| 369 |
]; |
| 370 |
$result = self::callServeWithThrow($source, $destination, $options); |
| 371 |
|
| 372 |
// Check that output looks like a report |
| 373 |
$this->assertTrue(strpos($result, 'source:') !== false, 'The following does not contain "source:":' . $result); |
| 374 |
|
| 375 |
// Test that headers were set as expected |
| 376 |
//$this->assertTrue(MockedHeader::hasHeaderContaining('X-WebP-Convert-Action:')); |
| 377 |
|
| 378 |
$this->assertTrue(MockedHeader::hasHeader('Content-Type: image/webp')); |
| 379 |
} |
| 380 |
|
| 381 |
public function testSourceIsLighter() |
| 382 |
{ |
| 383 |
$source = $this->getImagePath('plaintext-with-jpg-extension.jpg'); |
| 384 |
|
| 385 |
// create fake webp at destination, which is larger than the fake jpg |
| 386 |
file_put_contents($source . '.webp', 'aotehu aotnehuatnoehutaoehu atonse uaoehu'); |
| 387 |
|
| 388 |
$this->assertTrue(file_exists($source)); |
| 389 |
$this->assertTrue(file_exists($source . '.webp')); |
| 390 |
|
| 391 |
$options = [ |
| 392 |
'convert' => [ |
| 393 |
'converters' => [ |
| 394 |
'\\WebPConvert\\Tests\\Convert\\TestConverters\\SuccessGuaranteedConverter' |
| 395 |
] |
| 396 |
] |
| 397 |
]; |
| 398 |
$result = self::callServeWithThrow($source, $source . '.webp', $options); |
| 399 |
|
| 400 |
// the source file contains "text", so the next assert asserts that source was served |
| 401 |
$this->assertMatchesRegularExpression2('#text#', $result); |
| 402 |
} |
| 403 |
|
| 404 |
public function testExistingOutDated() |
| 405 |
{ |
| 406 |
$source = $this->getImagePath('test.jpg'); |
| 407 |
$this->assertTrue(file_exists($source)); |
| 408 |
|
| 409 |
$destination = $source . '.webp'; |
| 410 |
@unlink($destination); |
| 411 |
copy($this->getImagePath('pre-converted/test.webp'), $destination); |
| 412 |
|
| 413 |
// set modification date earlier than source |
| 414 |
touch($destination, filemtime($source) - 1000); |
| 415 |
|
| 416 |
// check that it worked |
| 417 |
$this->assertLessThan(filemtime($source), filemtime($destination)); |
| 418 |
|
| 419 |
$options = [ |
| 420 |
'convert' => [ |
| 421 |
'converters' => [ |
| 422 |
'\\WebPConvert\\Tests\\Convert\\TestConverters\\SuccessGuaranteedConverter' |
| 423 |
] |
| 424 |
] |
| 425 |
]; |
| 426 |
$result = self::callServeWithThrow($source, $source . '.webp', $options); |
| 427 |
|
| 428 |
unlink($destination); |
| 429 |
|
| 430 |
// Our success-converter always creates fake webps with the content: |
| 431 |
// "we-pretend-this-is-a-valid-webp!". |
| 432 |
// So testing that we got this back is the same as testing that a "conversion" was |
| 433 |
// done and the converted file was served. It is btw smaller than the source. |
| 434 |
|
| 435 |
$this->assertMatchesRegularExpression2('#we-pretend-this-is-a-valid-webp!#', $result); |
| 436 |
} |
| 437 |
|
| 438 |
public function testNoFileAtDestination() |
| 439 |
{ |
| 440 |
$source = $this->getImagePath('test.jpg'); |
| 441 |
$this->assertTrue(file_exists($source)); |
| 442 |
|
| 443 |
$destination = $source . '.webp'; |
| 444 |
@unlink($destination); |
| 445 |
|
| 446 |
$options = [ |
| 447 |
'convert' => [ |
| 448 |
'converters' => [ |
| 449 |
'\\WebPConvert\\Tests\\Convert\\TestConverters\\SuccessGuaranteedConverter' |
| 450 |
] |
| 451 |
] |
| 452 |
]; |
| 453 |
$result = self::callServeWithThrow($source, $source . '.webp', $options); |
| 454 |
|
| 455 |
// Our success-converter always creates fake webps with the content: |
| 456 |
// "we-pretend-this-is-a-valid-webp!". |
| 457 |
// So testing that we got this back is the same as testing that a "convert" was |
| 458 |
// done and the converted file was served. It is btw smaller than the source. |
| 459 |
|
| 460 |
$this->assertMatchesRegularExpression2('#we-pretend-this-is-a-valid-webp!#', $result); |
| 461 |
} |
| 462 |
|
| 463 |
} |
| 464 |
require_once('mock-header.inc'); |
| 465 |
|