Completed
Pull Request — develop (#645)
by Alejandro
05:15
created

properShortUrlIsDeletedWhenDomainIsProvided()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 10
c 2
b 0
f 0
nc 1
nop 0
dl 0
loc 13
rs 9.9332
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ShlinkioApiTest\Shlink\Rest\Action;
6
7
use Shlinkio\Shlink\TestUtils\ApiTest\ApiTestCase;
8
use ShlinkioApiTest\Shlink\Rest\Utils\NotFoundUrlHelpersTrait;
9
10
class DeleteShortUrlActionTest extends ApiTestCase
11
{
12
    use NotFoundUrlHelpersTrait;
13
14
    /**
15
     * @test
16
     * @dataProvider provideInvalidUrls
17
     */
18
    public function notFoundErrorIsReturnWhenDeletingInvalidUrl(
19
        string $shortCode,
20
        ?string $domain,
21
        string $expectedDetail
22
    ): void {
23
        $resp = $this->callApiWithKey(self::METHOD_DELETE, $this->buildShortUrlPath($shortCode, $domain));
24
        $payload = $this->getJsonResponsePayload($resp);
25
26
        $this->assertEquals(self::STATUS_NOT_FOUND, $resp->getStatusCode());
27
        $this->assertEquals(self::STATUS_NOT_FOUND, $payload['status']);
28
        $this->assertEquals('INVALID_SHORTCODE', $payload['type']);
29
        $this->assertEquals($expectedDetail, $payload['detail']);
30
        $this->assertEquals('Short URL not found', $payload['title']);
31
        $this->assertEquals($shortCode, $payload['shortCode']);
32
        $this->assertEquals($domain, $payload['domain'] ?? null);
33
    }
34
35
    /** @test */
36
    public function unprocessableEntityIsReturnedWhenTryingToDeleteUrlWithTooManyVisits(): void
37
    {
38
        // Generate visits first
39
        for ($i = 0; $i < 20; $i++) {
40
            $this->assertEquals(self::STATUS_FOUND, $this->callShortUrl('abc123')->getStatusCode());
41
        }
42
        $expectedDetail = 'Impossible to delete short URL with short code "abc123" since it has more than "15" visits.';
43
44
        $resp = $this->callApiWithKey(self::METHOD_DELETE, '/short-urls/abc123');
45
        $payload = $this->getJsonResponsePayload($resp);
46
47
        $this->assertEquals(self::STATUS_UNPROCESSABLE_ENTITY, $resp->getStatusCode());
48
        $this->assertEquals(self::STATUS_UNPROCESSABLE_ENTITY, $payload['status']);
49
        $this->assertEquals('INVALID_SHORTCODE_DELETION', $payload['type']);
50
        $this->assertEquals($expectedDetail, $payload['detail']);
51
        $this->assertEquals('Cannot delete short URL', $payload['title']);
52
    }
53
54
    /** @test */
55
    public function properShortUrlIsDeletedWhenDomainIsProvided(): void
56
    {
57
        $fetchWithDomainBefore = $this->callApiWithKey(self::METHOD_GET, '/short-urls/ghi789?domain=example.com');
58
        $fetchWithoutDomainBefore = $this->callApiWithKey(self::METHOD_GET, '/short-urls/ghi789');
59
        $deleteResp = $this->callApiWithKey(self::METHOD_DELETE, '/short-urls/ghi789?domain=example.com');
60
        $fetchWithDomainAfter = $this->callApiWithKey(self::METHOD_GET, '/short-urls/ghi789?domain=example.com');
61
        $fetchWithoutDomainAfter = $this->callApiWithKey(self::METHOD_GET, '/short-urls/ghi789');
62
63
        $this->assertEquals(self::STATUS_OK, $fetchWithDomainBefore->getStatusCode());
64
        $this->assertEquals(self::STATUS_OK, $fetchWithoutDomainBefore->getStatusCode());
65
        $this->assertEquals(self::STATUS_NO_CONTENT, $deleteResp->getStatusCode());
66
        $this->assertEquals(self::STATUS_NOT_FOUND, $fetchWithDomainAfter->getStatusCode());
67
        $this->assertEquals(self::STATUS_OK, $fetchWithoutDomainAfter->getStatusCode());
68
    }
69
}
70