|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace ShlinkioTest\Shlink\Rest\Action\ShortCode; |
|
5
|
|
|
|
|
6
|
|
|
use PHPUnit\Framework\TestCase; |
|
7
|
|
|
use Prophecy\Argument; |
|
8
|
|
|
use Prophecy\Prophecy\ObjectProphecy; |
|
9
|
|
|
use Shlinkio\Shlink\Core\Entity\ShortUrl; |
|
10
|
|
|
use Shlinkio\Shlink\Core\Exception\InvalidShortCodeException; |
|
11
|
|
|
use Shlinkio\Shlink\Core\Service\ShortUrlServiceInterface; |
|
12
|
|
|
use Shlinkio\Shlink\Rest\Action\ShortCode\EditShortCodeAction; |
|
13
|
|
|
use Shlinkio\Shlink\Rest\Util\RestUtils; |
|
14
|
|
|
use Zend\Diactoros\Response\JsonResponse; |
|
15
|
|
|
use Zend\Diactoros\ServerRequestFactory; |
|
16
|
|
|
use Zend\I18n\Translator\Translator; |
|
17
|
|
|
|
|
18
|
|
|
class EditShortCodeActionTest extends TestCase |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @var EditShortCodeAction |
|
22
|
|
|
*/ |
|
23
|
|
|
private $action; |
|
24
|
|
|
/** |
|
25
|
|
|
* @var ObjectProphecy |
|
26
|
|
|
*/ |
|
27
|
|
|
private $shortUrlService; |
|
28
|
|
|
|
|
29
|
|
|
public function setUp() |
|
30
|
|
|
{ |
|
31
|
|
|
$this->shortUrlService = $this->prophesize(ShortUrlServiceInterface::class); |
|
32
|
|
|
$this->action = new EditShortCodeAction($this->shortUrlService->reveal(), Translator::factory([])); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @test |
|
37
|
|
|
*/ |
|
38
|
|
|
public function invalidDataReturnsError() |
|
39
|
|
|
{ |
|
40
|
|
|
$request = ServerRequestFactory::fromGlobals()->withParsedBody([ |
|
41
|
|
|
'maxVisits' => 'invalid', |
|
42
|
|
|
]); |
|
43
|
|
|
|
|
44
|
|
|
/** @var JsonResponse $resp */ |
|
45
|
|
|
$resp = $this->action->handle($request); |
|
46
|
|
|
$payload = $resp->getPayload(); |
|
47
|
|
|
|
|
48
|
|
|
$this->assertEquals(400, $resp->getStatusCode()); |
|
49
|
|
|
$this->assertEquals(RestUtils::INVALID_ARGUMENT_ERROR, $payload['error']); |
|
50
|
|
|
$this->assertEquals('Provided data is invalid.', $payload['message']); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @test |
|
55
|
|
|
*/ |
|
56
|
|
|
public function incorrectShortCodeReturnsError() |
|
57
|
|
|
{ |
|
58
|
|
|
$request = ServerRequestFactory::fromGlobals()->withAttribute('shortCode', 'abc123') |
|
59
|
|
|
->withParsedBody([ |
|
60
|
|
|
'maxVisits' => 5, |
|
61
|
|
|
]); |
|
62
|
|
|
$updateMeta = $this->shortUrlService->updateMetadataByShortCode(Argument::cetera())->willThrow( |
|
63
|
|
|
InvalidShortCodeException::class |
|
64
|
|
|
); |
|
65
|
|
|
|
|
66
|
|
|
/** @var JsonResponse $resp */ |
|
67
|
|
|
$resp = $this->action->handle($request); |
|
68
|
|
|
$payload = $resp->getPayload(); |
|
69
|
|
|
|
|
70
|
|
|
$this->assertEquals(404, $resp->getStatusCode()); |
|
71
|
|
|
$this->assertEquals(RestUtils::INVALID_SHORTCODE_ERROR, $payload['error']); |
|
72
|
|
|
$this->assertEquals('No URL found for short code "abc123"', $payload['message']); |
|
73
|
|
|
$updateMeta->shouldHaveBeenCalled(); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @test |
|
78
|
|
|
*/ |
|
79
|
|
|
public function correctShortCodeReturnsSuccess() |
|
80
|
|
|
{ |
|
81
|
|
|
$request = ServerRequestFactory::fromGlobals()->withAttribute('shortCode', 'abc123') |
|
82
|
|
|
->withParsedBody([ |
|
83
|
|
|
'maxVisits' => 5, |
|
84
|
|
|
]); |
|
85
|
|
|
$updateMeta = $this->shortUrlService->updateMetadataByShortCode(Argument::cetera())->willReturn(new ShortUrl()); |
|
86
|
|
|
|
|
87
|
|
|
$resp = $this->action->handle($request); |
|
88
|
|
|
|
|
89
|
|
|
$this->assertEquals(204, $resp->getStatusCode()); |
|
90
|
|
|
$updateMeta->shouldHaveBeenCalled(); |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|