1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace EcodevTests\Felix\Handler; |
6
|
|
|
|
7
|
|
|
use Doctrine\Persistence\ObjectRepository; |
8
|
|
|
use Ecodev\Felix\Handler\ImageHandler; |
9
|
|
|
use Ecodev\Felix\Model\Image; |
10
|
|
|
use Ecodev\Felix\Service\ImageResizer; |
11
|
|
|
use Laminas\Diactoros\ServerRequest; |
12
|
|
|
use org\bovigo\vfs\vfsStream; |
13
|
|
|
use PHPUnit\Framework\TestCase; |
14
|
|
|
use Psr\Http\Message\ResponseInterface; |
15
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
16
|
|
|
|
17
|
|
|
class ImageHandlerTest extends TestCase |
18
|
|
|
{ |
19
|
|
|
protected function setUp(): void |
20
|
|
|
{ |
21
|
|
|
// Minimal binary headers to cheat mime detection |
22
|
|
|
$virtualFileSystem = [ |
23
|
|
|
'image.png' => '', |
24
|
|
|
'image-100.jpg' => "\xff\xd8\xff\xe0\x00\x10\x4a\x46\x49\x46\x00\x01\x01\x01\x00\x60", |
25
|
|
|
'image-100.webp' => 'RIFF4<..WEBPVP8', |
26
|
|
|
]; |
27
|
|
|
|
28
|
|
|
vfsStream::setup('felix', null, $virtualFileSystem); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function testWillServeJpgByDefault(): void |
32
|
|
|
{ |
33
|
|
|
$image = $this->createImageMock(); |
34
|
|
|
$repository = $this->createRepositoryMock($image); |
35
|
|
|
|
36
|
|
|
$maxHeight = 100; |
37
|
|
|
$imageResizer = $this->createMock(ImageResizer::class); |
38
|
|
|
$imageResizer->expects(self::once()) |
39
|
|
|
->method('resize') |
40
|
|
|
->with($image, $maxHeight, false) |
41
|
|
|
->willReturn('vfs://felix/image-100.jpg'); |
42
|
|
|
|
43
|
|
|
// A request without accept header |
44
|
|
|
$request = new ServerRequest(); |
45
|
|
|
$request = $request->withAttribute('maxHeight', $maxHeight); |
46
|
|
|
|
47
|
|
|
$response = $this->handle($repository, $imageResizer, $request); |
48
|
|
|
|
49
|
|
|
self::assertSame('image/jpeg', $response->getHeaderLine('content-type')); |
50
|
|
|
self::assertSame('16', $response->getHeaderLine('content-length')); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function testWillServeWebpIfAccepted(): void |
54
|
|
|
{ |
55
|
|
|
$image = $this->createImageMock(); |
56
|
|
|
$repository = $this->createRepositoryMock($image); |
57
|
|
|
|
58
|
|
|
$maxHeight = 100; |
59
|
|
|
$imageResizer = $this->createMock(ImageResizer::class); |
60
|
|
|
$imageResizer->expects(self::once()) |
61
|
|
|
->method('resize') |
62
|
|
|
->with($image, $maxHeight, true) |
63
|
|
|
->willReturn('vfs://felix/image-100.webp'); |
64
|
|
|
|
65
|
|
|
// A request specifically accepting webp images |
66
|
|
|
$request = new ServerRequest(); |
67
|
|
|
$request = $request->withAttribute('maxHeight', $maxHeight) |
68
|
|
|
->withHeader('accept', 'text/html, image/webp, */*;q=0.8'); |
69
|
|
|
|
70
|
|
|
$response = $this->handle($repository, $imageResizer, $request); |
71
|
|
|
|
72
|
|
|
self::assertSame('image/webp', $response->getHeaderLine('content-type')); |
73
|
|
|
self::assertSame('15', $response->getHeaderLine('content-length')); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
private function handle(ObjectRepository $repository, ImageResizer $imageResizer, ServerRequestInterface $request): ResponseInterface |
77
|
|
|
{ |
78
|
|
|
$handler = new ImageHandler($repository, $imageResizer); |
79
|
|
|
|
80
|
|
|
return $handler->handle($request); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
private function createImageMock(): Image |
84
|
|
|
{ |
85
|
|
|
$image = $this->createMock(Image::class); |
86
|
|
|
$image->expects(self::once())->method('getPath')->willReturn('vfs://felix/image.png'); |
87
|
|
|
|
88
|
|
|
return $image; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
private function createRepositoryMock(Image $image): ObjectRepository |
92
|
|
|
{ |
93
|
|
|
$repository = $this->createMock(ObjectRepository::class); |
94
|
|
|
$repository->expects(self::once())->method('find')->willReturn($image); |
95
|
|
|
|
96
|
|
|
return $repository; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|