Completed
Pull Request — master (#213)
by Alejandro
04:40
created

SingleStepCreateShortUrlActionTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 105
rs 10
c 0
b 0
f 0
wmc 5
lcom 1
cbo 3

5 Methods

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