Completed
Pull Request — master (#148)
by Alejandro
04:22
created

SingleStepCreateShortCodeActionTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 9
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 15
rs 9.4285
1
<?php
2
declare(strict_types=1);
3
4
namespace ShlinkioTest\Shlink\Rest\Action\ShortCode;
5
6
use PHPUnit\Framework\Assert;
7
use PHPUnit\Framework\TestCase;
8
use Prophecy\Argument;
9
use Prophecy\Prophecy\ObjectProphecy;
10
use Psr\Http\Message\UriInterface;
11
use Shlinkio\Shlink\Core\Service\UrlShortenerInterface;
12
use Shlinkio\Shlink\Rest\Action\ShortCode\SingleStepCreateShortCodeAction;
13
use Shlinkio\Shlink\Rest\Entity\ApiKey;
14
use Shlinkio\Shlink\Rest\Service\ApiKeyServiceInterface;
15
use Zend\Diactoros\Response\JsonResponse;
16
use Zend\Diactoros\ServerRequestFactory;
17
use Zend\I18n\Translator\Translator;
18
19
class SingleStepCreateShortCodeActionTest extends TestCase
20
{
21
    /**
22
     * @var SingleStepCreateShortCodeAction
23
     */
24
    private $action;
25
    /**
26
     * @var ObjectProphecy
27
     */
28
    private $urlShortener;
29
    /**
30
     * @var ObjectProphecy
31
     */
32
    private $apiKeyService;
33
34
    public function setUp()
35
    {
36
        $this->urlShortener = $this->prophesize(UrlShortenerInterface::class);
37
        $this->apiKeyService = $this->prophesize(ApiKeyServiceInterface::class);
38
39
        $this->action = new SingleStepCreateShortCodeAction(
40
            $this->urlShortener->reveal(),
41
            Translator::factory([]),
42
            $this->apiKeyService->reveal(),
43
            [
44
                'schema' => 'http',
45
                'hostname' => 'foo.com',
46
            ]
47
        );
48
    }
49
50
    /**
51
     * @test
52
     * @dataProvider provideInvalidApiKeys
53
     */
54
    public function errorResponseIsReturnedIfInvalidApiKeyIsProvided(?ApiKey $apiKey)
55
    {
56
        $request = ServerRequestFactory::fromGlobals()->withQueryParams(['apiKey' => 'abc123']);
57
        $findApiKey = $this->apiKeyService->getByKey('abc123')->willReturn($apiKey);
58
59
        /** @var JsonResponse $resp */
60
        $resp = $this->action->handle($request);
61
        $payload = $resp->getPayload();
62
63
        $this->assertEquals(400, $resp->getStatusCode());
64
        $this->assertEquals('INVALID_ARGUMENT', $payload['error']);
65
        $this->assertEquals('No API key was provided or it is not valid', $payload['message']);
66
        $findApiKey->shouldHaveBeenCalled();
67
    }
68
69
    public function provideInvalidApiKeys(): array
70
    {
71
        return [
72
            [null],
73
            [(new ApiKey())->disable()],
74
        ];
75
    }
76
77
    /**
78
     * @test
79
     */
80
    public function errorResponseIsReturnedIfNoUrlIsProvided()
81
    {
82
        $request = ServerRequestFactory::fromGlobals()->withQueryParams(['apiKey' => 'abc123']);
83
        $findApiKey = $this->apiKeyService->getByKey('abc123')->willReturn(new ApiKey());
84
85
        /** @var JsonResponse $resp */
86
        $resp = $this->action->handle($request);
87
        $payload = $resp->getPayload();
88
89
        $this->assertEquals(400, $resp->getStatusCode());
90
        $this->assertEquals('INVALID_ARGUMENT', $payload['error']);
91
        $this->assertEquals('A URL was not provided', $payload['message']);
92
        $findApiKey->shouldHaveBeenCalled();
93
    }
94
95
    /**
96
     * @test
97
     */
98
    public function properDataIsPassedWhenGeneratingShortCode()
99
    {
100
        $request = ServerRequestFactory::fromGlobals()->withQueryParams([
101
            'apiKey' => 'abc123',
102
            'longUrl' => 'http://foobar.com',
103
        ]);
104
        $findApiKey = $this->apiKeyService->getByKey('abc123')->willReturn(new ApiKey());
105
        $generateShortCode = $this->urlShortener->urlToShortCode(
106
            Argument::that(function (UriInterface $argument) {
107
                Assert::assertEquals('http://foobar.com', (string) $argument);
108
                return $argument;
109
            }),
110
            [],
111
            null,
112
            null,
113
            null,
114
            null
115
        );
116
117
        $resp = $this->action->handle($request);
118
119
        $this->assertEquals(200, $resp->getStatusCode());
120
        $findApiKey->shouldHaveBeenCalled();
121
        $generateShortCode->shouldHaveBeenCalled();
122
    }
123
}
124