mirror of
https://github.com/shlinkio/shlink.git
synced 2025-02-25 18:45:27 -06:00
Removed deprecated features
This commit is contained in:
@@ -7,14 +7,12 @@ namespace ShlinkioTest\Shlink\Rest\Action\Tag;
|
||||
use Laminas\Diactoros\Response\JsonResponse;
|
||||
use Laminas\Diactoros\ServerRequestFactory;
|
||||
use Pagerfanta\Adapter\ArrayAdapter;
|
||||
use PHPUnit\Framework\Attributes\DataProvider;
|
||||
use PHPUnit\Framework\Attributes\Test;
|
||||
use PHPUnit\Framework\MockObject\MockObject;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Shlinkio\Shlink\Common\Paginator\Paginator;
|
||||
use Shlinkio\Shlink\Core\Tag\Entity\Tag;
|
||||
use Shlinkio\Shlink\Core\Tag\Model\TagInfo;
|
||||
use Shlinkio\Shlink\Core\Tag\TagServiceInterface;
|
||||
use Shlinkio\Shlink\Rest\Action\Tag\ListTagsAction;
|
||||
use Shlinkio\Shlink\Rest\Entity\ApiKey;
|
||||
@@ -32,8 +30,8 @@ class ListTagsActionTest extends TestCase
|
||||
$this->action = new ListTagsAction($this->tagService);
|
||||
}
|
||||
|
||||
#[Test, DataProvider('provideNoStatsQueries')]
|
||||
public function returnsBaseDataWhenStatsAreNotRequested(array $query): void
|
||||
#[Test]
|
||||
public function returnsBaseDataWhenStatsAreNotRequested(): void
|
||||
{
|
||||
$tags = [new Tag('foo'), new Tag('bar')];
|
||||
$tagsCount = count($tags);
|
||||
@@ -43,7 +41,7 @@ class ListTagsActionTest extends TestCase
|
||||
)->willReturn(new Paginator(new ArrayAdapter($tags)));
|
||||
|
||||
/** @var JsonResponse $resp */
|
||||
$resp = $this->action->handle($this->requestWithApiKey()->withQueryParams($query));
|
||||
$resp = $this->action->handle($this->requestWithApiKey());
|
||||
$payload = $resp->getPayload();
|
||||
|
||||
self::assertEquals([
|
||||
@@ -60,46 +58,6 @@ class ListTagsActionTest extends TestCase
|
||||
], $payload);
|
||||
}
|
||||
|
||||
public static function provideNoStatsQueries(): iterable
|
||||
{
|
||||
yield 'no query' => [[]];
|
||||
yield 'withStats is false' => [['withStats' => 'withStats']];
|
||||
yield 'withStats is something else' => [['withStats' => 'foo']];
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function returnsStatsWhenRequested(): void
|
||||
{
|
||||
$stats = [
|
||||
new TagInfo('foo', 1, 1),
|
||||
new TagInfo('bar', 3, 10),
|
||||
];
|
||||
$itemsCount = count($stats);
|
||||
$this->tagService->expects($this->once())->method('tagsInfo')->with(
|
||||
$this->anything(),
|
||||
$this->isInstanceOf(ApiKey::class),
|
||||
)->willReturn(new Paginator(new ArrayAdapter($stats)));
|
||||
$req = $this->requestWithApiKey()->withQueryParams(['withStats' => 'true']);
|
||||
|
||||
/** @var JsonResponse $resp */
|
||||
$resp = $this->action->handle($req);
|
||||
$payload = $resp->getPayload();
|
||||
|
||||
self::assertEquals([
|
||||
'tags' => [
|
||||
'data' => ['foo', 'bar'],
|
||||
'stats' => $stats,
|
||||
'pagination' => [
|
||||
'currentPage' => 1,
|
||||
'pagesCount' => 1,
|
||||
'itemsPerPage' => 10,
|
||||
'itemsInCurrentPage' => $itemsCount,
|
||||
'totalItems' => $itemsCount,
|
||||
],
|
||||
],
|
||||
], $payload);
|
||||
}
|
||||
|
||||
private function requestWithApiKey(): ServerRequestInterface
|
||||
{
|
||||
return ServerRequestFactory::fromGlobals()->withAttribute(ApiKey::class, ApiKey::create());
|
||||
|
||||
@@ -1,113 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ShlinkioTest\Shlink\Rest\Exception;
|
||||
|
||||
use Exception;
|
||||
use Mezzio\ProblemDetails\Exception\ProblemDetailsExceptionInterface;
|
||||
use PHPUnit\Framework\Attributes\DataProvider;
|
||||
use PHPUnit\Framework\Attributes\Test;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Shlinkio\Shlink\Core\Exception\DeleteShortUrlException;
|
||||
use Shlinkio\Shlink\Core\Exception\DomainNotFoundException;
|
||||
use Shlinkio\Shlink\Core\Exception\ForbiddenTagOperationException;
|
||||
use Shlinkio\Shlink\Core\Exception\InvalidUrlException;
|
||||
use Shlinkio\Shlink\Core\Exception\NonUniqueSlugException;
|
||||
use Shlinkio\Shlink\Core\Exception\ShortUrlNotFoundException;
|
||||
use Shlinkio\Shlink\Core\Exception\TagConflictException;
|
||||
use Shlinkio\Shlink\Core\Exception\TagNotFoundException;
|
||||
use Shlinkio\Shlink\Core\Exception\ValidationException;
|
||||
use Shlinkio\Shlink\Rest\Exception\BackwardsCompatibleProblemDetailsException;
|
||||
use Shlinkio\Shlink\Rest\Exception\MercureException;
|
||||
use Shlinkio\Shlink\Rest\Exception\MissingAuthenticationException;
|
||||
use Shlinkio\Shlink\Rest\Exception\VerifyAuthenticationException;
|
||||
|
||||
class BackwardsCompatibleProblemDetailsExceptionTest extends TestCase
|
||||
{
|
||||
#[Test, DataProvider('provideTypes')]
|
||||
public function typeIsRemappedOnWrappedException(
|
||||
string $wrappedType,
|
||||
string $expectedType,
|
||||
bool $expectSameType = false,
|
||||
): void {
|
||||
$original = new class ($wrappedType) extends Exception implements ProblemDetailsExceptionInterface {
|
||||
public function __construct(private readonly string $type)
|
||||
{
|
||||
parent::__construct('');
|
||||
}
|
||||
|
||||
public function getStatus(): int
|
||||
{
|
||||
return 123;
|
||||
}
|
||||
|
||||
public function getType(): string
|
||||
{
|
||||
return $this->type;
|
||||
}
|
||||
|
||||
public function getTitle(): string
|
||||
{
|
||||
return 'title';
|
||||
}
|
||||
|
||||
public function getDetail(): string
|
||||
{
|
||||
return 'detail';
|
||||
}
|
||||
|
||||
public function getAdditionalData(): array
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
public function toArray(): array
|
||||
{
|
||||
return ['type' => $this->type];
|
||||
}
|
||||
|
||||
public function jsonSerialize(): array
|
||||
{
|
||||
return ['type' => $this->type];
|
||||
}
|
||||
};
|
||||
$e = BackwardsCompatibleProblemDetailsException::fromProblemDetails($original);
|
||||
|
||||
self::assertEquals($e->getType(), $expectedType);
|
||||
self::assertEquals($e->toArray(), ['type' => $expectedType]);
|
||||
self::assertEquals($e->jsonSerialize(), ['type' => $expectedType]);
|
||||
|
||||
self::assertEquals($original->getTitle(), $e->getTitle());
|
||||
self::assertEquals($original->getDetail(), $e->getDetail());
|
||||
self::assertEquals($original->getAdditionalData(), $e->getAdditionalData());
|
||||
|
||||
if ($expectSameType) {
|
||||
self::assertEquals($original->getType(), $e->getType());
|
||||
self::assertEquals($original->toArray(), $e->toArray());
|
||||
self::assertEquals($original->jsonSerialize(), $e->jsonSerialize());
|
||||
} else {
|
||||
self::assertNotEquals($original->getType(), $e->getType());
|
||||
self::assertNotEquals($original->toArray(), $e->toArray());
|
||||
self::assertNotEquals($original->jsonSerialize(), $e->jsonSerialize());
|
||||
}
|
||||
}
|
||||
|
||||
public static function provideTypes(): iterable
|
||||
{
|
||||
yield ['foo', 'foo', true];
|
||||
yield ['bar', 'bar', true];
|
||||
yield [ValidationException::ERROR_CODE, 'INVALID_ARGUMENT'];
|
||||
yield [DeleteShortUrlException::ERROR_CODE, 'INVALID_SHORT_URL_DELETION'];
|
||||
yield [DomainNotFoundException::ERROR_CODE, 'DOMAIN_NOT_FOUND'];
|
||||
yield [ForbiddenTagOperationException::ERROR_CODE, 'FORBIDDEN_OPERATION'];
|
||||
yield [InvalidUrlException::ERROR_CODE, 'INVALID_URL'];
|
||||
yield [NonUniqueSlugException::ERROR_CODE, 'INVALID_SLUG'];
|
||||
yield [ShortUrlNotFoundException::ERROR_CODE, 'INVALID_SHORTCODE'];
|
||||
yield [TagConflictException::ERROR_CODE, 'TAG_CONFLICT'];
|
||||
yield [TagNotFoundException::ERROR_CODE, 'TAG_NOT_FOUND'];
|
||||
yield [MercureException::ERROR_CODE, 'MERCURE_NOT_CONFIGURED'];
|
||||
yield [MissingAuthenticationException::ERROR_CODE, 'INVALID_AUTHORIZATION'];
|
||||
yield [VerifyAuthenticationException::ERROR_CODE, 'INVALID_API_KEY'];
|
||||
}
|
||||
}
|
||||
@@ -1,74 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ShlinkioTest\Shlink\Rest\Middleware\ErrorHandler;
|
||||
|
||||
use Laminas\Diactoros\ServerRequestFactory;
|
||||
use PHPUnit\Framework\Attributes\DataProvider;
|
||||
use PHPUnit\Framework\Attributes\Test;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Psr\Http\Server\RequestHandlerInterface;
|
||||
use Shlinkio\Shlink\Core\Exception\ValidationException;
|
||||
use Shlinkio\Shlink\Rest\Exception\BackwardsCompatibleProblemDetailsException;
|
||||
use Shlinkio\Shlink\Rest\Middleware\ErrorHandler\BackwardsCompatibleProblemDetailsHandler;
|
||||
use Throwable;
|
||||
|
||||
class BackwardsCompatibleProblemDetailsHandlerTest extends TestCase
|
||||
{
|
||||
private BackwardsCompatibleProblemDetailsHandler $handler;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->handler = new BackwardsCompatibleProblemDetailsHandler();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param class-string<Throwable> $expectedException
|
||||
*/
|
||||
#[Test, DataProvider('provideExceptions')]
|
||||
public function expectedExceptionIsThrownBasedOnTheRequestVersion(
|
||||
ServerRequestInterface $request,
|
||||
Throwable $thrownException,
|
||||
string $expectedException,
|
||||
): void {
|
||||
$handler = $this->createMock(RequestHandlerInterface::class);
|
||||
$handler->expects($this->once())->method('handle')->with($request)->willThrowException($thrownException);
|
||||
|
||||
$this->expectException($expectedException);
|
||||
|
||||
$this->handler->process($request, $handler);
|
||||
}
|
||||
|
||||
public static function provideExceptions(): iterable
|
||||
{
|
||||
$baseRequest = ServerRequestFactory::fromGlobals();
|
||||
|
||||
yield 'no version' => [
|
||||
$baseRequest,
|
||||
ValidationException::fromArray([]),
|
||||
BackwardsCompatibleProblemDetailsException::class,
|
||||
];
|
||||
yield 'version 1' => [
|
||||
$baseRequest->withAttribute('version', '1'),
|
||||
ValidationException::fromArray([]),
|
||||
BackwardsCompatibleProblemDetailsException::class,
|
||||
];
|
||||
yield 'version 2' => [
|
||||
$baseRequest->withAttribute('version', '2'),
|
||||
ValidationException::fromArray([]),
|
||||
BackwardsCompatibleProblemDetailsException::class,
|
||||
];
|
||||
yield 'version 3' => [
|
||||
$baseRequest->withAttribute('version', '3'),
|
||||
ValidationException::fromArray([]),
|
||||
ValidationException::class,
|
||||
];
|
||||
yield 'version 4' => [
|
||||
$baseRequest->withAttribute('version', '3'),
|
||||
ValidationException::fromArray([]),
|
||||
ValidationException::class,
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user