Skip to content

Commit

Permalink
normalizer
Browse files Browse the repository at this point in the history
  • Loading branch information
soyuka committed Nov 29, 2023
1 parent aacd21a commit 3fac8d2
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
use Symfony\Component\PropertyInfo\Extractor\SerializerExtractor;
use Symfony\Component\PropertyInfo\PropertyInfoExtractor;
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory;
use Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader;
use Symfony\Component\Serializer\Mapping\Loader\AttributeLoader;

/**
* @author Oskar Stark <oskarstark@googlemail.com>
Expand Down Expand Up @@ -81,7 +81,7 @@ public function testCreateMethodReturnsProperPropertyNameCollectionForObjectWith
new PropertyInfoExtractor([
new SerializerExtractor(
new ClassMetadataFactory(
new AnnotationLoader(
new AttributeLoader(
)
)
),
Expand Down
4 changes: 2 additions & 2 deletions tests/Elasticsearch/Serializer/ItemNormalizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,12 +150,12 @@ public function testGetSupportedTypes(): void

// TODO: use prophecy when getSupportedTypes() will be added to the interface
$this->itemNormalizer = new ItemNormalizer(new class() implements NormalizerInterface {
public function normalize(mixed $object, string $format = null, array $context = [])
public function normalize(mixed $object, ?string $format = null, array $context = []): \ArrayObject|array|string|int|float|bool|null
{
return null;
}

public function supportsNormalization(mixed $data, string $format = null): bool
public function supportsNormalization(mixed $data, string $format = null, array $context = []): bool
{
return true;
}
Expand Down
11 changes: 4 additions & 7 deletions tests/Fixtures/DummySequentiallyValidatedEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,10 @@ class DummySequentiallyValidatedEntity
{
/**
* @var string
*
* @Assert\Sequentially({
*
* @Assert\Length(min=1, max=32),
*
* @Assert\Regex(pattern="/^[a-z]$/")
* })
*/
#[Assert\Sequentially([
new Assert\Length(min: 1, max: 32),
new Assert\Regex(pattern: "/^[a-z]$/")
])]
public $dummy;
}
2 changes: 1 addition & 1 deletion tests/Fixtures/app/AppKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ protected function configureContainer(ContainerBuilder $c, LoaderInterface $load
'validation' => ['enable_attributes' => true, 'email_validation_mode' => 'html5'],
'serializer' => ['enable_attributes' => true],
'test' => null,
'session' => ['cookie_secure' => true, 'cookie_samesite' => 'lax', 'handler_id' => 'session.handler.native_file'],
'session' => class_exists(SessionFactory::class) ? ['storage_factory_id' => 'session.storage.factory.mock_file'] : ['storage_id' => 'session.storage.mock_file'],
'profiler' => [
'enabled' => true,
'collect' => false,
Expand Down
2 changes: 1 addition & 1 deletion tests/GraphQl/Resolver/Stage/SerializeStageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ public function testApplyBadNormalizedData(): void
$normalizationContext = ['normalization' => true];
$this->serializerContextBuilderProphecy->create($resourceClass, $operation, $context, true)->shouldBeCalled()->willReturn($normalizationContext);

$this->normalizerProphecy->normalize(Argument::type(\stdClass::class), ItemNormalizer::FORMAT, $normalizationContext)->willReturn(new \stdClass());
$this->normalizerProphecy->normalize(Argument::type(\stdClass::class), ItemNormalizer::FORMAT, $normalizationContext)->willReturn(0);

$this->expectException(\UnexpectedValueException::class);
$this->expectExceptionMessage('Expected serialized data to be a nullable array.');
Expand Down
4 changes: 2 additions & 2 deletions tests/Hal/Serializer/ItemNormalizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
use Symfony\Component\PropertyInfo\Type;
use Symfony\Component\Serializer\Exception\LogicException;
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\NameConverter\NameConverterInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
Expand Down Expand Up @@ -289,7 +289,7 @@ public function testMaxDepth(): void
$resourceClassResolverProphecy->reveal(),
null,
null,
new ClassMetadataFactory(new AnnotationLoader())
new ClassMetadataFactory(new AttributeLoader())
);
$serializer = new Serializer([$normalizer]);
$normalizer->setSerializer($serializer);
Expand Down
4 changes: 2 additions & 2 deletions tests/Hydra/Serializer/CollectionFiltersNormalizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -344,12 +344,12 @@ public function testGetSupportedTypes(): void
// TODO: use prophecy when getSupportedTypes() will be added to the interface
$normalizer = new CollectionFiltersNormalizer(
new class() implements NormalizerInterface {
public function normalize(mixed $object, string $format = null, array $context = [])
public function normalize(mixed $object, string $format = null, array $context = []): \ArrayObject|array|string|int|float|bool|null
{
return null;
}

public function supportsNormalization(mixed $data, string $format = null): bool
public function supportsNormalization(mixed $data, string $format = null, array $context = []): bool
{
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,12 +202,12 @@ public function testGetSupportedTypes(): void

// TODO: use prophecy when getSupportedTypes() will be added to the interface
$normalizer = new PartialCollectionViewNormalizer(new class() implements NormalizerInterface {
public function normalize(mixed $object, string $format = null, array $context = [])
public function normalize(mixed $object, string $format = null, array $context = []): \ArrayObject|array|string|int|float|bool|null
{
return null;
}

public function supportsNormalization(mixed $data, string $format = null): bool
public function supportsNormalization(mixed $data, string $format = null, array $context = []): bool
{
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,14 @@
use ApiPlatform\Tests\Fixtures\DummyValidatedEntity;
use ApiPlatform\Tests\Fixtures\DummyValidatedHostnameEntity;
use ApiPlatform\Tests\Fixtures\DummyValidatedUlidEntity;
use Doctrine\Common\Annotations\AnnotationReader;
use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;
use Symfony\Component\PropertyInfo\Type;
use Symfony\Component\Validator\Constraints\Hostname;
use Symfony\Component\Validator\Constraints\Ulid;
use Symfony\Component\Validator\Mapping\ClassMetadata;
use Symfony\Component\Validator\Mapping\Factory\MetadataFactoryInterface;
use Symfony\Component\Validator\Mapping\Loader\AnnotationLoader;
use Symfony\Component\Validator\Mapping\Loader\AttributeLoader;

/**
* @author Baptiste Meyer <baptiste.meyer@gmail.com>
Expand All @@ -64,7 +63,7 @@ class ValidatorPropertyMetadataFactoryTest extends TestCase
protected function setUp(): void
{
$this->validatorClassMetadata = new ClassMetadata(DummyValidatedEntity::class);
(new AnnotationLoader(new AnnotationReader()))->loadClassMetadata($this->validatorClassMetadata);
(new AttributeLoader())->loadClassMetadata($this->validatorClassMetadata);
}

public function testCreateWithPropertyWithRequiredConstraints(): void
Expand Down Expand Up @@ -218,7 +217,7 @@ public function testCreateWithRequiredByDecorated(): void
public function testCreateWithPropertyWithValidationConstraints(): void
{
$validatorClassMetadata = new ClassMetadata(DummyIriWithValidationEntity::class);
(new AnnotationLoader(new AnnotationReader()))->loadClassMetadata($validatorClassMetadata);
(new AttributeLoader())->loadClassMetadata($validatorClassMetadata);

$types = [
'dummyUrl' => 'https://schema.org/url',
Expand Down Expand Up @@ -260,7 +259,7 @@ public function testCreateWithPropertyWithValidationConstraints(): void
public function testCreateWithPropertyLengthRestriction(): void
{
$validatorClassMetadata = new ClassMetadata(DummyValidatedEntity::class);
(new AnnotationLoader(new AnnotationReader()))->loadClassMetadata($validatorClassMetadata);
(new AttributeLoader())->loadClassMetadata($validatorClassMetadata);

$validatorMetadataFactory = $this->prophesize(MetadataFactoryInterface::class);
$validatorMetadataFactory->getMetadataFor(DummyValidatedEntity::class)
Expand All @@ -287,7 +286,7 @@ public function testCreateWithPropertyLengthRestriction(): void
public function testCreateWithPropertyRegexRestriction(): void
{
$validatorClassMetadata = new ClassMetadata(DummyValidatedEntity::class);
(new AnnotationLoader(new AnnotationReader()))->loadClassMetadata($validatorClassMetadata);
(new AttributeLoader())->loadClassMetadata($validatorClassMetadata);

$validatorMetadataFactory = $this->prophesize(MetadataFactoryInterface::class);
$validatorMetadataFactory->getMetadataFor(DummyValidatedEntity::class)
Expand Down Expand Up @@ -316,7 +315,7 @@ public function testCreateWithPropertyRegexRestriction(): void
public function testCreateWithPropertyFormatRestriction(string $property, string $class, array $expectedSchema): void
{
$validatorClassMetadata = new ClassMetadata($class);
(new AnnotationLoader(new AnnotationReader()))->loadClassMetadata($validatorClassMetadata);
(new AttributeLoader())->loadClassMetadata($validatorClassMetadata);

$validatorMetadataFactory = $this->prophesize(MetadataFactoryInterface::class);
$validatorMetadataFactory->getMetadataFor($class)
Expand Down Expand Up @@ -355,7 +354,7 @@ public static function providePropertySchemaFormatCases(): \Generator
public function testCreateWithSequentiallyConstraint(): void
{
$validatorClassMetadata = new ClassMetadata(DummySequentiallyValidatedEntity::class);
(new AnnotationLoader(new AnnotationReader()))->loadClassMetadata($validatorClassMetadata);
(new AttributeLoader())->loadClassMetadata($validatorClassMetadata);

$validatorMetadataFactory = $this->prophesize(MetadataFactoryInterface::class);
$validatorMetadataFactory->getMetadataFor(DummySequentiallyValidatedEntity::class)
Expand All @@ -382,7 +381,7 @@ public function testCreateWithSequentiallyConstraint(): void
public function testCreateWithCompoundConstraint(): void
{
$validatorClassMetadata = new ClassMetadata(DummyCompoundValidatedEntity::class);
(new AnnotationLoader(new AnnotationReader()))->loadClassMetadata($validatorClassMetadata);
(new AttributeLoader())->loadClassMetadata($validatorClassMetadata);

$validatorMetadataFactory = $this->prophesize(MetadataFactoryInterface::class);
$validatorMetadataFactory->getMetadataFor(DummyCompoundValidatedEntity::class)
Expand All @@ -409,7 +408,7 @@ public function testCreateWithCompoundConstraint(): void
public function testCreateWithAtLeastOneOfConstraint(): void
{
$validatorClassMetadata = new ClassMetadata(DummyAtLeastOneOfValidatedEntity::class);
(new AnnotationLoader(new AnnotationReader()))->loadClassMetadata($validatorClassMetadata);
(new AttributeLoader())->loadClassMetadata($validatorClassMetadata);

$validatorMetadataFactory = $this->prophesize(MetadataFactoryInterface::class);
$validatorMetadataFactory->getMetadataFor(DummyAtLeastOneOfValidatedEntity::class)
Expand Down Expand Up @@ -440,7 +439,7 @@ public function testCreateWithAtLeastOneOfConstraint(): void
public function testCreateWithPropertyUniqueRestriction(): void
{
$validatorClassMetadata = new ClassMetadata(DummyUniqueValidatedEntity::class);
(new AnnotationLoader(new AnnotationReader()))->loadClassMetadata($validatorClassMetadata);
(new AttributeLoader())->loadClassMetadata($validatorClassMetadata);

$validatorMetadataFactory = $this->prophesize(MetadataFactoryInterface::class);
$validatorMetadataFactory->getMetadataFor(DummyUniqueValidatedEntity::class)
Expand Down Expand Up @@ -469,7 +468,7 @@ public function testCreateWithPropertyUniqueRestriction(): void
public function testCreateWithRangeConstraint(Type $type, string $property, array $expectedSchema): void
{
$validatorClassMetadata = new ClassMetadata(DummyRangeValidatedEntity::class);
(new AnnotationLoader(new AnnotationReader()))->loadClassMetadata($validatorClassMetadata);
(new AttributeLoader())->loadClassMetadata($validatorClassMetadata);

$validatorMetadataFactory = $this->prophesize(MetadataFactoryInterface::class);
$validatorMetadataFactory->getMetadataFor(DummyRangeValidatedEntity::class)
Expand Down Expand Up @@ -506,7 +505,7 @@ public static function provideRangeConstraintCases(): \Generator
public function testCreateWithPropertyChoiceRestriction(ApiProperty $propertyMetadata, string $property, array $expectedSchema): void
{
$validatorClassMetadata = new ClassMetadata(DummyValidatedChoiceEntity::class);
(new AnnotationLoader(new AnnotationReader()))->loadClassMetadata($validatorClassMetadata);
(new AttributeLoader())->loadClassMetadata($validatorClassMetadata);

$validatorMetadataFactory = $this->prophesize(MetadataFactoryInterface::class);
$validatorMetadataFactory->getMetadataFor(DummyValidatedChoiceEntity::class)
Expand Down Expand Up @@ -545,7 +544,7 @@ public static function provideChoiceConstraintCases(): \Generator
public function testCreateWithPropertyCountRestriction(string $property, array $expectedSchema): void
{
$validatorClassMetadata = new ClassMetadata(DummyCountValidatedEntity::class);
(new AnnotationLoader(new AnnotationReader()))->loadClassMetadata($validatorClassMetadata);
(new AttributeLoader())->loadClassMetadata($validatorClassMetadata);

$validatorMetadataFactory = $this->prophesize(MetadataFactoryInterface::class);
$validatorMetadataFactory->getMetadataFor(DummyCountValidatedEntity::class)
Expand Down Expand Up @@ -577,7 +576,7 @@ public static function provideCountConstraintCases(): \Generator
public function testCreateWithPropertyCollectionRestriction(): void
{
$validatorClassMetadata = new ClassMetadata(DummyCollectionValidatedEntity::class);
(new AnnotationLoader(new AnnotationReader()))->loadClassMetadata($validatorClassMetadata);
(new AttributeLoader())->loadClassMetadata($validatorClassMetadata);

$validatorMetadataFactory = $this->prophesize(MetadataFactoryInterface::class);
$validatorMetadataFactory->getMetadataFor(DummyCollectionValidatedEntity::class)
Expand Down Expand Up @@ -643,7 +642,7 @@ public function testCreateWithPropertyCollectionRestriction(): void
public function testCreateWithPropertyNumericRestriction(ApiProperty $propertyMetadata, string $property, array $expectedSchema): void
{
$validatorClassMetadata = new ClassMetadata(DummyNumericValidatedEntity::class);
(new AnnotationLoader(new AnnotationReader()))->loadClassMetadata($validatorClassMetadata);
(new AttributeLoader())->loadClassMetadata($validatorClassMetadata);

$validatorMetadataFactory = $this->prophesize(MetadataFactoryInterface::class);
$validatorMetadataFactory->getMetadataFor(DummyNumericValidatedEntity::class)
Expand Down

0 comments on commit 3fac8d2

Please sign in to comment.