2019-11-20 22:38:19 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace ShlinkioApiTest\Shlink\Rest\Action;
|
|
|
|
|
|
|
|
use Shlinkio\Shlink\TestUtils\ApiTest\ApiTestCase;
|
2020-02-02 15:15:08 +03:00
|
|
|
use ShlinkioApiTest\Shlink\Rest\Utils\NotFoundUrlHelpersTrait;
|
2019-11-20 22:38:19 +03:00
|
|
|
|
|
|
|
class DeleteShortUrlActionTest extends ApiTestCase
|
|
|
|
{
|
2020-02-02 15:15:08 +03:00
|
|
|
use NotFoundUrlHelpersTrait;
|
2019-11-27 22:48:35 +03:00
|
|
|
|
2020-02-02 15:15:08 +03:00
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @dataProvider provideInvalidUrls
|
|
|
|
*/
|
|
|
|
public function notFoundErrorIsReturnWhenDeletingInvalidUrl(
|
|
|
|
string $shortCode,
|
|
|
|
?string $domain,
|
2021-01-10 11:02:05 +03:00
|
|
|
string $expectedDetail,
|
|
|
|
string $apiKey
|
2020-02-02 15:15:08 +03:00
|
|
|
): void {
|
2021-01-10 11:02:05 +03:00
|
|
|
$resp = $this->callApiWithKey(self::METHOD_DELETE, $this->buildShortUrlPath($shortCode, $domain), [], $apiKey);
|
2019-11-27 22:48:35 +03:00
|
|
|
$payload = $this->getJsonResponsePayload($resp);
|
2019-11-20 22:38:19 +03:00
|
|
|
|
2020-10-04 01:35:14 +03:00
|
|
|
self::assertEquals(self::STATUS_NOT_FOUND, $resp->getStatusCode());
|
|
|
|
self::assertEquals(self::STATUS_NOT_FOUND, $payload['status']);
|
|
|
|
self::assertEquals('INVALID_SHORTCODE', $payload['type']);
|
|
|
|
self::assertEquals($expectedDetail, $payload['detail']);
|
|
|
|
self::assertEquals('Short URL not found', $payload['title']);
|
|
|
|
self::assertEquals($shortCode, $payload['shortCode']);
|
|
|
|
self::assertEquals($domain, $payload['domain'] ?? null);
|
2019-11-20 22:38:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/** @test */
|
2019-11-25 01:56:02 +03:00
|
|
|
public function unprocessableEntityIsReturnedWhenTryingToDeleteUrlWithTooManyVisits(): void
|
2019-11-20 22:38:19 +03:00
|
|
|
{
|
|
|
|
// Generate visits first
|
|
|
|
for ($i = 0; $i < 20; $i++) {
|
2020-10-04 01:35:14 +03:00
|
|
|
self::assertEquals(self::STATUS_FOUND, $this->callShortUrl('abc123')->getStatusCode());
|
2019-11-20 22:38:19 +03:00
|
|
|
}
|
2019-11-27 22:48:35 +03:00
|
|
|
$expectedDetail = 'Impossible to delete short URL with short code "abc123" since it has more than "15" visits.';
|
2019-11-20 22:38:19 +03:00
|
|
|
|
|
|
|
$resp = $this->callApiWithKey(self::METHOD_DELETE, '/short-urls/abc123');
|
2019-11-27 22:48:35 +03:00
|
|
|
$payload = $this->getJsonResponsePayload($resp);
|
2019-11-20 22:38:19 +03:00
|
|
|
|
2020-10-04 01:35:14 +03:00
|
|
|
self::assertEquals(self::STATUS_UNPROCESSABLE_ENTITY, $resp->getStatusCode());
|
|
|
|
self::assertEquals(self::STATUS_UNPROCESSABLE_ENTITY, $payload['status']);
|
|
|
|
self::assertEquals('INVALID_SHORTCODE_DELETION', $payload['type']);
|
|
|
|
self::assertEquals($expectedDetail, $payload['detail']);
|
|
|
|
self::assertEquals('Cannot delete short URL', $payload['title']);
|
2019-11-20 22:38:19 +03:00
|
|
|
}
|
2020-02-02 12:28:10 +03:00
|
|
|
|
|
|
|
/** @test */
|
|
|
|
public function properShortUrlIsDeletedWhenDomainIsProvided(): void
|
|
|
|
{
|
2020-02-02 14:58:26 +03:00
|
|
|
$fetchWithDomainBefore = $this->callApiWithKey(self::METHOD_GET, '/short-urls/ghi789?domain=example.com');
|
|
|
|
$fetchWithoutDomainBefore = $this->callApiWithKey(self::METHOD_GET, '/short-urls/ghi789');
|
2020-02-02 12:28:10 +03:00
|
|
|
$deleteResp = $this->callApiWithKey(self::METHOD_DELETE, '/short-urls/ghi789?domain=example.com');
|
2020-02-02 14:58:26 +03:00
|
|
|
$fetchWithDomainAfter = $this->callApiWithKey(self::METHOD_GET, '/short-urls/ghi789?domain=example.com');
|
|
|
|
$fetchWithoutDomainAfter = $this->callApiWithKey(self::METHOD_GET, '/short-urls/ghi789');
|
2020-02-02 12:28:10 +03:00
|
|
|
|
2020-10-04 01:35:14 +03:00
|
|
|
self::assertEquals(self::STATUS_OK, $fetchWithDomainBefore->getStatusCode());
|
|
|
|
self::assertEquals(self::STATUS_OK, $fetchWithoutDomainBefore->getStatusCode());
|
|
|
|
self::assertEquals(self::STATUS_NO_CONTENT, $deleteResp->getStatusCode());
|
|
|
|
self::assertEquals(self::STATUS_NOT_FOUND, $fetchWithDomainAfter->getStatusCode());
|
|
|
|
self::assertEquals(self::STATUS_OK, $fetchWithoutDomainAfter->getStatusCode());
|
2020-02-02 12:28:10 +03:00
|
|
|
}
|
2019-11-20 22:38:19 +03:00
|
|
|
}
|