EditShortUrlActionTest::setUp()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
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