Completed
Pull Request — master (#554)
by Alejandro
14:05
created

are()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 0
dl 0
loc 10
rs 10
c 0
b 0
f 0
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());
0 ignored issues
show
Deprecated Code introduced by
The class Shlinkio\Shlink\Core\Action\PreviewAction has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

37
        $this->action = /** @scrutinizer ignore-deprecated */ new PreviewAction($this->previewGenerator->reveal(), $this->urlShortener->reveal());
Loading history...
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