|
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
|
|
|
|