Completed
Push — master ( 038254...115ca0 )
by Alejandro
06:27
created

invalidShortCodeFallsBackToNextMiddleware()   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\EntityDoesNotExistException;
15
use Shlinkio\Shlink\Core\Exception\InvalidShortCodeException;
16
use Shlinkio\Shlink\Core\Service\UrlShortener;
17
use Shlinkio\Shlink\PreviewGenerator\Service\PreviewGenerator;
18
use Zend\Diactoros\Response;
19
use Zend\Diactoros\ServerRequest;
20
21
use function filesize;
22
23
use const FILEINFO_MIME;
24
25
class PreviewActionTest extends TestCase
26
{
27
    /** @var PreviewAction */
28
    private $action;
29
    /** @var ObjectProphecy */
30
    private $previewGenerator;
31
    /** @var ObjectProphecy */
32
    private $urlShortener;
33
34
    public function setUp(): void
35
    {
36
        $this->previewGenerator = $this->prophesize(PreviewGenerator::class);
37
        $this->urlShortener = $this->prophesize(UrlShortener::class);
38
        $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

38
        $this->action = /** @scrutinizer ignore-deprecated */ new PreviewAction($this->previewGenerator->reveal(), $this->urlShortener->reveal());
Loading history...
39
    }
40
41
    /** @test */
42
    public function invalidShortCodeFallsBackToNextMiddleware(): void
43
    {
44
        $shortCode = 'abc123';
45
        $this->urlShortener->shortCodeToUrl($shortCode)->willThrow(EntityDoesNotExistException::class)
46
                                                       ->shouldBeCalledOnce();
47
        $delegate = $this->prophesize(RequestHandlerInterface::class);
48
        $delegate->handle(Argument::cetera())->shouldBeCalledOnce()
49
                                              ->willReturn(new Response());
50
51
        $this->action->process((new ServerRequest())->withAttribute('shortCode', $shortCode), $delegate->reveal());
52
    }
53
54
    /** @test */
55
    public function correctShortCodeReturnsImageResponse(): void
56
    {
57
        $shortCode = 'abc123';
58
        $url = 'foobar.com';
59
        $shortUrl = new ShortUrl($url);
60
        $path = __FILE__;
61
        $this->urlShortener->shortCodeToUrl($shortCode)->willReturn($shortUrl)->shouldBeCalledOnce();
62
        $this->previewGenerator->generatePreview($url)->willReturn($path)->shouldBeCalledOnce();
63
64
        $resp = $this->action->process(
65
            (new ServerRequest())->withAttribute('shortCode', $shortCode),
66
            $this->prophesize(RequestHandlerInterface::class)->reveal()
67
        );
68
69
        $this->assertEquals(filesize($path), $resp->getHeaderLine('Content-length'));
70
        $this->assertEquals((new finfo(FILEINFO_MIME))->file($path), $resp->getHeaderLine('Content-type'));
71
    }
72
73
    /** @test */
74
    public function invalidShortCodeExceptionFallsBackToNextMiddleware(): void
75
    {
76
        $shortCode = 'abc123';
77
        $this->urlShortener->shortCodeToUrl($shortCode)->willThrow(InvalidShortCodeException::class)
78
                                                       ->shouldBeCalledOnce();
79
        $delegate = $this->prophesize(RequestHandlerInterface::class);
80
        $process = $delegate->handle(Argument::any())->willReturn(new Response());
81
82
        $this->action->process(
83
            (new ServerRequest())->withAttribute('shortCode', $shortCode),
84
            $delegate->reveal()
85
        );
86
87
        $process->shouldHaveBeenCalledOnce();
88
    }
89
}
90