From bffb2588902455a003cbad8a9254b1a34d592e78 Mon Sep 17 00:00:00 2001 From: soyuka Date: Wed, 20 Apr 2022 16:02:19 +0200 Subject: [PATCH] fix: deprecate data transformers --- .../Symfony/Messenger/DataTransformer.php | 82 +++++++++++++- .../DataTransformerInitializerInterface.php | 2 +- .../DataTransformerInterface.php | 2 +- src/Serializer/AbstractItemNormalizer.php | 15 ++- .../Bundle/Resources/config/messenger.xml | 2 +- src/Symfony/Messenger/DataTransformer.php | 100 ------------------ src/deprecated_interfaces.php | 4 - src/deprecation.php | 1 - .../Symfony/Messenger/DataTransformerTest.php | 2 +- .../CustomInputDtoDataTransformer.php | 2 +- .../CustomOutputDtoDataTransformer.php | 2 +- ...myDtoNoInputToOutputDtoDataTransformer.php | 2 +- .../InitializeInputDataTransformer.php | 2 +- .../InputDtoDataTransformer.php | 2 +- .../OutputDtoDataTransformer.php | 2 +- .../OutputDtoSameClassTransformer.php | 2 +- .../RPCOutputDataTransformer.php | 2 +- .../RecoverPasswordInputDataTransformer.php | 2 +- .../RecoverPasswordOutputDataTransformer.php | 2 +- .../Serializer/AbstractItemNormalizerTest.php | 25 +++-- tests/Serializer/ItemNormalizerTest.php | 8 +- 21 files changed, 131 insertions(+), 132 deletions(-) rename src/{ => Core}/DataTransformer/DataTransformerInitializerInterface.php (93%) rename src/{ => Core}/DataTransformer/DataTransformerInterface.php (95%) delete mode 100644 src/Symfony/Messenger/DataTransformer.php rename tests/{ => Core/Bridge}/Symfony/Messenger/DataTransformerTest.php (98%) diff --git a/src/Core/Bridge/Symfony/Messenger/DataTransformer.php b/src/Core/Bridge/Symfony/Messenger/DataTransformer.php index 73e0dcdf626..d940788fbf9 100644 --- a/src/Core/Bridge/Symfony/Messenger/DataTransformer.php +++ b/src/Core/Bridge/Symfony/Messenger/DataTransformer.php @@ -13,10 +13,86 @@ namespace ApiPlatform\Core\Bridge\Symfony\Messenger; -class_exists(\ApiPlatform\Symfony\Messenger\DataTransformer::class); +use ApiPlatform\Core\DataTransformer\DataTransformerInterface; +use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface; +use ApiPlatform\Exception\OperationNotFoundException; +use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface; +use ApiPlatform\Util\ClassInfoTrait; -if (false) { - final class DataTransformer extends \ApiPlatform\Symfony\Messenger\DataTransformer +/** + * Transforms an Input to itself. This gives the ability to send the Input to a + * message handler and process it asynchronously. + * + * @author Antoine Bluchet + */ +final class DataTransformer implements DataTransformerInterface +{ + use ClassInfoTrait; + + /** + * @var ResourceMetadataCollectionFactoryInterface|ResourceMetadataFactoryInterface + */ + private $resourceMetadataFactory; + + public function __construct($resourceMetadataFactory) { + $this->resourceMetadataFactory = $resourceMetadataFactory; + + if (!$resourceMetadataFactory instanceof ResourceMetadataCollectionFactoryInterface) { + trigger_deprecation('api-platform/core', '2.7', sprintf('Use "%s" instead of "%s".', ResourceMetadataCollectionFactoryInterface::class, ResourceMetadataFactoryInterface::class)); + } + } + + /** + * {@inheritdoc} + * + * @return object + */ + public function transform($object, string $to, array $context = []) + { + return $object; + } + + /** + * {@inheritdoc} + */ + public function supportsTransformation($data, string $to, array $context = []): bool + { + if ( + \is_object($data) // data is not normalized yet, it should be an array + || + null === ($context['input']['class'] ?? null) + ) { + return false; + } + + if ($this->resourceMetadataFactory instanceof ResourceMetadataCollectionFactoryInterface) { + try { + $resourceMetadataCollection = $this->resourceMetadataFactory->create($context['resource_class'] ?? $to); + $operation = $resourceMetadataCollection->getOperation($context['operation_name'] ?? null); + + return 'input' === $operation->getMessenger(); + } catch (OperationNotFoundException $e) { + return false; + } + } + + $metadata = $this->resourceMetadataFactory->create($context['resource_class'] ?? $to); + + if (isset($context['graphql_operation_name'])) { + return 'input' === $metadata->getGraphqlAttribute($context['graphql_operation_name'], 'messenger', null, true); + } + + if (!isset($context['operation_type'])) { + return 'input' === $metadata->getAttribute('messenger'); + } + + return 'input' === $metadata->getTypedOperationAttribute( + $context['operation_type'], + $context[$context['operation_type'].'_operation_name'] ?? '', + 'messenger', + null, + true + ); } } diff --git a/src/DataTransformer/DataTransformerInitializerInterface.php b/src/Core/DataTransformer/DataTransformerInitializerInterface.php similarity index 93% rename from src/DataTransformer/DataTransformerInitializerInterface.php rename to src/Core/DataTransformer/DataTransformerInitializerInterface.php index dde47366ce5..85007c818e1 100644 --- a/src/DataTransformer/DataTransformerInitializerInterface.php +++ b/src/Core/DataTransformer/DataTransformerInitializerInterface.php @@ -11,7 +11,7 @@ declare(strict_types=1); -namespace ApiPlatform\DataTransformer; +namespace ApiPlatform\Core\DataTransformer; interface DataTransformerInitializerInterface extends DataTransformerInterface { diff --git a/src/DataTransformer/DataTransformerInterface.php b/src/Core/DataTransformer/DataTransformerInterface.php similarity index 95% rename from src/DataTransformer/DataTransformerInterface.php rename to src/Core/DataTransformer/DataTransformerInterface.php index 56abe3fb5c5..156c0ffe437 100644 --- a/src/DataTransformer/DataTransformerInterface.php +++ b/src/Core/DataTransformer/DataTransformerInterface.php @@ -11,7 +11,7 @@ declare(strict_types=1); -namespace ApiPlatform\DataTransformer; +namespace ApiPlatform\Core\DataTransformer; /** * Transforms a DTO or an Anonymous class to a Resource object. diff --git a/src/Serializer/AbstractItemNormalizer.php b/src/Serializer/AbstractItemNormalizer.php index 01d8e27eb1d..ee02afc5f1c 100644 --- a/src/Serializer/AbstractItemNormalizer.php +++ b/src/Serializer/AbstractItemNormalizer.php @@ -16,12 +16,13 @@ use ApiPlatform\Api\IriConverterInterface; use ApiPlatform\Api\UrlGeneratorInterface; use ApiPlatform\Core\Api\IriConverterInterface as LegacyIriConverterInterface; +use ApiPlatform\Core\Bridge\Symfony\Messenger\DataTransformer as MessengerDataTransformer; use ApiPlatform\Core\DataProvider\ItemDataProviderInterface; +use ApiPlatform\Core\DataTransformer\DataTransformerInitializerInterface; +use ApiPlatform\Core\DataTransformer\DataTransformerInterface; use ApiPlatform\Core\Metadata\Property\Factory\PropertyMetadataFactoryInterface as LegacyPropertyMetadataFactoryInterface; use ApiPlatform\Core\Metadata\Property\PropertyMetadata; use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface; -use ApiPlatform\DataTransformer\DataTransformerInitializerInterface; -use ApiPlatform\DataTransformer\DataTransformerInterface; use ApiPlatform\Exception\InvalidArgumentException; use ApiPlatform\Exception\InvalidValueException; use ApiPlatform\Exception\ItemNotFoundException; @@ -110,6 +111,16 @@ public function __construct(PropertyNameCollectionFactoryInterface $propertyName $this->allowPlainIdentifiers = $allowPlainIdentifiers; $this->dataTransformers = $dataTransformers; + + // Just skip our data transformer to trigger a proper deprecation + $customDataTransformers = array_filter(\is_array($dataTransformers) ? $dataTransformers : iterator_to_array($dataTransformers), function ($dataTransformer) { + return !$dataTransformer instanceof MessengerDataTransformer; + }); + + if (\count($customDataTransformers)) { + trigger_deprecation('api-platform/core', '2.7', 'The DataTransformer pattern is deprecated, use a Provider or a Processor and either use your input or return a new output there.'); + } + if ($resourceMetadataFactory && !$resourceMetadataFactory instanceof ResourceMetadataCollectionFactoryInterface) { trigger_deprecation('api-platform/core', '2.7', sprintf('Use "%s" instead of "%s".', ResourceMetadataCollectionFactoryInterface::class, ResourceMetadataFactoryInterface::class)); } diff --git a/src/Symfony/Bundle/Resources/config/messenger.xml b/src/Symfony/Bundle/Resources/config/messenger.xml index ca63b5dc9b9..8d2527f8968 100644 --- a/src/Symfony/Bundle/Resources/config/messenger.xml +++ b/src/Symfony/Bundle/Resources/config/messenger.xml @@ -21,7 +21,7 @@ - + diff --git a/src/Symfony/Messenger/DataTransformer.php b/src/Symfony/Messenger/DataTransformer.php deleted file mode 100644 index 4358ed4ee2d..00000000000 --- a/src/Symfony/Messenger/DataTransformer.php +++ /dev/null @@ -1,100 +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\Symfony\Messenger; - -use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface; -use ApiPlatform\DataTransformer\DataTransformerInterface; -use ApiPlatform\Exception\OperationNotFoundException; -use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface; -use ApiPlatform\Util\ClassInfoTrait; - -/** - * Transforms an Input to itself. This gives the ability to send the Input to a - * message handler and process it asynchronously. - * - * @author Antoine Bluchet - */ -final class DataTransformer implements DataTransformerInterface -{ - use ClassInfoTrait; - - /** - * @var ResourceMetadataCollectionFactoryInterface|ResourceMetadataFactoryInterface - */ - private $resourceMetadataFactory; - - public function __construct($resourceMetadataFactory) - { - $this->resourceMetadataFactory = $resourceMetadataFactory; - - if (!$resourceMetadataFactory instanceof ResourceMetadataCollectionFactoryInterface) { - trigger_deprecation('api-platform/core', '2.7', sprintf('Use "%s" instead of "%s".', ResourceMetadataCollectionFactoryInterface::class, ResourceMetadataFactoryInterface::class)); - } - } - - /** - * {@inheritdoc} - * - * @return object - */ - public function transform($object, string $to, array $context = []) - { - return $object; - } - - /** - * {@inheritdoc} - */ - public function supportsTransformation($data, string $to, array $context = []): bool - { - if ( - \is_object($data) // data is not normalized yet, it should be an array - || - null === ($context['input']['class'] ?? null) - ) { - return false; - } - - if ($this->resourceMetadataFactory instanceof ResourceMetadataCollectionFactoryInterface) { - try { - $resourceMetadataCollection = $this->resourceMetadataFactory->create($context['resource_class'] ?? $to); - $operation = $resourceMetadataCollection->getOperation($context['operation_name'] ?? null); - - return 'input' === $operation->getMessenger(); - } catch (OperationNotFoundException $e) { - return false; - } - } - - $metadata = $this->resourceMetadataFactory->create($context['resource_class'] ?? $to); - - if (isset($context['graphql_operation_name'])) { - return 'input' === $metadata->getGraphqlAttribute($context['graphql_operation_name'], 'messenger', null, true); - } - - if (!isset($context['operation_type'])) { - return 'input' === $metadata->getAttribute('messenger'); - } - - return 'input' === $metadata->getTypedOperationAttribute( - $context['operation_type'], - $context[$context['operation_type'].'_operation_name'] ?? '', - 'messenger', - null, - true - ); - } -} - -class_alias(DataTransformer::class, \ApiPlatform\Core\Bridge\Symfony\Messenger\DataTransformer::class); diff --git a/src/deprecated_interfaces.php b/src/deprecated_interfaces.php index 5ad660cb5d9..8f1c66a5dfc 100644 --- a/src/deprecated_interfaces.php +++ b/src/deprecated_interfaces.php @@ -65,10 +65,6 @@ ApiPlatform\Core\DataProvider\PaginatorInterface::class => ApiPlatform\State\Pagination\PaginatorInterface::class, ApiPlatform\Core\DataProvider\PartialPaginatorInterface::class => ApiPlatform\State\Pagination\PartialPaginatorInterface::class, - // DataTransformer - ApiPlatform\Core\DataTransformer\DataTransformerInitializerInterface::class => ApiPlatform\DataTransformer\DataTransformerInitializerInterface::class, - ApiPlatform\Core\DataTransformer\DataTransformerInterface::class => ApiPlatform\DataTransformer\DataTransformerInterface::class, - // Documentation ApiPlatform\Core\Documentation\DocumentationInterface::class => ApiPlatform\Documentation\DocumentationInterface::class, diff --git a/src/deprecation.php b/src/deprecation.php index 6a443408429..d27c444c06c 100644 --- a/src/deprecation.php +++ b/src/deprecation.php @@ -188,7 +188,6 @@ class_alias($interfaceName, $oldInterfaceName); // Bridge\Symfony\Messenger ApiPlatform\Core\Bridge\Symfony\Messenger\ContextStamp::class => ApiPlatform\Symfony\Messenger\ContextStamp::class, ApiPlatform\Core\Bridge\Symfony\Messenger\DispatchTrait::class => ApiPlatform\Symfony\Messenger\DispatchTrait::class, - ApiPlatform\Core\Bridge\Symfony\Messenger\DataTransformer::class => ApiPlatform\Symfony\Messenger\DataTransformer::class, ApiPlatform\Core\Bridge\Symfony\Messenger\RemoveStamp::class => ApiPlatform\Symfony\Messenger\RemoveStamp::class, // Bridge\Symfony\PropertyInfo\Metadata\Property => Metadata\Property diff --git a/tests/Symfony/Messenger/DataTransformerTest.php b/tests/Core/Bridge/Symfony/Messenger/DataTransformerTest.php similarity index 98% rename from tests/Symfony/Messenger/DataTransformerTest.php rename to tests/Core/Bridge/Symfony/Messenger/DataTransformerTest.php index c4eaf998bad..d7b7ab5e78d 100644 --- a/tests/Symfony/Messenger/DataTransformerTest.php +++ b/tests/Core/Bridge/Symfony/Messenger/DataTransformerTest.php @@ -14,10 +14,10 @@ namespace ApiPlatform\Tests\Symfony\Messenger; use ApiPlatform\Core\Api\OperationType; +use ApiPlatform\Core\Bridge\Symfony\Messenger\DataTransformer; use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface; use ApiPlatform\Core\Metadata\Resource\ResourceMetadata; use ApiPlatform\Core\Tests\ProphecyTrait; -use ApiPlatform\Symfony\Messenger\DataTransformer; use ApiPlatform\Tests\Fixtures\TestBundle\Entity\Dummy; use PHPUnit\Framework\TestCase; diff --git a/tests/Fixtures/TestBundle/DataTransformer/CustomInputDtoDataTransformer.php b/tests/Fixtures/TestBundle/DataTransformer/CustomInputDtoDataTransformer.php index 5c960a28493..8374881e57b 100644 --- a/tests/Fixtures/TestBundle/DataTransformer/CustomInputDtoDataTransformer.php +++ b/tests/Fixtures/TestBundle/DataTransformer/CustomInputDtoDataTransformer.php @@ -13,7 +13,7 @@ namespace ApiPlatform\Tests\Fixtures\TestBundle\DataTransformer; -use ApiPlatform\DataTransformer\DataTransformerInterface; +use ApiPlatform\Core\DataTransformer\DataTransformerInterface; use ApiPlatform\Serializer\AbstractItemNormalizer; use ApiPlatform\Tests\Fixtures\TestBundle\Document\DummyDtoCustom as DummyDtoCustomDocument; use ApiPlatform\Tests\Fixtures\TestBundle\Dto\CustomInputDto; diff --git a/tests/Fixtures/TestBundle/DataTransformer/CustomOutputDtoDataTransformer.php b/tests/Fixtures/TestBundle/DataTransformer/CustomOutputDtoDataTransformer.php index a7b5e5847aa..0e3586e10bf 100644 --- a/tests/Fixtures/TestBundle/DataTransformer/CustomOutputDtoDataTransformer.php +++ b/tests/Fixtures/TestBundle/DataTransformer/CustomOutputDtoDataTransformer.php @@ -13,7 +13,7 @@ namespace ApiPlatform\Tests\Fixtures\TestBundle\DataTransformer; -use ApiPlatform\DataTransformer\DataTransformerInterface; +use ApiPlatform\Core\DataTransformer\DataTransformerInterface; use ApiPlatform\Tests\Fixtures\TestBundle\Document\DummyDtoCustom as DummyDtoCustomDocument; use ApiPlatform\Tests\Fixtures\TestBundle\Dto\CustomOutputDto; use ApiPlatform\Tests\Fixtures\TestBundle\Entity\DummyDtoCustom; diff --git a/tests/Fixtures/TestBundle/DataTransformer/DummyDtoNoInputToOutputDtoDataTransformer.php b/tests/Fixtures/TestBundle/DataTransformer/DummyDtoNoInputToOutputDtoDataTransformer.php index 73a7b67114f..e9eb4cb7d2e 100644 --- a/tests/Fixtures/TestBundle/DataTransformer/DummyDtoNoInputToOutputDtoDataTransformer.php +++ b/tests/Fixtures/TestBundle/DataTransformer/DummyDtoNoInputToOutputDtoDataTransformer.php @@ -13,7 +13,7 @@ namespace ApiPlatform\Tests\Fixtures\TestBundle\DataTransformer; -use ApiPlatform\DataTransformer\DataTransformerInterface; +use ApiPlatform\Core\DataTransformer\DataTransformerInterface; use ApiPlatform\Tests\Fixtures\TestBundle\Document\DummyDtoNoInput as DummyDtoNoInputDocument; use ApiPlatform\Tests\Fixtures\TestBundle\Dto\Document\OutputDto as OutputDtoDocument; use ApiPlatform\Tests\Fixtures\TestBundle\Dto\OutputDto; diff --git a/tests/Fixtures/TestBundle/DataTransformer/InitializeInputDataTransformer.php b/tests/Fixtures/TestBundle/DataTransformer/InitializeInputDataTransformer.php index 3281c5dd92e..122c13cd671 100644 --- a/tests/Fixtures/TestBundle/DataTransformer/InitializeInputDataTransformer.php +++ b/tests/Fixtures/TestBundle/DataTransformer/InitializeInputDataTransformer.php @@ -13,7 +13,7 @@ namespace ApiPlatform\Tests\Fixtures\TestBundle\DataTransformer; -use ApiPlatform\DataTransformer\DataTransformerInitializerInterface; +use ApiPlatform\Core\DataTransformer\DataTransformerInitializerInterface; use ApiPlatform\Serializer\AbstractItemNormalizer; use ApiPlatform\Tests\Fixtures\TestBundle\Document\InitializeInput as InitializeInputDocument; use ApiPlatform\Tests\Fixtures\TestBundle\Dto\InitializeInputDto; diff --git a/tests/Fixtures/TestBundle/DataTransformer/InputDtoDataTransformer.php b/tests/Fixtures/TestBundle/DataTransformer/InputDtoDataTransformer.php index 2c9b603a66e..41f55532fb5 100644 --- a/tests/Fixtures/TestBundle/DataTransformer/InputDtoDataTransformer.php +++ b/tests/Fixtures/TestBundle/DataTransformer/InputDtoDataTransformer.php @@ -13,7 +13,7 @@ namespace ApiPlatform\Tests\Fixtures\TestBundle\DataTransformer; -use ApiPlatform\DataTransformer\DataTransformerInterface; +use ApiPlatform\Core\DataTransformer\DataTransformerInterface; use ApiPlatform\Serializer\AbstractItemNormalizer; use ApiPlatform\Tests\Fixtures\TestBundle\Document\DummyDtoInputOutput as DummyDtoInputOutputDocument; use ApiPlatform\Tests\Fixtures\TestBundle\Dto\Document\InputDto as InputDtoDocument; diff --git a/tests/Fixtures/TestBundle/DataTransformer/OutputDtoDataTransformer.php b/tests/Fixtures/TestBundle/DataTransformer/OutputDtoDataTransformer.php index 8b15292ce0e..cfdec547e6a 100644 --- a/tests/Fixtures/TestBundle/DataTransformer/OutputDtoDataTransformer.php +++ b/tests/Fixtures/TestBundle/DataTransformer/OutputDtoDataTransformer.php @@ -13,7 +13,7 @@ namespace ApiPlatform\Tests\Fixtures\TestBundle\DataTransformer; -use ApiPlatform\DataTransformer\DataTransformerInterface; +use ApiPlatform\Core\DataTransformer\DataTransformerInterface; use ApiPlatform\State\Pagination\ArrayPaginator; use ApiPlatform\Tests\Fixtures\TestBundle\Document\DummyDtoInputOutput as DummyDtoInputOutputDocument; use ApiPlatform\Tests\Fixtures\TestBundle\Dto\Document\OutputDto as OutputDtoDocument; diff --git a/tests/Fixtures/TestBundle/DataTransformer/OutputDtoSameClassTransformer.php b/tests/Fixtures/TestBundle/DataTransformer/OutputDtoSameClassTransformer.php index 0a3e11596d1..f8a776016c1 100644 --- a/tests/Fixtures/TestBundle/DataTransformer/OutputDtoSameClassTransformer.php +++ b/tests/Fixtures/TestBundle/DataTransformer/OutputDtoSameClassTransformer.php @@ -13,7 +13,7 @@ namespace ApiPlatform\Tests\Fixtures\TestBundle\DataTransformer; -use ApiPlatform\DataTransformer\DataTransformerInterface; +use ApiPlatform\Core\DataTransformer\DataTransformerInterface; use ApiPlatform\Tests\Fixtures\TestBundle\Document\DummyDtoOutputFallbackToSameClass as DummyDtoOutputFallbackToSameClassDocument; use ApiPlatform\Tests\Fixtures\TestBundle\Document\DummyDtoOutputSameClass as DummyDtoOutputSameClassDocument; use ApiPlatform\Tests\Fixtures\TestBundle\Dto\OutputDtoDummy; diff --git a/tests/Fixtures/TestBundle/DataTransformer/RPCOutputDataTransformer.php b/tests/Fixtures/TestBundle/DataTransformer/RPCOutputDataTransformer.php index e5f5a80808c..24d6bc442c0 100644 --- a/tests/Fixtures/TestBundle/DataTransformer/RPCOutputDataTransformer.php +++ b/tests/Fixtures/TestBundle/DataTransformer/RPCOutputDataTransformer.php @@ -13,7 +13,7 @@ namespace ApiPlatform\Tests\Fixtures\TestBundle\DataTransformer; -use ApiPlatform\DataTransformer\DataTransformerInterface; +use ApiPlatform\Core\DataTransformer\DataTransformerInterface; use ApiPlatform\Tests\Fixtures\TestBundle\Document\RPC as RPCDocument; use ApiPlatform\Tests\Fixtures\TestBundle\Dto\RPCOutput; use ApiPlatform\Tests\Fixtures\TestBundle\Entity\RPC; diff --git a/tests/Fixtures/TestBundle/DataTransformer/RecoverPasswordInputDataTransformer.php b/tests/Fixtures/TestBundle/DataTransformer/RecoverPasswordInputDataTransformer.php index 1acc5df9cfd..30113044dd5 100644 --- a/tests/Fixtures/TestBundle/DataTransformer/RecoverPasswordInputDataTransformer.php +++ b/tests/Fixtures/TestBundle/DataTransformer/RecoverPasswordInputDataTransformer.php @@ -13,7 +13,7 @@ namespace ApiPlatform\Tests\Fixtures\TestBundle\DataTransformer; -use ApiPlatform\DataTransformer\DataTransformerInterface; +use ApiPlatform\Core\DataTransformer\DataTransformerInterface; use ApiPlatform\Serializer\AbstractItemNormalizer; use ApiPlatform\Tests\Fixtures\TestBundle\Document\User as UserDocument; use ApiPlatform\Tests\Fixtures\TestBundle\Dto\RecoverPasswordInput; diff --git a/tests/Fixtures/TestBundle/DataTransformer/RecoverPasswordOutputDataTransformer.php b/tests/Fixtures/TestBundle/DataTransformer/RecoverPasswordOutputDataTransformer.php index e62154d8f3f..5a19c76b525 100644 --- a/tests/Fixtures/TestBundle/DataTransformer/RecoverPasswordOutputDataTransformer.php +++ b/tests/Fixtures/TestBundle/DataTransformer/RecoverPasswordOutputDataTransformer.php @@ -13,7 +13,7 @@ namespace ApiPlatform\Tests\Fixtures\TestBundle\DataTransformer; -use ApiPlatform\DataTransformer\DataTransformerInterface; +use ApiPlatform\Core\DataTransformer\DataTransformerInterface; use ApiPlatform\Tests\Fixtures\TestBundle\Dto\RecoverPasswordOutput; use ApiPlatform\Tests\Fixtures\TestBundle\Entity\Dummy; use ApiPlatform\Tests\Fixtures\TestBundle\Entity\User; diff --git a/tests/Serializer/AbstractItemNormalizerTest.php b/tests/Serializer/AbstractItemNormalizerTest.php index 05bc406f951..a0a26cf4619 100644 --- a/tests/Serializer/AbstractItemNormalizerTest.php +++ b/tests/Serializer/AbstractItemNormalizerTest.php @@ -13,23 +13,26 @@ namespace ApiPlatform\Tests\Serializer; +use ApiPlatform\Api\IriConverterInterface as NewIriConverterInterface; use ApiPlatform\Api\ResourceClassResolverInterface; use ApiPlatform\Core\Api\IriConverterInterface; use ApiPlatform\Core\DataProvider\ItemDataProviderInterface; +use ApiPlatform\Core\DataTransformer\DataTransformerInitializerInterface; +use ApiPlatform\Core\DataTransformer\DataTransformerInterface; use ApiPlatform\Core\Metadata\Property\Factory\PropertyMetadataFactoryInterface as LegacyPropertyMetadataFactoryInterface; use ApiPlatform\Core\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface; use ApiPlatform\Core\Metadata\Property\PropertyMetadata; use ApiPlatform\Core\Metadata\Property\PropertyNameCollection; -use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface; -use ApiPlatform\Core\Metadata\Resource\ResourceMetadata; use ApiPlatform\Core\Tests\ProphecyTrait; -use ApiPlatform\DataTransformer\DataTransformerInitializerInterface; -use ApiPlatform\DataTransformer\DataTransformerInterface; use ApiPlatform\Exception\InvalidArgumentException; use ApiPlatform\Exception\ItemNotFoundException; use ApiPlatform\Metadata\ApiProperty; +use ApiPlatform\Metadata\ApiResource; use ApiPlatform\Metadata\Get; +use ApiPlatform\Metadata\Operations; use ApiPlatform\Metadata\Property\Factory\PropertyMetadataFactoryInterface; +use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface; +use ApiPlatform\Metadata\Resource\ResourceMetadataCollection; use ApiPlatform\Serializer\AbstractItemNormalizer; use ApiPlatform\Symfony\Security\ResourceAccessCheckerInterface; use ApiPlatform\Tests\Fixtures\TestBundle\Dto\InputDto; @@ -43,6 +46,7 @@ use Doctrine\Common\Collections\ArrayCollection; use PHPUnit\Framework\TestCase; use Prophecy\Argument; +use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait; use Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException; use Symfony\Component\PropertyAccess\PropertyAccessorInterface; use Symfony\Component\PropertyInfo\Type; @@ -60,6 +64,7 @@ */ class AbstractItemNormalizerTest extends TestCase { + use ExpectDeprecationTrait; use ProphecyTrait; /** @@ -1445,16 +1450,19 @@ public function testDoNotDenormalizeRelationWithPlainIdWhenPlainIdentifiersAreNo * 4. Messenger is used, we send the `Dummy` * 5. The handler receives a `{Dummy}` json representation and tries to denormalize it * 6. Because it has an `input`, the `AbstractItemNormalizer` tries to denormalize it as a `InputDto` which is wrong, it's a `{Dummy}`. + * + * @group legacy */ public function testNormalizationWithDataTransformer() { + $this->expectDeprecation('Since api-platform/core 2.7: The DataTransformer pattern is deprecated, use a Provider or a Processor and either use your input or return a new output there.'); $propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class); $propertyNameCollectionFactoryProphecy->create(Dummy::class, Argument::any())->willReturn(new PropertyNameCollection(['name'])); $propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class); $propertyMetadataFactoryProphecy->create(Dummy::class, 'name', Argument::any())->willReturn((new ApiProperty())->withBuiltinTypes([new Type(Type::BUILTIN_TYPE_STRING)])->withDescription('')->withReadable(false)->withWritable(true)); - $iriConverterProphecy = $this->prophesize(IriConverterInterface::class); + $iriConverterProphecy = $this->prophesize(NewIriConverterInterface::class); $resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class); $resourceClassResolverProphecy->getResourceClass(null, Dummy::class)->willReturn(Dummy::class); @@ -1496,8 +1504,11 @@ public function testNormalizationWithDataTransformer() $itemDataProviderProphecy = $this->prophesize(ItemDataProviderInterface::class); - $resourceMetadataFactoryProphecy = $this->prophesize(ResourceMetadataFactoryInterface::class); - $resourceMetadataFactoryProphecy->create(Dummy::class)->willReturn(new ResourceMetadata('dummy', '', '', null, null, ['input' => ['class' => InputDto::class]])); + $resourceMetadataFactoryProphecy = $this->prophesize(ResourceMetadataCollectionFactoryInterface::class); + $resourceMetadataFactoryProphecy->create(Dummy::class)->willReturn(new ResourceMetadataCollection( + Dummy::class, + [(new ApiResource())->withOperations(new Operations([(new Get())->withShortName('dummy')->withInput(['class' => InputDto::class])]))] + )); $dataTransformerProphecy = $this->prophesize(DataTransformerInterface::class); $dataTransformerProphecy->supportsTransformation($jsonInput, Dummy::class, $context)->willReturn(true); diff --git a/tests/Serializer/ItemNormalizerTest.php b/tests/Serializer/ItemNormalizerTest.php index 5c296174197..462f78fcfd3 100644 --- a/tests/Serializer/ItemNormalizerTest.php +++ b/tests/Serializer/ItemNormalizerTest.php @@ -15,10 +15,10 @@ use ApiPlatform\Api\ResourceClassResolverInterface; use ApiPlatform\Core\Api\IriConverterInterface; +use ApiPlatform\Core\DataTransformer\DataTransformerInterface; use ApiPlatform\Core\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface; use ApiPlatform\Core\Metadata\Property\PropertyNameCollection; use ApiPlatform\Core\Tests\ProphecyTrait; -use ApiPlatform\DataTransformer\DataTransformerInterface; use ApiPlatform\Exception\InvalidArgumentException; use ApiPlatform\Metadata\ApiProperty; use ApiPlatform\Metadata\Property\Factory\PropertyMetadataFactoryInterface; @@ -27,6 +27,7 @@ use ApiPlatform\Tests\Fixtures\TestBundle\Entity\Dummy; use PHPUnit\Framework\TestCase; use Prophecy\Argument; +use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait; use Symfony\Component\Serializer\Exception\NotNormalizableValueException; use Symfony\Component\Serializer\Normalizer\DenormalizerInterface; use Symfony\Component\Serializer\Normalizer\NormalizerInterface; @@ -38,6 +39,7 @@ */ class ItemNormalizerTest extends TestCase { + use ExpectDeprecationTrait; use ProphecyTrait; /** @@ -282,8 +284,12 @@ public function testDenormalizeWithIdAndNoResourceClass() $this->assertSame('hello', $object->getName()); } + /** + * @group legacy + */ public function testNormalizeWithDataTransformers() { + $this->expectDeprecation('Since api-platform/core 2.7: The DataTransformer pattern is deprecated, use a Provider or a Processor and either use your input or return a new output there.'); $dummy = new Dummy(); $dummy->setName('hello'); $output = new OutputDto();