From 6bb193af423c4510a7dcd0e476e991a1b7db11af Mon Sep 17 00:00:00 2001 From: Lennart Tinkloh Date: Fri, 19 Jan 2024 10:42:47 +0100 Subject: [PATCH] BRAIN: Symfony 7 upgrade --- config/services/braintree.xml | 4 +++ phpstan.neon | 5 ---- .../Serializer/BraintreeNormalizer.php | 9 ++++++- .../Serializer/CollectionNormalizer.php | 12 +++++++-- src/Framework/Serializer/EntityNormalizer.php | 15 ++++++++--- src/Tests/Serializer/TestSerializer.php | 4 +-- .../Serializer/BraintreeNormalizerTest.php | 8 ++++++ .../Serializer/CollectionNormalizerTest.php | 11 ++++++++ .../Serializer/EntityNormalizerTest.php | 25 ++++++++++++++----- 9 files changed, 73 insertions(+), 20 deletions(-) diff --git a/config/services/braintree.xml b/config/services/braintree.xml index 4897001..6688fe8 100644 --- a/config/services/braintree.xml +++ b/config/services/braintree.xml @@ -15,5 +15,9 @@ controller.argument_value_resolver + + + + diff --git a/phpstan.neon b/phpstan.neon index acd6d23..12da0c3 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -59,7 +59,6 @@ rules: - Symplify\PHPStanRules\Rules\NoIssetOnObjectRule # explicit naming - - Symplify\PHPStanRules\Rules\NoParentMethodCallOnNoOverrideProcessRule - Symplify\PHPStanRules\Rules\ForbiddenMultipleClassLikeInOneFileRule - Symplify\PHPStanRules\Rules\Complexity\ForbiddenArrayMethodCallRule @@ -67,13 +66,9 @@ rules: # complexity rules - Symplify\PHPStanRules\Rules\Explicit\NoMixedPropertyFetcherRule - Symplify\PHPStanRules\Rules\Explicit\NoMixedMethodCallerRule - - Symplify\PHPStanRules\Rules\PHPUnit\NoRightPHPUnitAssertScalarRule - Symplify\PHPStanRules\Rules\NoDynamicNameRule # naming rules - Symplify\PHPStanRules\Rules\NoVoidGetterMethodRule - Symplify\PHPStanRules\Rules\UppercaseConstantRule - Symplify\PHPStanRules\Rules\CheckClassNamespaceFollowPsr4Rule - - # test rules - - Symplify\PHPStanRules\Rules\NoConstructorInTestRule diff --git a/src/Framework/Serializer/BraintreeNormalizer.php b/src/Framework/Serializer/BraintreeNormalizer.php index d01c6f1..9551f15 100644 --- a/src/Framework/Serializer/BraintreeNormalizer.php +++ b/src/Framework/Serializer/BraintreeNormalizer.php @@ -20,7 +20,7 @@ public function normalize(mixed $object, string $format = null, array $context = return \array_map(fn (Instance $braintreeObject) => $braintreeObject->toArray(), $object); } - public function supportsNormalization(mixed $data, string $format = null): bool + public function supportsNormalization(mixed $data, string $format = null, array $context = []): bool { if (!\is_array($data) || \count($data) === 0) { return false; @@ -34,4 +34,11 @@ public function supportsNormalization(mixed $data, string $format = null): bool return true; } + + public function getSupportedTypes(?string $format): array + { + return [ + Instance::class => true, + ]; + } } diff --git a/src/Framework/Serializer/CollectionNormalizer.php b/src/Framework/Serializer/CollectionNormalizer.php index de1c3e7..9cd55fa 100644 --- a/src/Framework/Serializer/CollectionNormalizer.php +++ b/src/Framework/Serializer/CollectionNormalizer.php @@ -30,7 +30,7 @@ public function normalize(mixed $object, string $format = null, array $context = })->toArray(); } - public function supportsNormalization(mixed $data, string $format = null): bool + public function supportsNormalization(mixed $data, string $format = null, array $context = []): bool { return $data instanceof Collection && $data->forAll(function ($_, $item) { return $item instanceof EntityInterface || $item instanceof AbstractShop; @@ -47,8 +47,16 @@ public function denormalize(mixed $data, string $type, string $format = null, ar return new ArrayCollection($data); } - public function supportsDenormalization(mixed $data, string $type, string $format = null): bool + public function supportsDenormalization(mixed $data, string $type, string $format = null, array $context = []): bool { return \is_subclass_of($type, Collection::class, true) && \is_array($data); } + + public function getSupportedTypes(?string $format): array + { + return [ + AbstractShop::class => true, + Collection::class => true, + ]; + } } diff --git a/src/Framework/Serializer/EntityNormalizer.php b/src/Framework/Serializer/EntityNormalizer.php index ed242e2..532b440 100644 --- a/src/Framework/Serializer/EntityNormalizer.php +++ b/src/Framework/Serializer/EntityNormalizer.php @@ -7,7 +7,6 @@ use Symfony\Component\DependencyInjection\Attribute\AutoconfigureTag; use Symfony\Component\Serializer\Exception\InvalidArgumentException; use Symfony\Component\Serializer\Normalizer\NormalizerInterface; -use Symfony\Component\Serializer\Normalizer\ObjectNormalizer; #[AutoconfigureTag(name: 'serializer.normalizer')] class EntityNormalizer implements NormalizerInterface @@ -17,7 +16,7 @@ class EntityNormalizer implements NormalizerInterface private const REGEX = '/.*(Swag.*)/'; public function __construct( - private readonly ObjectNormalizer $normalizer + private readonly NormalizerInterface $normalizer ) { } @@ -25,7 +24,7 @@ public function __construct( * @param EntityInterface|AbstractShop $object * @param array $context */ - public function normalize(mixed $object, string $format = null, array $context = []): mixed + public function normalize(mixed $object, string $format = null, array $context = []): string|array|\ArrayObject|bool|float|int|null { if (\array_key_exists(self::ORIGINAL_DATA, $context) && $context[self::ORIGINAL_DATA]) { $objectClass = $this->normalizeSwagNamespace($object::class); @@ -43,7 +42,7 @@ public function normalize(mixed $object, string $format = null, array $context = return $this->normalizer->normalize($object, $format, $context); } - public function supportsNormalization(mixed $data, string $format = null): mixed + public function supportsNormalization(mixed $data, string $format = null, array $context = []): bool { return $data instanceof EntityInterface || $data instanceof AbstractShop; } @@ -61,4 +60,12 @@ public function normalizeSwagNamespace(string $class): string return $matches[1]; } + + public function getSupportedTypes(?string $format): array + { + return [ + AbstractShop::class => true, + EntityInterface::class => true, + ]; + } } diff --git a/src/Tests/Serializer/TestSerializer.php b/src/Tests/Serializer/TestSerializer.php index e57b221..2598dcd 100644 --- a/src/Tests/Serializer/TestSerializer.php +++ b/src/Tests/Serializer/TestSerializer.php @@ -8,7 +8,7 @@ use Swag\Braintree\Framework\Serializer\EntityNormalizer; use Symfony\Component\Serializer\Encoder\JsonEncoder; use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory; -use Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader; +use Symfony\Component\Serializer\Mapping\Loader\AttributeLoader; use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer; use Symfony\Component\Serializer\Normalizer\ObjectNormalizer; use Symfony\Component\Serializer\Normalizer\ProblemNormalizer; @@ -21,7 +21,7 @@ class TestSerializer { public static function create(): Serializer { - $objectNormalizer = new ObjectNormalizer(new ClassMetadataFactory(new AnnotationLoader())); + $objectNormalizer = new ObjectNormalizer(new ClassMetadataFactory(new AttributeLoader())); return new Serializer([ new UidNormalizer(), diff --git a/tests/unit/Framework/Serializer/BraintreeNormalizerTest.php b/tests/unit/Framework/Serializer/BraintreeNormalizerTest.php index ca3e6ed..f039bf4 100644 --- a/tests/unit/Framework/Serializer/BraintreeNormalizerTest.php +++ b/tests/unit/Framework/Serializer/BraintreeNormalizerTest.php @@ -43,6 +43,14 @@ public function testSupportsNormalization(): void static::assertFalse($this->normalizer->supportsNormalization([$instance, $entity])); static::assertFalse($this->normalizer->supportsNormalization(null)); } + + public function testSupportedTypes(): void + { + static::assertSame( + [Instance::class => true], + $this->normalizer->getSupportedTypes(null) + ); + } } class TestInstance extends Instance diff --git a/tests/unit/Framework/Serializer/CollectionNormalizerTest.php b/tests/unit/Framework/Serializer/CollectionNormalizerTest.php index a560fcc..4ae864e 100644 --- a/tests/unit/Framework/Serializer/CollectionNormalizerTest.php +++ b/tests/unit/Framework/Serializer/CollectionNormalizerTest.php @@ -85,4 +85,15 @@ public function testSupportsDenormalization(): void static::assertFalse($this->normalizer->supportsDenormalization([], 'null')); static::assertFalse($this->normalizer->supportsDenormalization([], 'stdClass')); } + + public function testSupportedTypes(): void + { + static::assertSame( + [ + AbstractShop::class => true, + Collection::class => true, + ], + $this->normalizer->getSupportedTypes(null) + ); + } } diff --git a/tests/unit/Framework/Serializer/EntityNormalizerTest.php b/tests/unit/Framework/Serializer/EntityNormalizerTest.php index 5a3819d..bb0f3f0 100644 --- a/tests/unit/Framework/Serializer/EntityNormalizerTest.php +++ b/tests/unit/Framework/Serializer/EntityNormalizerTest.php @@ -11,6 +11,8 @@ use Swag\Braintree\Tests\Entity; use Swag\Braintree\Tests\IdsCollection; use Symfony\Component\Serializer\Exception\InvalidArgumentException; +use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer; +use Symfony\Component\Serializer\Normalizer\NormalizerInterface; use Symfony\Component\Serializer\Normalizer\ObjectNormalizer; #[CoversClass(EntityNormalizer::class)] @@ -22,11 +24,11 @@ class EntityNormalizerTest extends TestCase protected function setUp(): void { - $objectNormalizer = $this->createMock(ObjectNormalizer::class); + $objectNormalizer = $this->createMock(NormalizerInterface::class); $objectNormalizer ->expects(static::any()) ->method('normalize') - ->willReturnCallback(fn (mixed $object) => $object); + ->willReturnCallback(fn (mixed $object) => [$object]); $this->normalizer = new EntityNormalizer($objectNormalizer); $this->ids = new IdsCollection(); @@ -44,8 +46,8 @@ public function testNormalizeWithEntity(): void $entity = new Entity(); $entity->setId($this->ids->getUuid('entity')); - static::assertSame($entity, $this->normalizer->normalize($entity, null, [EntityNormalizer::ORIGINAL_DATA => $entity])); - static::assertSame($entity, $this->normalizer->normalize($entity)); + static::assertSame([$entity], $this->normalizer->normalize($entity, null, [EntityNormalizer::ORIGINAL_DATA => $entity])); + static::assertSame([$entity], $this->normalizer->normalize($entity)); static::assertSame($this->ids->get('entity'), $this->normalizer->normalize($entity, null, [EntityNormalizer::ORIGINAL_DATA => new ShopEntity('', '', '')])); } @@ -54,8 +56,8 @@ public function testNormalizeWithShop(): void { $shop = new ShopEntity($this->ids->get('shop'), '', ''); - static::assertSame($shop, $this->normalizer->normalize($shop, null, [EntityNormalizer::ORIGINAL_DATA => $shop])); - static::assertSame($shop, $this->normalizer->normalize($shop)); + static::assertSame([$shop], $this->normalizer->normalize($shop, null, [EntityNormalizer::ORIGINAL_DATA => $shop])); + static::assertSame([$shop], $this->normalizer->normalize($shop)); static::assertSame($this->ids->get('shop'), $this->normalizer->normalize($shop, null, [EntityNormalizer::ORIGINAL_DATA => new Entity()])); } @@ -82,4 +84,15 @@ public function testNormalizeNamespace(): void { static::assertSame('Swag\Entity\Braintree', $this->normalizer->normalizeSwagNamespace('Proxies\__CG__\Swag\Entity\Braintree')); } + + public function testSupportedTypes(): void + { + static::assertSame( + [ + AbstractShop::class => true, + EntityInterface::class => true, + ], + $this->normalizer->getSupportedTypes(null) + ); + } }