2019-11-20 20:38:19 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace ShlinkioApiTest\Shlink\Rest\Action;
|
|
|
|
|
2023-02-09 20:42:18 +01:00
|
|
|
use PHPUnit\Framework\Attributes\DataProvider;
|
2023-06-18 10:36:45 +02:00
|
|
|
use PHPUnit\Framework\Attributes\DataProviderExternal;
|
2023-02-09 20:42:18 +01:00
|
|
|
use PHPUnit\Framework\Attributes\Test;
|
2019-11-20 20:38:19 +01:00
|
|
|
use Shlinkio\Shlink\TestUtils\ApiTest\ApiTestCase;
|
2023-06-18 10:36:45 +02:00
|
|
|
use ShlinkioApiTest\Shlink\Rest\Utils\ApiTestDataProviders;
|
|
|
|
use ShlinkioApiTest\Shlink\Rest\Utils\UrlBuilder;
|
2019-11-20 20:38:19 +01:00
|
|
|
|
2022-08-13 17:48:55 +02:00
|
|
|
use function sprintf;
|
|
|
|
|
2021-01-24 09:25:36 +01:00
|
|
|
class DeleteShortUrlTest extends ApiTestCase
|
2019-11-20 20:38:19 +01:00
|
|
|
{
|
2023-06-18 10:36:45 +02:00
|
|
|
#[Test, DataProviderExternal(ApiTestDataProviders::class, 'invalidUrlsProvider')]
|
2020-02-02 13:15:08 +01:00
|
|
|
public function notFoundErrorIsReturnWhenDeletingInvalidUrl(
|
|
|
|
string $shortCode,
|
|
|
|
?string $domain,
|
2021-01-10 09:02:05 +01:00
|
|
|
string $expectedDetail,
|
2021-05-23 12:31:10 +02:00
|
|
|
string $apiKey,
|
2020-02-02 13:15:08 +01:00
|
|
|
): void {
|
2023-06-18 10:36:45 +02:00
|
|
|
$resp = $this->callApiWithKey(
|
|
|
|
self::METHOD_DELETE,
|
|
|
|
UrlBuilder::buildShortUrlPath($shortCode, $domain),
|
|
|
|
apiKey: $apiKey,
|
|
|
|
);
|
2019-11-27 20:48:35 +01:00
|
|
|
$payload = $this->getJsonResponsePayload($resp);
|
2019-11-20 20:38:19 +01:00
|
|
|
|
2020-10-04 00:35:14 +02:00
|
|
|
self::assertEquals(self::STATUS_NOT_FOUND, $resp->getStatusCode());
|
|
|
|
self::assertEquals(self::STATUS_NOT_FOUND, $payload['status']);
|
2024-02-12 20:29:40 +01:00
|
|
|
self::assertEquals('https://shlink.io/api/error/short-url-not-found', $payload['type']);
|
2020-10-04 00:35:14 +02:00
|
|
|
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 20:38:19 +01:00
|
|
|
}
|
|
|
|
|
2023-02-09 20:42:18 +01:00
|
|
|
#[Test, DataProvider('provideApiVersions')]
|
2022-08-13 17:48:55 +02: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 09:32:38 +01:00
|
|
|
public static function provideApiVersions(): iterable
|
2022-08-13 17:48:55 +02:00
|
|
|
{
|
2024-02-12 20:29:40 +01:00
|
|
|
yield ['1', 'https://shlink.io/api/error/short-url-not-found'];
|
|
|
|
yield ['2', 'https://shlink.io/api/error/short-url-not-found'];
|
2022-08-13 17:48:55 +02:00
|
|
|
yield ['3', 'https://shlink.io/api/error/short-url-not-found'];
|
|
|
|
}
|
|
|
|
|
2023-02-09 20:42:18 +01:00
|
|
|
#[Test]
|
2020-02-02 10:28:10 +01:00
|
|
|
public function properShortUrlIsDeletedWhenDomainIsProvided(): void
|
|
|
|
{
|
2020-02-02 12:58:26 +01: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 10:28:10 +01:00
|
|
|
$deleteResp = $this->callApiWithKey(self::METHOD_DELETE, '/short-urls/ghi789?domain=example.com');
|
2020-02-02 12:58:26 +01: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 10:28:10 +01:00
|
|
|
|
2020-10-04 00:35:14 +02: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 10:28:10 +01:00
|
|
|
}
|
2019-11-20 20:38:19 +01:00
|
|
|
}
|