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,
|
|
|
|
string $expectedDetail
|
|
|
|
): void {
|
|
|
|
$resp = $this->callApiWithKey(self::METHOD_DELETE, $this->buildShortUrlPath($shortCode, $domain));
|
2019-11-27 22:48:35 +03:00
|
|
|
$payload = $this->getJsonResponsePayload($resp);
|
2019-11-20 22:38:19 +03:00
|
|
|
|
|
|
|
$this->assertEquals(self::STATUS_NOT_FOUND, $resp->getStatusCode());
|
2019-11-27 22:48:35 +03:00
|
|
|
$this->assertEquals(self::STATUS_NOT_FOUND, $payload['status']);
|
|
|
|
$this->assertEquals('INVALID_SHORTCODE', $payload['type']);
|
|
|
|
$this->assertEquals($expectedDetail, $payload['detail']);
|
|
|
|
$this->assertEquals('Short URL not found', $payload['title']);
|
2020-02-02 15:15:08 +03:00
|
|
|
$this->assertEquals($shortCode, $payload['shortCode']);
|
|
|
|
$this->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++) {
|
|
|
|
$this->assertEquals(self::STATUS_FOUND, $this->callShortUrl('abc123')->getStatusCode());
|
|
|
|
}
|
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
|
|
|
|
2019-11-25 01:56:02 +03:00
|
|
|
$this->assertEquals(self::STATUS_UNPROCESSABLE_ENTITY, $resp->getStatusCode());
|
2019-11-27 22:48:35 +03:00
|
|
|
$this->assertEquals(self::STATUS_UNPROCESSABLE_ENTITY, $payload['status']);
|
|
|
|
$this->assertEquals('INVALID_SHORTCODE_DELETION', $payload['type']);
|
|
|
|
$this->assertEquals($expectedDetail, $payload['detail']);
|
|
|
|
$this->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-02-02 14:58:26 +03:00
|
|
|
$this->assertEquals(self::STATUS_OK, $fetchWithDomainBefore->getStatusCode());
|
|
|
|
$this->assertEquals(self::STATUS_OK, $fetchWithoutDomainBefore->getStatusCode());
|
2020-02-02 12:28:10 +03:00
|
|
|
$this->assertEquals(self::STATUS_NO_CONTENT, $deleteResp->getStatusCode());
|
2020-02-02 14:58:26 +03:00
|
|
|
$this->assertEquals(self::STATUS_NOT_FOUND, $fetchWithDomainAfter->getStatusCode());
|
|
|
|
$this->assertEquals(self::STATUS_OK, $fetchWithoutDomainAfter->getStatusCode());
|
2020-02-02 12:28:10 +03:00
|
|
|
}
|
2019-11-20 22:38:19 +03:00
|
|
|
}
|