From 88e6b8e54478a4da83dc131b676750d351cdf4b0 Mon Sep 17 00:00:00 2001 From: soyuka Date: Tue, 25 Jun 2024 22:24:35 +0200 Subject: [PATCH] chore: remove deprecations --- Serializer/CollectionNormalizer.php | 2 +- .../ConstraintViolationListNormalizer.php | 18 +-- Serializer/EntrypointNormalizer.php | 18 +-- Serializer/ErrorNormalizer.php | 62 +------- Serializer/ErrorNormalizerTrait.php | 57 -------- Serializer/ObjectNormalizer.php | 26 +--- Tests/JsonSchema/SchemaFactoryTest.php | 1 - Tests/Serializer/CollectionNormalizerTest.php | 8 -- .../ConstraintViolationNormalizerTest.php | 8 -- Tests/Serializer/EntrypointNormalizerTest.php | 5 - Tests/Serializer/ErrorNormalizerTest.php | 132 ------------------ .../ReservedAttributeNameConverterTest.php | 8 +- Tests/State/JsonApiProviderTest.php | 4 +- Tests/Util/IriHelperTest.php | 83 ----------- Util/IriHelper.php | 108 -------------- composer.json | 2 +- 16 files changed, 16 insertions(+), 526 deletions(-) delete mode 100644 Serializer/ErrorNormalizerTrait.php delete mode 100644 Tests/Serializer/ErrorNormalizerTest.php delete mode 100644 Tests/Util/IriHelperTest.php delete mode 100644 Util/IriHelper.php diff --git a/Serializer/CollectionNormalizer.php b/Serializer/CollectionNormalizer.php index 3d55a0b..e465b6f 100644 --- a/Serializer/CollectionNormalizer.php +++ b/Serializer/CollectionNormalizer.php @@ -13,9 +13,9 @@ namespace ApiPlatform\JsonApi\Serializer; -use ApiPlatform\JsonApi\Util\IriHelper; use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface; use ApiPlatform\Metadata\ResourceClassResolverInterface; +use ApiPlatform\Metadata\Util\IriHelper; use ApiPlatform\Serializer\AbstractCollectionNormalizer; use Symfony\Component\Serializer\Exception\UnexpectedValueException; diff --git a/Serializer/ConstraintViolationListNormalizer.php b/Serializer/ConstraintViolationListNormalizer.php index 92afba9..28604a9 100644 --- a/Serializer/ConstraintViolationListNormalizer.php +++ b/Serializer/ConstraintViolationListNormalizer.php @@ -14,10 +14,8 @@ namespace ApiPlatform\JsonApi\Serializer; use ApiPlatform\Metadata\Property\Factory\PropertyMetadataFactoryInterface; -use ApiPlatform\Serializer\CacheableSupportsMethodInterface; use Symfony\Component\Serializer\NameConverter\NameConverterInterface; use Symfony\Component\Serializer\Normalizer\NormalizerInterface; -use Symfony\Component\Serializer\Serializer; use Symfony\Component\Validator\ConstraintViolationInterface; use Symfony\Component\Validator\ConstraintViolationListInterface; @@ -26,7 +24,7 @@ * * @author Héctor Hurtarte */ -final class ConstraintViolationListNormalizer implements NormalizerInterface, CacheableSupportsMethodInterface +final class ConstraintViolationListNormalizer implements NormalizerInterface { public const FORMAT = 'jsonapi'; @@ -65,20 +63,6 @@ public function getSupportedTypes($format): array return self::FORMAT === $format ? [ConstraintViolationListInterface::class => true] : []; } - public function hasCacheableSupportsMethod(): bool - { - if (method_exists(Serializer::class, 'getSupportedTypes')) { - trigger_deprecation( - 'api-platform/core', - '3.1', - 'The "%s()" method is deprecated, use "getSupportedTypes()" instead.', - __METHOD__ - ); - } - - return true; - } - private function getSourcePointerFromViolation(ConstraintViolationInterface $violation): string { $fieldName = $violation->getPropertyPath(); diff --git a/Serializer/EntrypointNormalizer.php b/Serializer/EntrypointNormalizer.php index 6dc1e1e..1dd6b67 100644 --- a/Serializer/EntrypointNormalizer.php +++ b/Serializer/EntrypointNormalizer.php @@ -20,9 +20,7 @@ use ApiPlatform\Metadata\IriConverterInterface; use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface; use ApiPlatform\Metadata\UrlGeneratorInterface; -use ApiPlatform\Serializer\CacheableSupportsMethodInterface; use Symfony\Component\Serializer\Normalizer\NormalizerInterface; -use Symfony\Component\Serializer\Serializer; /** * Normalizes the API entrypoint. @@ -30,7 +28,7 @@ * @author Amrouche Hamza * @author Kévin Dunglas */ -final class EntrypointNormalizer implements NormalizerInterface, CacheableSupportsMethodInterface +final class EntrypointNormalizer implements NormalizerInterface { public const FORMAT = 'jsonapi'; @@ -79,18 +77,4 @@ public function getSupportedTypes($format): array { return self::FORMAT === $format ? [Entrypoint::class => true] : []; } - - public function hasCacheableSupportsMethod(): bool - { - if (method_exists(Serializer::class, 'getSupportedTypes')) { - trigger_deprecation( - 'api-platform/core', - '3.1', - 'The "%s()" method is deprecated, use "getSupportedTypes()" instead.', - __METHOD__ - ); - } - - return true; - } } diff --git a/Serializer/ErrorNormalizer.php b/Serializer/ErrorNormalizer.php index 6249a07..c6a5997 100644 --- a/Serializer/ErrorNormalizer.php +++ b/Serializer/ErrorNormalizer.php @@ -13,31 +13,20 @@ namespace ApiPlatform\JsonApi\Serializer; -use ApiPlatform\Serializer\CacheableSupportsMethodInterface; -use ApiPlatform\Symfony\Validator\Exception\ConstraintViolationListAwareExceptionInterface as LegacyConstraintViolationListAwareExceptionInterface; -use ApiPlatform\Validator\Exception\ConstraintViolationListAwareExceptionInterface; use Symfony\Component\ErrorHandler\Exception\FlattenException; use Symfony\Component\Serializer\Normalizer\NormalizerInterface; -use Symfony\Component\Serializer\Serializer; /** * Converts {@see \Exception} or {@see FlattenException} or to a JSON API error representation. * * @author Héctor Hurtarte */ -final class ErrorNormalizer implements NormalizerInterface, CacheableSupportsMethodInterface +final class ErrorNormalizer implements NormalizerInterface { - use ErrorNormalizerTrait; - public const FORMAT = 'jsonapi'; - public const TITLE = 'title'; - private array $defaultContext = [ - self::TITLE => 'An error occurred', - ]; - public function __construct(private readonly bool $debug = false, array $defaultContext = [], private ?NormalizerInterface $itemNormalizer = null, private ?NormalizerInterface $constraintViolationListNormalizer = null) + public function __construct(private ?NormalizerInterface $itemNormalizer = null) { - $this->defaultContext = array_merge($this->defaultContext, $defaultContext); } /** @@ -45,35 +34,12 @@ public function __construct(private readonly bool $debug = false, array $default */ public function normalize(mixed $object, ?string $format = null, array $context = []): array { - // TODO: in api platform 4 this will be the default, note that JSON:API is close to Problem so we should use the same normalizer - if ($context['rfc_7807_compliant_errors'] ?? false) { - if ($object instanceof LegacyConstraintViolationListAwareExceptionInterface || $object instanceof ConstraintViolationListAwareExceptionInterface) { - // TODO: return ['errors' => $this->constraintViolationListNormalizer(...)] - return $this->constraintViolationListNormalizer->normalize($object->getConstraintViolationList(), $format, $context); - } - - $jsonApiObject = $this->itemNormalizer->normalize($object, $format, $context); - $error = $jsonApiObject['data']['attributes']; - $error['id'] = $jsonApiObject['data']['id']; - $error['type'] = $jsonApiObject['data']['id']; - - return ['errors' => [$error]]; - } - - $data = [ - 'title' => $context[self::TITLE] ?? $this->defaultContext[self::TITLE], - 'description' => $this->getErrorMessage($object, $context, $this->debug), - ]; - - if (null !== $errorCode = $this->getErrorCode($object)) { - $data['code'] = $errorCode; - } + $jsonApiObject = $this->itemNormalizer->normalize($object, $format, $context); + $error = $jsonApiObject['data']['attributes']; + $error['id'] = $jsonApiObject['data']['id']; + $error['type'] = $jsonApiObject['data']['id']; - if ($this->debug && null !== $trace = $object->getTrace()) { - $data['trace'] = $trace; - } - - return $data; + return ['errors' => [$error]]; } /** @@ -95,18 +61,4 @@ public function getSupportedTypes($format): array return []; } - - public function hasCacheableSupportsMethod(): bool - { - if (method_exists(Serializer::class, 'getSupportedTypes')) { - trigger_deprecation( - 'api-platform/core', - '3.1', - 'The "%s()" method is deprecated, use "getSupportedTypes()" instead.', - __METHOD__ - ); - } - - return true; - } } diff --git a/Serializer/ErrorNormalizerTrait.php b/Serializer/ErrorNormalizerTrait.php deleted file mode 100644 index ffb08c4..0000000 --- a/Serializer/ErrorNormalizerTrait.php +++ /dev/null @@ -1,57 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -declare(strict_types=1); - -namespace ApiPlatform\JsonApi\Serializer; - -use ApiPlatform\Metadata\Exception\ErrorCodeSerializableInterface; -use Symfony\Component\ErrorHandler\Exception\FlattenException; -use Symfony\Component\HttpFoundation\Response; - -/** - * @internal - */ -trait ErrorNormalizerTrait -{ - private function getErrorMessage($object, array $context, bool $debug = false): string - { - $message = $object->getMessage(); - - if ($debug) { - return $message; - } - - if ($object instanceof FlattenException) { - $statusCode = $context['statusCode'] ?? $object->getStatusCode(); - if ($statusCode >= 500 && $statusCode < 600) { - $message = Response::$statusTexts[$statusCode] ?? Response::$statusTexts[Response::HTTP_INTERNAL_SERVER_ERROR]; - } - } - - return $message; - } - - private function getErrorCode(object $object): ?string - { - if ($object instanceof FlattenException) { - $exceptionClass = $object->getClass(); - } else { - $exceptionClass = $object::class; - } - - if (is_a($exceptionClass, ErrorCodeSerializableInterface::class, true)) { - return $exceptionClass::getErrorCode(); - } - - return null; - } -} diff --git a/Serializer/ObjectNormalizer.php b/Serializer/ObjectNormalizer.php index f6b1583..196c12a 100644 --- a/Serializer/ObjectNormalizer.php +++ b/Serializer/ObjectNormalizer.php @@ -17,16 +17,13 @@ use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface; use ApiPlatform\Metadata\ResourceClassResolverInterface; use ApiPlatform\Metadata\Util\ClassInfoTrait; -use ApiPlatform\Serializer\CacheableSupportsMethodInterface; -use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface as BaseCacheableSupportsMethodInterface; use Symfony\Component\Serializer\Normalizer\NormalizerInterface; -use Symfony\Component\Serializer\Serializer; /** * Decorates the output with JSON API metadata when appropriate, but otherwise * just passes through to the decorated normalizer. */ -final class ObjectNormalizer implements NormalizerInterface, CacheableSupportsMethodInterface +final class ObjectNormalizer implements NormalizerInterface { use ClassInfoTrait; @@ -46,30 +43,9 @@ public function supportsNormalization(mixed $data, ?string $format = null, array public function getSupportedTypes($format): array { - // @deprecated remove condition when support for symfony versions under 6.3 is dropped - if (!method_exists($this->decorated, 'getSupportedTypes')) { - return [ - '*' => $this->decorated instanceof BaseCacheableSupportsMethodInterface && $this->decorated->hasCacheableSupportsMethod(), - ]; - } - return self::FORMAT === $format ? $this->decorated->getSupportedTypes($format) : []; } - public function hasCacheableSupportsMethod(): bool - { - if (method_exists(Serializer::class, 'getSupportedTypes')) { - trigger_deprecation( - 'api-platform/core', - '3.1', - 'The "%s()" method is deprecated, use "getSupportedTypes()" instead.', - __METHOD__ - ); - } - - return $this->decorated instanceof BaseCacheableSupportsMethodInterface && $this->decorated->hasCacheableSupportsMethod(); - } - /** * {@inheritdoc} */ diff --git a/Tests/JsonSchema/SchemaFactoryTest.php b/Tests/JsonSchema/SchemaFactoryTest.php index 5c9b6f6..2f22ea2 100644 --- a/Tests/JsonSchema/SchemaFactoryTest.php +++ b/Tests/JsonSchema/SchemaFactoryTest.php @@ -53,7 +53,6 @@ protected function setUp(): void $definitionNameFactory = new DefinitionNameFactory(['jsonapi' => true]); $baseSchemaFactory = new BaseSchemaFactory( - typeFactory: null, resourceMetadataFactory: $resourceMetadataFactory->reveal(), propertyNameCollectionFactory: $propertyNameCollectionFactory->reveal(), propertyMetadataFactory: $propertyMetadataFactory->reveal(), diff --git a/Tests/Serializer/CollectionNormalizerTest.php b/Tests/Serializer/CollectionNormalizerTest.php index 6d117e3..62ca83a 100644 --- a/Tests/Serializer/CollectionNormalizerTest.php +++ b/Tests/Serializer/CollectionNormalizerTest.php @@ -26,7 +26,6 @@ use Prophecy\PhpUnit\ProphecyTrait; use Symfony\Component\Serializer\Exception\UnexpectedValueException; use Symfony\Component\Serializer\Normalizer\NormalizerInterface; -use Symfony\Component\Serializer\Serializer; /** * @author Amrouche Hamza @@ -35,9 +34,6 @@ class CollectionNormalizerTest extends TestCase { use ProphecyTrait; - /** - * @group legacy - */ public function testSupportsNormalize(): void { $resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class); @@ -56,10 +52,6 @@ public function testSupportsNormalize(): void 'native-array' => true, '\Traversable' => true, ], $normalizer->getSupportedTypes($normalizer::FORMAT)); - - if (!method_exists(Serializer::class, 'getSupportedTypes')) { - $this->assertTrue($normalizer->hasCacheableSupportsMethod()); - } } public function testNormalizePaginator(): void diff --git a/Tests/Serializer/ConstraintViolationNormalizerTest.php b/Tests/Serializer/ConstraintViolationNormalizerTest.php index 6da0e19..f587321 100644 --- a/Tests/Serializer/ConstraintViolationNormalizerTest.php +++ b/Tests/Serializer/ConstraintViolationNormalizerTest.php @@ -22,7 +22,6 @@ use Prophecy\PhpUnit\ProphecyTrait; use Symfony\Component\PropertyInfo\Type; use Symfony\Component\Serializer\NameConverter\NameConverterInterface; -use Symfony\Component\Serializer\Serializer; use Symfony\Component\Validator\ConstraintViolation; use Symfony\Component\Validator\ConstraintViolationList; use Symfony\Component\Validator\ConstraintViolationListInterface; @@ -34,9 +33,6 @@ class ConstraintViolationNormalizerTest extends TestCase { use ProphecyTrait; - /** - * @group legacy - */ public function testSupportNormalization(): void { $propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class); @@ -49,10 +45,6 @@ public function testSupportNormalization(): void $this->assertFalse($normalizer->supportsNormalization(new \stdClass(), ConstraintViolationListNormalizer::FORMAT)); $this->assertEmpty($normalizer->getSupportedTypes('json')); $this->assertSame([ConstraintViolationListInterface::class => true], $normalizer->getSupportedTypes($normalizer::FORMAT)); - - if (!method_exists(Serializer::class, 'getSupportedTypes')) { - $this->assertTrue($normalizer->hasCacheableSupportsMethod()); - } } public function testNormalize(): void diff --git a/Tests/Serializer/EntrypointNormalizerTest.php b/Tests/Serializer/EntrypointNormalizerTest.php index 8b0b820..2343253 100644 --- a/Tests/Serializer/EntrypointNormalizerTest.php +++ b/Tests/Serializer/EntrypointNormalizerTest.php @@ -32,7 +32,6 @@ use PHPUnit\Framework\TestCase; use Prophecy\Argument; use Prophecy\PhpUnit\ProphecyTrait; -use Symfony\Component\Serializer\Serializer; /** * @author Amrouche Hamza @@ -57,10 +56,6 @@ public function testSupportNormalization(): void $this->assertFalse($normalizer->supportsNormalization(new \stdClass(), EntrypointNormalizer::FORMAT)); $this->assertEmpty($normalizer->getSupportedTypes('json')); $this->assertSame([Entrypoint::class => true], $normalizer->getSupportedTypes($normalizer::FORMAT)); - - if (!method_exists(Serializer::class, 'getSupportedTypes')) { - $this->assertTrue($normalizer->hasCacheableSupportsMethod()); - } } public function testNormalize(): void diff --git a/Tests/Serializer/ErrorNormalizerTest.php b/Tests/Serializer/ErrorNormalizerTest.php deleted file mode 100644 index 7b9ee30..0000000 --- a/Tests/Serializer/ErrorNormalizerTest.php +++ /dev/null @@ -1,132 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -declare(strict_types=1); - -namespace ApiPlatform\JsonApi\Tests\Serializer; - -use ApiPlatform\JsonApi\Serializer\ErrorNormalizer; -use ApiPlatform\JsonApi\Tests\Fixtures\ErrorCodeSerializable; -use PHPUnit\Framework\TestCase; -use Symfony\Component\ErrorHandler\Exception\FlattenException; -use Symfony\Component\HttpFoundation\Response; -use Symfony\Component\Serializer\Serializer; - -/** - * @author Baptiste Meyer - */ -class ErrorNormalizerTest extends TestCase -{ - /** - * @group legacy - */ - public function testSupportsNormalization(): void - { - $normalizer = new ErrorNormalizer(); - - $this->assertTrue($normalizer->supportsNormalization(new \Exception(), ErrorNormalizer::FORMAT)); - $this->assertFalse($normalizer->supportsNormalization(new \Exception(), 'xml')); - $this->assertFalse($normalizer->supportsNormalization(new \stdClass(), ErrorNormalizer::FORMAT)); - - $this->assertTrue($normalizer->supportsNormalization(new FlattenException(), ErrorNormalizer::FORMAT)); - $this->assertFalse($normalizer->supportsNormalization(new FlattenException(), 'xml')); - $this->assertFalse($normalizer->supportsNormalization(new \stdClass(), ErrorNormalizer::FORMAT)); - $this->assertEmpty($normalizer->getSupportedTypes('json')); - $this->assertSame([ - \Exception::class => true, - FlattenException::class => true, - ], $normalizer->getSupportedTypes($normalizer::FORMAT)); - - if (!method_exists(Serializer::class, 'getSupportedTypes')) { - $this->assertTrue($normalizer->hasCacheableSupportsMethod()); - } - } - - /** - * @dataProvider errorProvider - * - * @group legacy - * - * @param int $status http status code of the Exception - * @param string $originalMessage original message of the Exception - * @param bool $debug simulates kernel debug variable - */ - public function testNormalize(int $status, string $originalMessage, bool $debug): void - { - $normalizer = new ErrorNormalizer($debug); - $exception = FlattenException::create(new \Exception($originalMessage), $status); - - $expected = [ - 'title' => 'An error occurred', - 'description' => ($debug || $status < 500) ? $originalMessage : Response::$statusTexts[$status], - ]; - - if ($debug) { - $expected['trace'] = $exception->getTrace(); - } - - $this->assertSame($expected, $normalizer->normalize($exception, ErrorNormalizer::FORMAT, ['statusCode' => $status])); - } - - /** - * @group legacy - */ - public function testNormalizeAnExceptionWithCustomErrorCode(): void - { - $status = Response::HTTP_BAD_REQUEST; - $originalMessage = 'my-message'; - $debug = false; - - $normalizer = new ErrorNormalizer($debug); - $exception = new ErrorCodeSerializable($originalMessage); - - $expected = [ - 'title' => 'An error occurred', - 'description' => 'my-message', - 'code' => ErrorCodeSerializable::getErrorCode(), - ]; - - $this->assertSame($expected, $normalizer->normalize($exception, ErrorNormalizer::FORMAT, ['statusCode' => $status])); - } - - /** - * @group legacy - */ - public function testNormalizeAFlattenExceptionWithCustomErrorCode(): void - { - $status = Response::HTTP_BAD_REQUEST; - $originalMessage = 'my-message'; - $debug = false; - - $normalizer = new ErrorNormalizer($debug); - $exception = FlattenException::create(new ErrorCodeSerializable($originalMessage), $status); - - $expected = [ - 'title' => 'An error occurred', - 'description' => 'my-message', - 'code' => ErrorCodeSerializable::getErrorCode(), - ]; - - $this->assertSame($expected, $normalizer->normalize($exception, ErrorNormalizer::FORMAT, ['statusCode' => $status])); - } - - public static function errorProvider(): array - { - return [ - [Response::HTTP_INTERNAL_SERVER_ERROR, 'Sensitive SQL error displayed', false], - [Response::HTTP_GATEWAY_TIMEOUT, 'Sensitive server error displayed', false], - [Response::HTTP_BAD_REQUEST, 'Bad Request Message', false], - [Response::HTTP_INTERNAL_SERVER_ERROR, 'Sensitive SQL error displayed', true], - [Response::HTTP_GATEWAY_TIMEOUT, 'Sensitive server error displayed', true], - [Response::HTTP_BAD_REQUEST, 'Bad Request Message', true], - ]; - } -} diff --git a/Tests/Serializer/ReservedAttributeNameConverterTest.php b/Tests/Serializer/ReservedAttributeNameConverterTest.php index 18bf241..7c261af 100644 --- a/Tests/Serializer/ReservedAttributeNameConverterTest.php +++ b/Tests/Serializer/ReservedAttributeNameConverterTest.php @@ -46,17 +46,13 @@ public static function propertiesProvider(): array ]; } - /** - * @dataProvider propertiesProvider - */ + #[\PHPUnit\Framework\Attributes\DataProvider('propertiesProvider')] public function testNormalize(string $propertyName, string $expectedPropertyName): void { $this->assertSame($expectedPropertyName, $this->reservedAttributeNameConverter->normalize($propertyName)); } - /** - * @dataProvider propertiesProvider - */ + #[\PHPUnit\Framework\Attributes\DataProvider('propertiesProvider')] public function testDenormalize(string $expectedPropertyName, string $propertyName): void { $this->assertSame($expectedPropertyName, $this->reservedAttributeNameConverter->denormalize($propertyName)); diff --git a/Tests/State/JsonApiProviderTest.php b/Tests/State/JsonApiProviderTest.php index 9139f80..180c5d5 100644 --- a/Tests/State/JsonApiProviderTest.php +++ b/Tests/State/JsonApiProviderTest.php @@ -17,6 +17,7 @@ use ApiPlatform\Metadata\Get; use ApiPlatform\State\ProviderInterface; use PHPUnit\Framework\TestCase; +use Symfony\Component\HttpFoundation\InputBag; use Symfony\Component\HttpFoundation\ParameterBag; use Symfony\Component\HttpFoundation\Request; @@ -29,8 +30,7 @@ public function testProvide(): void $request->attributes = $this->createMock(ParameterBag::class); $request->attributes->expects($this->once())->method('get')->with('_api_filters', [])->willReturn([]); $request->attributes->method('set')->with($this->logicalOr('_api_filter_property', '_api_included', '_api_filters'), $this->logicalOr(['id', 'name', 'dummyFloat', 'relatedDummy' => ['id', 'name']], ['relatedDummy'], [])); - $request->query = $this->createMock(ParameterBag::class); // @phpstan-ignore-line - $request->query->method('all')->willReturn(['fields' => ['dummy' => 'id,name,dummyFloat', 'relatedDummy' => 'id,name'], 'include' => 'relatedDummy,foo']); + $request->query = new InputBag(['fields' => ['dummy' => 'id,name,dummyFloat', 'relatedDummy' => 'id,name'], 'include' => 'relatedDummy,foo']); $operation = new Get(class: 'dummy', shortName: 'dummy'); $context = ['request' => $request]; $decorated = $this->createMock(ProviderInterface::class); diff --git a/Tests/Util/IriHelperTest.php b/Tests/Util/IriHelperTest.php deleted file mode 100644 index 3996176..0000000 --- a/Tests/Util/IriHelperTest.php +++ /dev/null @@ -1,83 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -declare(strict_types=1); - -namespace ApiPlatform\JsonApi\Tests\Util; - -use ApiPlatform\JsonApi\Util\IriHelper; -use ApiPlatform\Metadata\Exception\InvalidArgumentException; -use ApiPlatform\Metadata\UrlGeneratorInterface; -use PHPUnit\Framework\TestCase; -use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait; - -/** - * @author Kévin Dunglas - */ -class IriHelperTest extends TestCase -{ - use ExpectDeprecationTrait; - - public function testHelpers(): void - { - $parsed = [ - 'parts' => [ - 'path' => '/hello.json', - 'query' => 'foo=bar&page=2&bar=3', - ], - 'parameters' => [ - 'foo' => 'bar', - 'bar' => '3', - ], - ]; - - $this->assertSame($parsed, IriHelper::parseIri('/hello.json?foo=bar&page=2&bar=3', 'page')); - $this->assertSame('/hello.json?foo=bar&bar=3&page=2', IriHelper::createIri($parsed['parts'], $parsed['parameters'], 'page', 2.)); - } - - public function testHelpersWithNetworkPath(): void - { - $parsed = [ - 'parts' => [ - 'path' => '/hello.json', - 'query' => 'foo=bar&page=2&bar=3', - 'scheme' => 'http', - 'user' => 'foo', - 'pass' => 'bar', - 'host' => 'localhost', - 'port' => 8080, - 'fragment' => 'foo', - ], - 'parameters' => [ - 'foo' => 'bar', - 'bar' => '3', - ], - ]; - - $this->assertSame('//foo:bar@localhost:8080/hello.json?foo=bar&bar=3&page=2#foo', IriHelper::createIri($parsed['parts'], $parsed['parameters'], 'page', 2., UrlGeneratorInterface::NET_PATH)); - - unset($parsed['parts']['scheme']); - - $this->assertSame('//foo:bar@localhost:8080/hello.json?foo=bar&bar=3&page=2#foo', IriHelper::createIri($parsed['parts'], $parsed['parameters'], 'page', 2., UrlGeneratorInterface::NET_PATH)); - - $parsed['parts']['port'] = 443; - - $this->assertSame('//foo:bar@localhost:443/hello.json?foo=bar&bar=3&page=2#foo', IriHelper::createIri($parsed['parts'], $parsed['parameters'], 'page', 2., UrlGeneratorInterface::NET_PATH)); - } - - public function testParseIriWithInvalidUrl(): void - { - $this->expectException(InvalidArgumentException::class); - $this->expectExceptionMessage('The request URI "http:///" is malformed.'); - - IriHelper::parseIri('http:///', 'page'); - } -} diff --git a/Util/IriHelper.php b/Util/IriHelper.php deleted file mode 100644 index 11f0444..0000000 --- a/Util/IriHelper.php +++ /dev/null @@ -1,108 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -declare(strict_types=1); - -namespace ApiPlatform\JsonApi\Util; - -use ApiPlatform\Metadata\Exception\InvalidArgumentException; -use ApiPlatform\Metadata\UrlGeneratorInterface; -use ApiPlatform\State\Util\RequestParser; - -/** - * Parses and creates IRIs. - * - * @author Kévin Dunglas - * - * @internal - */ -final class IriHelper -{ - private function __construct() - { - } - - /** - * Parses and standardizes the request IRI. - * - * @throws InvalidArgumentException - */ - public static function parseIri(string $iri, string $pageParameterName): array - { - $parts = parse_url($iri); - if (false === $parts) { - throw new InvalidArgumentException(sprintf('The request URI "%s" is malformed.', $iri)); - } - - $parameters = []; - if (isset($parts['query'])) { - $parameters = RequestParser::parseRequestParams($parts['query']); - - // Remove existing page parameter - unset($parameters[$pageParameterName]); - } - - return ['parts' => $parts, 'parameters' => $parameters]; - } - - /** - * Gets a collection IRI for the given parameters. - */ - public static function createIri(array $parts, array $parameters, ?string $pageParameterName = null, ?float $page = null, $urlGenerationStrategy = UrlGeneratorInterface::ABS_PATH): string - { - if (null !== $page && null !== $pageParameterName) { - $parameters[$pageParameterName] = $page; - } - - $query = http_build_query($parameters, '', '&', \PHP_QUERY_RFC3986); - $parts['query'] = preg_replace('/%5B\d+%5D/', '%5B%5D', $query); - - $url = ''; - if ((UrlGeneratorInterface::ABS_URL === $urlGenerationStrategy || UrlGeneratorInterface::NET_PATH === $urlGenerationStrategy) && isset($parts['host'])) { - if (isset($parts['scheme'])) { - $scheme = $parts['scheme']; - } elseif (isset($parts['port']) && 443 === $parts['port']) { - $scheme = 'https'; - } else { - $scheme = 'http'; - } - $url .= UrlGeneratorInterface::NET_PATH === $urlGenerationStrategy ? '//' : "$scheme://"; - - if (isset($parts['user'])) { - $url .= $parts['user']; - - if (isset($parts['pass'])) { - $url .= ':'.$parts['pass']; - } - - $url .= '@'; - } - - $url .= $parts['host']; - - if (isset($parts['port'])) { - $url .= ':'.$parts['port']; - } - } - - $url .= $parts['path']; - - if ('' !== $parts['query']) { - $url .= '?'.$parts['query']; - } - - if (isset($parts['fragment'])) { - $url .= '#'.$parts['fragment']; - } - - return $url; - } -} diff --git a/composer.json b/composer.json index 6b5420b..12a5352 100644 --- a/composer.json +++ b/composer.json @@ -34,7 +34,7 @@ "api-platform/serializer": "*@dev || ^3.1", "api-platform/state": "*@dev || ^3.1", "symfony/error-handler": "^7.1", - "symfony/http-foundation": "^7.1" + "symfony/http-foundation": "^6.4 || ^7.1" }, "require-dev": { "phpspec/prophecy": "^1.19",