shlink/module/Rest/test-api/Action/DeleteShortUrlTest.php

72 lines
3 KiB
PHP
Raw Normal View History

<?php
declare(strict_types=1);
namespace ShlinkioApiTest\Shlink\Rest\Action;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Test;
use Shlinkio\Shlink\TestUtils\ApiTest\ApiTestCase;
use ShlinkioApiTest\Shlink\Rest\Utils\NotFoundUrlHelpersTrait;
2022-08-13 18:48:55 +03:00
use function sprintf;
2021-01-24 11:25:36 +03:00
class DeleteShortUrlTest extends ApiTestCase
{
use NotFoundUrlHelpersTrait;
#[Test, DataProvider('provideInvalidUrls')]
public function notFoundErrorIsReturnWhenDeletingInvalidUrl(
string $shortCode,
?string $domain,
string $expectedDetail,
string $apiKey,
): void {
$resp = $this->callApiWithKey(self::METHOD_DELETE, $this->buildShortUrlPath($shortCode, $domain), [], $apiKey);
$payload = $this->getJsonResponsePayload($resp);
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);
}
#[Test, DataProvider('provideApiVersions')]
2022-08-13 18:48:55 +03:00
public function expectedTypeIsReturnedBasedOnApiVersion(string $version, string $expectedType): void
{
$resp = $this->callApiWithKey(
self::METHOD_DELETE,
sprintf('/rest/v%s/short-urls/invalid-short-code', $version),
);
$payload = $this->getJsonResponsePayload($resp);
self::assertEquals($expectedType, $payload['type']);
}
2023-02-09 11:32:38 +03:00
public static function provideApiVersions(): iterable
2022-08-13 18:48:55 +03:00
{
yield ['1', 'INVALID_SHORTCODE'];
yield ['2', 'INVALID_SHORTCODE'];
yield ['3', 'https://shlink.io/api/error/short-url-not-found'];
}
#[Test]
public function properShortUrlIsDeletedWhenDomainIsProvided(): void
{
$fetchWithDomainBefore = $this->callApiWithKey(self::METHOD_GET, '/short-urls/ghi789?domain=example.com');
$fetchWithoutDomainBefore = $this->callApiWithKey(self::METHOD_GET, '/short-urls/ghi789');
$deleteResp = $this->callApiWithKey(self::METHOD_DELETE, '/short-urls/ghi789?domain=example.com');
$fetchWithDomainAfter = $this->callApiWithKey(self::METHOD_GET, '/short-urls/ghi789?domain=example.com');
$fetchWithoutDomainAfter = $this->callApiWithKey(self::METHOD_GET, '/short-urls/ghi789');
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());
}
}