|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace ShlinkioTest\Shlink\Rest\Action\ShortUrl; |
|
6
|
|
|
|
|
7
|
|
|
use Laminas\Diactoros\ServerRequest; |
|
8
|
|
|
use PHPUnit\Framework\TestCase; |
|
9
|
|
|
use Prophecy\Argument; |
|
10
|
|
|
use Prophecy\PhpUnit\ProphecyTrait; |
|
11
|
|
|
use Prophecy\Prophecy\ObjectProphecy; |
|
12
|
|
|
use Shlinkio\Shlink\Core\Entity\ShortUrl; |
|
13
|
|
|
use Shlinkio\Shlink\Core\Exception\ValidationException; |
|
14
|
|
|
use Shlinkio\Shlink\Core\Service\ShortUrlServiceInterface; |
|
15
|
|
|
use Shlinkio\Shlink\Rest\Action\ShortUrl\EditShortUrlAction; |
|
16
|
|
|
|
|
17
|
|
|
class EditShortUrlActionTest extends TestCase |
|
18
|
|
|
{ |
|
19
|
|
|
use ProphecyTrait; |
|
20
|
|
|
|
|
21
|
|
|
private EditShortUrlAction $action; |
|
22
|
|
|
private ObjectProphecy $shortUrlService; |
|
23
|
|
|
|
|
24
|
|
|
public function setUp(): void |
|
25
|
|
|
{ |
|
26
|
|
|
$this->shortUrlService = $this->prophesize(ShortUrlServiceInterface::class); |
|
27
|
|
|
$this->action = new EditShortUrlAction($this->shortUrlService->reveal()); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** @test */ |
|
31
|
|
|
public function invalidDataThrowsError(): void |
|
32
|
|
|
{ |
|
33
|
|
|
$request = (new ServerRequest())->withParsedBody([ |
|
34
|
|
|
'maxVisits' => 'invalid', |
|
35
|
|
|
]); |
|
36
|
|
|
|
|
37
|
|
|
$this->expectException(ValidationException::class); |
|
38
|
|
|
|
|
39
|
|
|
$this->action->handle($request); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** @test */ |
|
43
|
|
|
public function correctShortCodeReturnsSuccess(): void |
|
44
|
|
|
{ |
|
45
|
|
|
$request = (new ServerRequest())->withAttribute('shortCode', 'abc123') |
|
46
|
|
|
->withParsedBody([ |
|
47
|
|
|
'maxVisits' => 5, |
|
48
|
|
|
]); |
|
49
|
|
|
$updateMeta = $this->shortUrlService->updateMetadataByShortCode(Argument::cetera())->willReturn( |
|
50
|
|
|
new ShortUrl(''), |
|
51
|
|
|
); |
|
52
|
|
|
|
|
53
|
|
|
$resp = $this->action->handle($request); |
|
54
|
|
|
|
|
55
|
|
|
self::assertEquals(204, $resp->getStatusCode()); |
|
56
|
|
|
$updateMeta->shouldHaveBeenCalled(); |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|