Completed
Push — develop ( 319b79...8d438a )
by Alejandro
26s queued 13s
created

QrCodeActionTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 8
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 Laminas\Diactoros\Response;
8
use Laminas\Diactoros\ServerRequest;
9
use Mezzio\Router\RouterInterface;
10
use PHPUnit\Framework\TestCase;
11
use Prophecy\Argument;
12
use Prophecy\Prophecy\ObjectProphecy;
13
use Psr\Http\Server\RequestHandlerInterface;
14
use Shlinkio\Shlink\Common\Response\QrCodeResponse;
15
use Shlinkio\Shlink\Core\Action\QrCodeAction;
16
use Shlinkio\Shlink\Core\Entity\ShortUrl;
17
use Shlinkio\Shlink\Core\Exception\ShortUrlNotFoundException;
18
use Shlinkio\Shlink\Core\Model\ShortUrlIdentifier;
19
use Shlinkio\Shlink\Core\Service\ShortUrl\ShortUrlResolverInterface;
20
21
class QrCodeActionTest extends TestCase
22
{
23
    private QrCodeAction $action;
24
    private ObjectProphecy $urlResolver;
25
26
    public function setUp(): void
27
    {
28
        $router = $this->prophesize(RouterInterface::class);
29
        $router->generateUri(Argument::cetera())->willReturn('/foo/bar');
30
31
        $this->urlResolver = $this->prophesize(ShortUrlResolverInterface::class);
32
33
        $this->action = new QrCodeAction($this->urlResolver->reveal(), ['domain' => 'doma.in']);
34
    }
35
36
    /** @test */
37
    public function aNotFoundShortCodeWillDelegateIntoNextMiddleware(): void
38
    {
39
        $shortCode = 'abc123';
40
        $this->urlResolver->resolveEnabledShortUrl(new ShortUrlIdentifier($shortCode, ''))
41
            ->willThrow(ShortUrlNotFoundException::class)
42
            ->shouldBeCalledOnce();
43
        $delegate = $this->prophesize(RequestHandlerInterface::class);
44
        $process = $delegate->handle(Argument::any())->willReturn(new Response());
45
46
        $this->action->process((new ServerRequest())->withAttribute('shortCode', $shortCode), $delegate->reveal());
47
48
        $process->shouldHaveBeenCalledOnce();
49
    }
50
51
    /** @test */
52
    public function anInvalidShortCodeWillReturnNotFoundResponse(): void
53
    {
54
        $shortCode = 'abc123';
55
        $this->urlResolver->resolveEnabledShortUrl(new ShortUrlIdentifier($shortCode, ''))
56
            ->willThrow(ShortUrlNotFoundException::class)
57
            ->shouldBeCalledOnce();
58
        $delegate = $this->prophesize(RequestHandlerInterface::class);
59
        $process = $delegate->handle(Argument::any())->willReturn(new Response());
60
61
        $this->action->process((new ServerRequest())->withAttribute('shortCode', $shortCode), $delegate->reveal());
62
63
        $process->shouldHaveBeenCalledOnce();
64
    }
65
66
    /** @test */
67
    public function aCorrectRequestReturnsTheQrCodeResponse(): void
68
    {
69
        $shortCode = 'abc123';
70
        $this->urlResolver->resolveEnabledShortUrl(new ShortUrlIdentifier($shortCode, ''))
71
            ->willReturn(new ShortUrl(''))
72
            ->shouldBeCalledOnce();
73
        $delegate = $this->prophesize(RequestHandlerInterface::class);
74
75
        $resp = $this->action->process(
76
            (new ServerRequest())->withAttribute('shortCode', $shortCode),
77
            $delegate->reveal(),
78
        );
79
80
        $this->assertInstanceOf(QrCodeResponse::class, $resp);
81
        $this->assertEquals(200, $resp->getStatusCode());
82
        $delegate->handle(Argument::any())->shouldHaveBeenCalledTimes(0);
83
    }
84
85
    /**
86
     * @test
87
     * @dataProvider provideQueries
88
     */
89
    public function imageIsReturnedWithExpectedContentTypeBasedOnProvidedFormat(
90
        array $query,
91
        string $expectedContentType
92
    ): void {
93
        $code = 'abc123';
94
        $this->urlResolver->resolveEnabledShortUrl(new ShortUrlIdentifier($code, ''))->willReturn(new ShortUrl(''));
95
        $delegate = $this->prophesize(RequestHandlerInterface::class);
96
        $req = (new ServerRequest())->withAttribute('shortCode', $code)->withQueryParams($query);
97
98
        $resp = $this->action->process($req, $delegate->reveal());
99
100
        $this->assertEquals($expectedContentType, $resp->getHeaderLine('Content-Type'));
101
    }
102
103
    public function provideQueries(): iterable
104
    {
105
        yield 'no format' => [[], 'image/png'];
106
        yield 'png format' => [['format' => 'png'], 'image/png'];
107
        yield 'svg format' => [['format' => 'svg'], 'image/svg+xml'];
108
        yield 'unsupported format' => [['format' => 'jpg'], 'image/png'];
109
    }
110
}
111