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

DeleteShortUrlActionTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 5 1
A emptyResponseIsReturnedIfProperlyDeleted() 0 10 1
A returnsErrorResponseInCaseOfException() 0 12 1
A provideExceptions() 0 7 1
1
<?php
2
declare(strict_types=1);
3
4
namespace ShlinkioTest\Shlink\Rest\Action\ShortUrl;
5
6
use PHPUnit\Framework\TestCase;
7
use Prophecy\Argument;
8
use Prophecy\Prophecy\ObjectProphecy;
9
use Shlinkio\Shlink\Core\Exception;
10
use Shlinkio\Shlink\Core\Service\ShortUrl\DeleteShortUrlServiceInterface;
11
use Shlinkio\Shlink\Rest\Action\ShortUrl\DeleteShortUrlAction;
12
use Shlinkio\Shlink\Rest\Util\RestUtils;
13
use Zend\Diactoros\Response\JsonResponse;
14
use Zend\Diactoros\ServerRequestFactory;
15
use Zend\I18n\Translator\Translator;
16
17
class DeleteShortUrlActionTest extends TestCase
18
{
19
    /**
20
     * @var DeleteShortUrlAction
21
     */
22
    private $action;
23
    /**
24
     * @var ObjectProphecy
25
     */
26
    private $service;
27
28
    public function setUp()
29
    {
30
        $this->service = $this->prophesize(DeleteShortUrlServiceInterface::class);
31
        $this->action = new DeleteShortUrlAction($this->service->reveal(), Translator::factory([]));
32
    }
33
34
    /**
35
     * @test
36
     */
37
    public function emptyResponseIsReturnedIfProperlyDeleted()
38
    {
39
        $deleteByShortCode = $this->service->deleteByShortCode(Argument::any())->will(function () {
40
        });
41
42
        $resp = $this->action->handle(ServerRequestFactory::fromGlobals());
43
44
        $this->assertEquals(204, $resp->getStatusCode());
45
        $deleteByShortCode->shouldHaveBeenCalledTimes(1);
46
    }
47
48
    /**
49
     * @test
50
     * @dataProvider provideExceptions
51
     */
52
    public function returnsErrorResponseInCaseOfException(\Throwable $e, string $error, int $statusCode)
53
    {
54
        $deleteByShortCode = $this->service->deleteByShortCode(Argument::any())->willThrow($e);
55
56
        /** @var JsonResponse $resp */
57
        $resp = $this->action->handle(ServerRequestFactory::fromGlobals());
58
        $payload = $resp->getPayload();
59
60
        $this->assertEquals($statusCode, $resp->getStatusCode());
61
        $this->assertEquals($error, $payload['error']);
62
        $deleteByShortCode->shouldHaveBeenCalledTimes(1);
63
    }
64
65
    public function provideExceptions(): array
66
    {
67
        return [
68
            [new Exception\InvalidShortCodeException(), RestUtils::INVALID_SHORTCODE_ERROR, 404],
69
            [new Exception\DeleteShortUrlException(5), RestUtils::INVALID_SHORTCODE_DELETION_ERROR, 400],
70
        ];
71
    }
72
}
73