|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace ShlinkioTest\Shlink\Core\Action; |
|
6
|
|
|
|
|
7
|
|
|
use finfo; |
|
8
|
|
|
use PHPUnit\Framework\TestCase; |
|
9
|
|
|
use Prophecy\Argument; |
|
10
|
|
|
use Prophecy\Prophecy\ObjectProphecy; |
|
11
|
|
|
use Psr\Http\Server\RequestHandlerInterface; |
|
12
|
|
|
use Shlinkio\Shlink\Core\Action\PreviewAction; |
|
13
|
|
|
use Shlinkio\Shlink\Core\Entity\ShortUrl; |
|
14
|
|
|
use Shlinkio\Shlink\Core\Exception\ShortUrlNotFoundException; |
|
15
|
|
|
use Shlinkio\Shlink\Core\Service\UrlShortener; |
|
16
|
|
|
use Shlinkio\Shlink\PreviewGenerator\Service\PreviewGenerator; |
|
17
|
|
|
use Zend\Diactoros\Response; |
|
18
|
|
|
use Zend\Diactoros\ServerRequest; |
|
19
|
|
|
|
|
20
|
|
|
use function filesize; |
|
21
|
|
|
|
|
22
|
|
|
use const FILEINFO_MIME; |
|
23
|
|
|
|
|
24
|
|
|
class PreviewActionTest extends TestCase |
|
25
|
|
|
{ |
|
26
|
|
|
/** @var PreviewAction */ |
|
27
|
|
|
private $action; |
|
28
|
|
|
/** @var ObjectProphecy */ |
|
29
|
|
|
private $previewGenerator; |
|
30
|
|
|
/** @var ObjectProphecy */ |
|
31
|
|
|
private $urlShortener; |
|
32
|
|
|
|
|
33
|
|
|
public function setUp(): void |
|
34
|
|
|
{ |
|
35
|
|
|
$this->previewGenerator = $this->prophesize(PreviewGenerator::class); |
|
36
|
|
|
$this->urlShortener = $this->prophesize(UrlShortener::class); |
|
37
|
|
|
$this->action = new PreviewAction($this->previewGenerator->reveal(), $this->urlShortener->reveal()); |
|
|
|
|
|
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** @test */ |
|
41
|
|
|
public function correctShortCodeReturnsImageResponse(): void |
|
42
|
|
|
{ |
|
43
|
|
|
$shortCode = 'abc123'; |
|
44
|
|
|
$url = 'foobar.com'; |
|
45
|
|
|
$shortUrl = new ShortUrl($url); |
|
46
|
|
|
$path = __FILE__; |
|
47
|
|
|
$this->urlShortener->shortCodeToUrl($shortCode)->willReturn($shortUrl)->shouldBeCalledOnce(); |
|
48
|
|
|
$this->previewGenerator->generatePreview($url)->willReturn($path)->shouldBeCalledOnce(); |
|
49
|
|
|
|
|
50
|
|
|
$resp = $this->action->process( |
|
51
|
|
|
(new ServerRequest())->withAttribute('shortCode', $shortCode), |
|
52
|
|
|
$this->prophesize(RequestHandlerInterface::class)->reveal() |
|
53
|
|
|
); |
|
54
|
|
|
|
|
55
|
|
|
$this->assertEquals(filesize($path), $resp->getHeaderLine('Content-length')); |
|
56
|
|
|
$this->assertEquals((new finfo(FILEINFO_MIME))->file($path), $resp->getHeaderLine('Content-type')); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** @test */ |
|
60
|
|
|
public function invalidShortCodeExceptionFallsBackToNextMiddleware(): void |
|
61
|
|
|
{ |
|
62
|
|
|
$shortCode = 'abc123'; |
|
63
|
|
|
$this->urlShortener->shortCodeToUrl($shortCode)->willThrow(ShortUrlNotFoundException::class) |
|
64
|
|
|
->shouldBeCalledOnce(); |
|
65
|
|
|
$delegate = $this->prophesize(RequestHandlerInterface::class); |
|
66
|
|
|
$process = $delegate->handle(Argument::any())->willReturn(new Response()); |
|
67
|
|
|
|
|
68
|
|
|
$this->action->process( |
|
69
|
|
|
(new ServerRequest())->withAttribute('shortCode', $shortCode), |
|
70
|
|
|
$delegate->reveal() |
|
71
|
|
|
); |
|
72
|
|
|
|
|
73
|
|
|
$process->shouldHaveBeenCalledOnce(); |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|