2021-01-21 21:43:34 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace ShlinkioApiTest\Shlink\Rest\Action;
|
|
|
|
|
|
|
|
use GuzzleHttp\RequestOptions;
|
2023-02-09 22:42:18 +03:00
|
|
|
use PHPUnit\Framework\Attributes\DataProvider;
|
|
|
|
use PHPUnit\Framework\Attributes\Test;
|
2021-01-21 21:43:34 +03:00
|
|
|
use Psr\Http\Message\ResponseInterface;
|
|
|
|
use Shlinkio\Shlink\TestUtils\ApiTest\ApiTestCase;
|
|
|
|
|
|
|
|
class SingleStepCreateShortUrlTest extends ApiTestCase
|
|
|
|
{
|
2023-02-09 22:42:18 +03:00
|
|
|
#[Test, DataProvider('provideFormats')]
|
2021-01-21 21:43:34 +03:00
|
|
|
public function createsNewShortUrlWithExpectedResponse(?string $format, string $expectedContentType): void
|
|
|
|
{
|
|
|
|
$resp = $this->createShortUrl($format, 'valid_api_key');
|
|
|
|
|
|
|
|
self::assertEquals(self::STATUS_OK, $resp->getStatusCode());
|
|
|
|
self::assertEquals($expectedContentType, $resp->getHeaderLine('Content-Type'));
|
|
|
|
}
|
|
|
|
|
2023-02-09 11:32:38 +03:00
|
|
|
public static function provideFormats(): iterable
|
2021-01-21 21:43:34 +03:00
|
|
|
{
|
|
|
|
yield 'txt format' => ['txt', 'text/plain'];
|
|
|
|
yield 'json format' => ['json', 'application/json'];
|
|
|
|
yield '<empty> format' => [null, 'application/json'];
|
|
|
|
}
|
|
|
|
|
2023-02-09 22:42:18 +03:00
|
|
|
#[Test]
|
2021-01-21 21:43:34 +03:00
|
|
|
public function authorizationErrorIsReturnedIfNoApiKeyIsSent(): void
|
|
|
|
{
|
|
|
|
$expectedDetail = 'Expected authentication to be provided in "apiKey" query param';
|
|
|
|
|
|
|
|
$resp = $this->createShortUrl();
|
|
|
|
$payload = $this->getJsonResponsePayload($resp);
|
|
|
|
|
|
|
|
self::assertEquals(self::STATUS_UNAUTHORIZED, $resp->getStatusCode());
|
|
|
|
self::assertEquals(self::STATUS_UNAUTHORIZED, $payload['status']);
|
|
|
|
self::assertEquals('INVALID_AUTHORIZATION', $payload['type']);
|
|
|
|
self::assertEquals($expectedDetail, $payload['detail']);
|
|
|
|
self::assertEquals('Invalid authorization', $payload['title']);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function createShortUrl(?string $format = 'json', ?string $apiKey = null): ResponseInterface
|
|
|
|
{
|
|
|
|
$query = [
|
|
|
|
'longUrl' => 'https://app.shlink.io',
|
|
|
|
'apiKey' => $apiKey,
|
|
|
|
'format' => $format,
|
|
|
|
];
|
|
|
|
return $this->callApi(self::METHOD_GET, '/short-urls/shorten', [RequestOptions::QUERY => $query]);
|
|
|
|
}
|
|
|
|
}
|