diff --git a/.github/patch-types.php b/.github/patch-types.php index 3c91c7f580..ec4f35655a 100644 --- a/.github/patch-types.php +++ b/.github/patch-types.php @@ -35,6 +35,7 @@ foreach ($loader->getClassMap() as $class => $file) { case false !== strpos($file, '/src/Symfony/Component/PropertyInfo/Tests/Fixtures/Php80Dummy.php'): case false !== strpos($file, '/src/Symfony/Component/Routing/Tests/Fixtures/AttributeFixtures'): case false !== strpos($file, '/src/Symfony/Component/Serializer/Tests/Normalizer/Features/ObjectOuter.php'): + case false !== strpos($file, '/src/Symfony/Component/Serializer/Tests/Fixtures/Attributes/'): case false !== strpos($file, '/src/Symfony/Component/VarDumper/Tests/Fixtures/LotsOfAttributes.php'): case false !== strpos($file, '/src/Symfony/Component/Validator/Tests/Fixtures/Attribute/'): case false !== strpos($file, '/src/Symfony/Component/VarDumper/Tests/Fixtures/MyAttribute.php'): diff --git a/src/Symfony/Component/Serializer/Annotation/DiscriminatorMap.php b/src/Symfony/Component/Serializer/Annotation/DiscriminatorMap.php index c1184a53a5..18df1c38d6 100644 --- a/src/Symfony/Component/Serializer/Annotation/DiscriminatorMap.php +++ b/src/Symfony/Component/Serializer/Annotation/DiscriminatorMap.php @@ -21,6 +21,7 @@ use Symfony\Component\Serializer\Exception\InvalidArgumentException; * * @author Samuel Roze */ +#[\Attribute(\Attribute::TARGET_CLASS)] class DiscriminatorMap { /** @@ -34,20 +35,29 @@ class DiscriminatorMap private $mapping; /** + * @param string|array $typeProperty + * * @throws InvalidArgumentException */ - public function __construct(array $data) + public function __construct($typeProperty, array $mapping = null) { - if (empty($data['typeProperty'])) { + if (\is_array($typeProperty)) { + $mapping = $typeProperty['mapping'] ?? null; + $typeProperty = $typeProperty['typeProperty'] ?? null; + } elseif (!\is_string($typeProperty)) { + throw new \TypeError(sprintf('"%s": Argument $typeProperty was expected to be a string or array, got "%s".', __METHOD__, get_debug_type($typeProperty))); + } + + if (empty($typeProperty)) { throw new InvalidArgumentException(sprintf('Parameter "typeProperty" of annotation "%s" cannot be empty.', static::class)); } - if (empty($data['mapping'])) { + if (empty($mapping)) { throw new InvalidArgumentException(sprintf('Parameter "mapping" of annotation "%s" cannot be empty.', static::class)); } - $this->typeProperty = $data['typeProperty']; - $this->mapping = $data['mapping']; + $this->typeProperty = $typeProperty; + $this->mapping = $mapping; } public function getTypeProperty(): string diff --git a/src/Symfony/Component/Serializer/Annotation/Groups.php b/src/Symfony/Component/Serializer/Annotation/Groups.php index 4358a3e26d..8f1e1d94f9 100644 --- a/src/Symfony/Component/Serializer/Annotation/Groups.php +++ b/src/Symfony/Component/Serializer/Annotation/Groups.php @@ -21,6 +21,7 @@ use Symfony\Component\Serializer\Exception\InvalidArgumentException; * * @author Kévin Dunglas */ +#[\Attribute(\Attribute::TARGET_METHOD | \Attribute::TARGET_PROPERTY)] class Groups { /** @@ -31,20 +32,22 @@ class Groups /** * @throws InvalidArgumentException */ - public function __construct(array $data) + public function __construct(array $groups) { - if (!isset($data['value']) || !$data['value']) { + if (isset($groups['value'])) { + $groups = (array) $groups['value']; + } + if (empty($groups)) { throw new InvalidArgumentException(sprintf('Parameter of annotation "%s" cannot be empty.', static::class)); } - $value = (array) $data['value']; - foreach ($value as $group) { + foreach ($groups as $group) { if (!\is_string($group)) { throw new InvalidArgumentException(sprintf('Parameter of annotation "%s" must be a string or an array of strings.', static::class)); } } - $this->groups = $value; + $this->groups = $groups; } /** diff --git a/src/Symfony/Component/Serializer/Annotation/Ignore.php b/src/Symfony/Component/Serializer/Annotation/Ignore.php index 313c7f7947..b09e7007a4 100644 --- a/src/Symfony/Component/Serializer/Annotation/Ignore.php +++ b/src/Symfony/Component/Serializer/Annotation/Ignore.php @@ -19,6 +19,7 @@ namespace Symfony\Component\Serializer\Annotation; * * @author Kévin Dunglas */ +#[\Attribute(\Attribute::TARGET_METHOD | \Attribute::TARGET_PROPERTY)] final class Ignore { } diff --git a/src/Symfony/Component/Serializer/Annotation/MaxDepth.php b/src/Symfony/Component/Serializer/Annotation/MaxDepth.php index 9939fdab1c..97d29869a0 100644 --- a/src/Symfony/Component/Serializer/Annotation/MaxDepth.php +++ b/src/Symfony/Component/Serializer/Annotation/MaxDepth.php @@ -21,6 +21,7 @@ use Symfony\Component\Serializer\Exception\InvalidArgumentException; * * @author Kévin Dunglas */ +#[\Attribute(\Attribute::TARGET_METHOD | \Attribute::TARGET_PROPERTY)] class MaxDepth { /** @@ -28,17 +29,23 @@ class MaxDepth */ private $maxDepth; - public function __construct(array $data) + /** + * @param int|array $maxDepth + */ + public function __construct($maxDepth) { - if (!isset($data['value'])) { - throw new InvalidArgumentException(sprintf('Parameter of annotation "%s" should be set.', static::class)); + if (\is_array($maxDepth)) { + if (!isset($maxDepth['value'])) { + throw new InvalidArgumentException(sprintf('Parameter of annotation "%s" should be set.', static::class)); + } + $maxDepth = $maxDepth['value']; } - if (!\is_int($data['value']) || $data['value'] <= 0) { + if (!\is_int($maxDepth) || $maxDepth <= 0) { throw new InvalidArgumentException(sprintf('Parameter of annotation "%s" must be a positive integer.', static::class)); } - $this->maxDepth = $data['value']; + $this->maxDepth = $maxDepth; } public function getMaxDepth() diff --git a/src/Symfony/Component/Serializer/Annotation/SerializedName.php b/src/Symfony/Component/Serializer/Annotation/SerializedName.php index 747c8c55f1..d621d9903d 100644 --- a/src/Symfony/Component/Serializer/Annotation/SerializedName.php +++ b/src/Symfony/Component/Serializer/Annotation/SerializedName.php @@ -21,6 +21,7 @@ use Symfony\Component\Serializer\Exception\InvalidArgumentException; * * @author Fabien Bourigault */ +#[\Attribute(\Attribute::TARGET_METHOD | \Attribute::TARGET_PROPERTY)] final class SerializedName { /** @@ -28,17 +29,23 @@ final class SerializedName */ private $serializedName; - public function __construct(array $data) + /** + * @param string|array $serializedName + */ + public function __construct($serializedName) { - if (!isset($data['value'])) { - throw new InvalidArgumentException(sprintf('Parameter of annotation "%s" should be set.', static::class)); + if (\is_array($serializedName)) { + if (!isset($serializedName['value'])) { + throw new InvalidArgumentException(sprintf('Parameter of annotation "%s" should be set.', static::class)); + } + $serializedName = $serializedName['value']; } - if (!\is_string($data['value']) || empty($data['value'])) { + if (!\is_string($serializedName) || empty($serializedName)) { throw new InvalidArgumentException(sprintf('Parameter of annotation "%s" must be a non-empty string.', static::class)); } - $this->serializedName = $data['value']; + $this->serializedName = $serializedName; } public function getSerializedName(): string diff --git a/src/Symfony/Component/Serializer/CHANGELOG.md b/src/Symfony/Component/Serializer/CHANGELOG.md index 0fc8fd2c68..97ae3fd62f 100644 --- a/src/Symfony/Component/Serializer/CHANGELOG.md +++ b/src/Symfony/Component/Serializer/CHANGELOG.md @@ -8,6 +8,7 @@ CHANGELOG * added `UidNormalizer` * added `FormErrorNormalizer` * added `MimeMessageNormalizer` + * serializer mapping can be configured using php attributes 5.1.0 ----- diff --git a/src/Symfony/Component/Serializer/Mapping/Loader/AnnotationLoader.php b/src/Symfony/Component/Serializer/Mapping/Loader/AnnotationLoader.php index 978fe659bb..9409bca360 100644 --- a/src/Symfony/Component/Serializer/Mapping/Loader/AnnotationLoader.php +++ b/src/Symfony/Component/Serializer/Mapping/Loader/AnnotationLoader.php @@ -26,12 +26,21 @@ use Symfony\Component\Serializer\Mapping\ClassMetadataInterface; * Annotation loader. * * @author Kévin Dunglas + * @author Alexander M. Turek */ class AnnotationLoader implements LoaderInterface { + private const KNOWN_ANNOTATIONS = [ + DiscriminatorMap::class => true, + Groups::class => true, + Ignore:: class => true, + MaxDepth::class => true, + SerializedName::class => true, + ]; + private $reader; - public function __construct(Reader $reader) + public function __construct(Reader $reader = null) { $this->reader = $reader; } @@ -47,7 +56,7 @@ class AnnotationLoader implements LoaderInterface $attributesMetadata = $classMetadata->getAttributesMetadata(); - foreach ($this->reader->getClassAnnotations($reflectionClass) as $annotation) { + foreach ($this->loadAnnotations($reflectionClass) as $annotation) { if ($annotation instanceof DiscriminatorMap) { $classMetadata->setClassDiscriminatorMapping(new ClassDiscriminatorMapping( $annotation->getTypeProperty(), @@ -63,7 +72,7 @@ class AnnotationLoader implements LoaderInterface } if ($property->getDeclaringClass()->name === $className) { - foreach ($this->reader->getPropertyAnnotations($property) as $annotation) { + foreach ($this->loadAnnotations($property) as $annotation) { if ($annotation instanceof Groups) { foreach ($annotation->getGroups() as $group) { $attributesMetadata[$property->name]->addGroup($group); @@ -98,7 +107,7 @@ class AnnotationLoader implements LoaderInterface } } - foreach ($this->reader->getMethodAnnotations($method) as $annotation) { + foreach ($this->loadAnnotations($method) as $annotation) { if ($annotation instanceof Groups) { if (!$accessorOrMutator) { throw new MappingException(sprintf('Groups on "%s::%s" cannot be added. Groups can only be added on methods beginning with "get", "is", "has" or "set".', $className, $method->name)); @@ -129,4 +138,32 @@ class AnnotationLoader implements LoaderInterface return $loaded; } + + /** + * @param \ReflectionClass|\ReflectionMethod|\ReflectionProperty $reflector + */ + public function loadAnnotations(object $reflector): iterable + { + if (\PHP_VERSION_ID >= 80000) { + foreach ($reflector->getAttributes() as $attribute) { + if (self::KNOWN_ANNOTATIONS[$attribute->getName()] ?? false) { + yield $attribute->newInstance(); + } + } + } + + if (null === $this->reader) { + return; + } + + if ($reflector instanceof \ReflectionClass) { + yield from $this->reader->getClassAnnotations($reflector); + } + if ($reflector instanceof \ReflectionMethod) { + yield from $this->reader->getMethodAnnotations($reflector); + } + if ($reflector instanceof \ReflectionProperty) { + yield from $this->reader->getPropertyAnnotations($reflector); + } + } } diff --git a/src/Symfony/Component/Serializer/Tests/Fixtures/AbstractDummy.php b/src/Symfony/Component/Serializer/Tests/Fixtures/Annotations/AbstractDummy.php similarity index 58% rename from src/Symfony/Component/Serializer/Tests/Fixtures/AbstractDummy.php rename to src/Symfony/Component/Serializer/Tests/Fixtures/Annotations/AbstractDummy.php index 4ae9812e7c..fad842b0d4 100644 --- a/src/Symfony/Component/Serializer/Tests/Fixtures/AbstractDummy.php +++ b/src/Symfony/Component/Serializer/Tests/Fixtures/Annotations/AbstractDummy.php @@ -9,15 +9,15 @@ * file that was distributed with this source code. */ -namespace Symfony\Component\Serializer\Tests\Fixtures; +namespace Symfony\Component\Serializer\Tests\Fixtures\Annotations; use Symfony\Component\Serializer\Annotation\DiscriminatorMap; /** * @DiscriminatorMap(typeProperty="type", mapping={ - * "first"="Symfony\Component\Serializer\Tests\Fixtures\AbstractDummyFirstChild", - * "second"="Symfony\Component\Serializer\Tests\Fixtures\AbstractDummySecondChild", - * "third"="Symfony\Component\Serializer\Tests\Fixtures\AbstractDummyThirdChild", + * "first"="Symfony\Component\Serializer\Tests\Fixtures\Annotations\AbstractDummyFirstChild", + * "second"="Symfony\Component\Serializer\Tests\Fixtures\Annotations\AbstractDummySecondChild", + * "third"="Symfony\Component\Serializer\Tests\Fixtures\Annotations\AbstractDummyThirdChild", * }) */ abstract class AbstractDummy diff --git a/src/Symfony/Component/Serializer/Tests/Fixtures/Annotations/AbstractDummyFirstChild.php b/src/Symfony/Component/Serializer/Tests/Fixtures/Annotations/AbstractDummyFirstChild.php new file mode 100644 index 0000000000..81f15aaf0d --- /dev/null +++ b/src/Symfony/Component/Serializer/Tests/Fixtures/Annotations/AbstractDummyFirstChild.php @@ -0,0 +1,39 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Serializer\Tests\Fixtures\Annotations; + +use Symfony\Component\Serializer\Tests\Fixtures\DummyFirstChildQuux; + +class AbstractDummyFirstChild extends AbstractDummy +{ + public $bar; + + /** @var DummyFirstChildQuux|null */ + public $quux; + + public function __construct($foo = null, $bar = null) + { + parent::__construct($foo); + + $this->bar = $bar; + } + + public function getQuux(): ?DummyFirstChildQuux + { + return $this->quux; + } + + public function setQuux(DummyFirstChildQuux $quux): void + { + $this->quux = $quux; + } +} diff --git a/src/Symfony/Component/Serializer/Tests/Fixtures/Annotations/AbstractDummySecondChild.php b/src/Symfony/Component/Serializer/Tests/Fixtures/Annotations/AbstractDummySecondChild.php new file mode 100644 index 0000000000..4d113066bb --- /dev/null +++ b/src/Symfony/Component/Serializer/Tests/Fixtures/Annotations/AbstractDummySecondChild.php @@ -0,0 +1,39 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Serializer\Tests\Fixtures\Annotations; + +use Symfony\Component\Serializer\Tests\Fixtures\DummySecondChildQuux; + +class AbstractDummySecondChild extends AbstractDummy +{ + public $baz; + + /** @var DummySecondChildQuux|null */ + public $quux; + + public function __construct($foo = null, $baz = null) + { + parent::__construct($foo); + + $this->baz = $baz; + } + + public function getQuux(): ?DummySecondChildQuux + { + return $this->quux; + } + + public function setQuux(DummySecondChildQuux $quux): void + { + $this->quux = $quux; + } +} diff --git a/src/Symfony/Component/Serializer/Tests/Fixtures/Annotations/AbstractDummyThirdChild.php b/src/Symfony/Component/Serializer/Tests/Fixtures/Annotations/AbstractDummyThirdChild.php new file mode 100644 index 0000000000..2df26c3320 --- /dev/null +++ b/src/Symfony/Component/Serializer/Tests/Fixtures/Annotations/AbstractDummyThirdChild.php @@ -0,0 +1,16 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Serializer\Tests\Fixtures\Annotations; + +final class AbstractDummyThirdChild extends AbstractDummyFirstChild +{ +} diff --git a/src/Symfony/Component/Serializer/Tests/Fixtures/GroupDummy.php b/src/Symfony/Component/Serializer/Tests/Fixtures/Annotations/GroupDummy.php similarity index 90% rename from src/Symfony/Component/Serializer/Tests/Fixtures/GroupDummy.php rename to src/Symfony/Component/Serializer/Tests/Fixtures/Annotations/GroupDummy.php index 37bfa7eb3f..cf26b6e0da 100644 --- a/src/Symfony/Component/Serializer/Tests/Fixtures/GroupDummy.php +++ b/src/Symfony/Component/Serializer/Tests/Fixtures/Annotations/GroupDummy.php @@ -9,9 +9,10 @@ * file that was distributed with this source code. */ -namespace Symfony\Component\Serializer\Tests\Fixtures; +namespace Symfony\Component\Serializer\Tests\Fixtures\Annotations; use Symfony\Component\Serializer\Annotation\Groups; +use Symfony\Component\Serializer\Tests\Fixtures\GroupDummyInterface; /** * @author Kévin Dunglas diff --git a/src/Symfony/Component/Serializer/Tests/Fixtures/Annotations/GroupDummyChild.php b/src/Symfony/Component/Serializer/Tests/Fixtures/Annotations/GroupDummyChild.php new file mode 100644 index 0000000000..c8845736ac --- /dev/null +++ b/src/Symfony/Component/Serializer/Tests/Fixtures/Annotations/GroupDummyChild.php @@ -0,0 +1,33 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Serializer\Tests\Fixtures\Annotations; + +class GroupDummyChild extends GroupDummy +{ + private $baz; + + /** + * @return mixed + */ + public function getBaz() + { + return $this->baz; + } + + /** + * @param mixed $baz + */ + public function setBaz($baz) + { + $this->baz = $baz; + } +} diff --git a/src/Symfony/Component/Serializer/Tests/Fixtures/GroupDummyParent.php b/src/Symfony/Component/Serializer/Tests/Fixtures/Annotations/GroupDummyParent.php similarity index 92% rename from src/Symfony/Component/Serializer/Tests/Fixtures/GroupDummyParent.php rename to src/Symfony/Component/Serializer/Tests/Fixtures/Annotations/GroupDummyParent.php index dd24233993..77d539b940 100644 --- a/src/Symfony/Component/Serializer/Tests/Fixtures/GroupDummyParent.php +++ b/src/Symfony/Component/Serializer/Tests/Fixtures/Annotations/GroupDummyParent.php @@ -9,7 +9,7 @@ * file that was distributed with this source code. */ -namespace Symfony\Component\Serializer\Tests\Fixtures; +namespace Symfony\Component\Serializer\Tests\Fixtures\Annotations; use Symfony\Component\Serializer\Annotation\Groups; diff --git a/src/Symfony/Component/Serializer/Tests/Fixtures/IgnoreDummy.php b/src/Symfony/Component/Serializer/Tests/Fixtures/Annotations/IgnoreDummy.php similarity index 89% rename from src/Symfony/Component/Serializer/Tests/Fixtures/IgnoreDummy.php rename to src/Symfony/Component/Serializer/Tests/Fixtures/Annotations/IgnoreDummy.php index a272b4c6fe..900447c581 100644 --- a/src/Symfony/Component/Serializer/Tests/Fixtures/IgnoreDummy.php +++ b/src/Symfony/Component/Serializer/Tests/Fixtures/Annotations/IgnoreDummy.php @@ -9,7 +9,7 @@ * file that was distributed with this source code. */ -namespace Symfony\Component\Serializer\Tests\Fixtures; +namespace Symfony\Component\Serializer\Tests\Fixtures\Annotations; use Symfony\Component\Serializer\Annotation\Ignore; diff --git a/src/Symfony/Component/Serializer/Tests/Fixtures/MaxDepthDummy.php b/src/Symfony/Component/Serializer/Tests/Fixtures/Annotations/MaxDepthDummy.php similarity index 91% rename from src/Symfony/Component/Serializer/Tests/Fixtures/MaxDepthDummy.php rename to src/Symfony/Component/Serializer/Tests/Fixtures/Annotations/MaxDepthDummy.php index 92a4045f1e..12be2db03b 100644 --- a/src/Symfony/Component/Serializer/Tests/Fixtures/MaxDepthDummy.php +++ b/src/Symfony/Component/Serializer/Tests/Fixtures/Annotations/MaxDepthDummy.php @@ -9,7 +9,7 @@ * file that was distributed with this source code. */ -namespace Symfony\Component\Serializer\Tests\Fixtures; +namespace Symfony\Component\Serializer\Tests\Fixtures\Annotations; use Symfony\Component\Serializer\Annotation\MaxDepth; diff --git a/src/Symfony/Component/Serializer/Tests/Fixtures/SerializedNameDummy.php b/src/Symfony/Component/Serializer/Tests/Fixtures/Annotations/SerializedNameDummy.php similarity index 91% rename from src/Symfony/Component/Serializer/Tests/Fixtures/SerializedNameDummy.php rename to src/Symfony/Component/Serializer/Tests/Fixtures/Annotations/SerializedNameDummy.php index 6fed9d201a..1eaa579b46 100644 --- a/src/Symfony/Component/Serializer/Tests/Fixtures/SerializedNameDummy.php +++ b/src/Symfony/Component/Serializer/Tests/Fixtures/Annotations/SerializedNameDummy.php @@ -9,7 +9,7 @@ * file that was distributed with this source code. */ -namespace Symfony\Component\Serializer\Tests\Fixtures; +namespace Symfony\Component\Serializer\Tests\Fixtures\Annotations; use Symfony\Component\Serializer\Annotation\SerializedName; diff --git a/src/Symfony/Component/Serializer/Tests/Fixtures/Attributes/AbstractDummy.php b/src/Symfony/Component/Serializer/Tests/Fixtures/Attributes/AbstractDummy.php new file mode 100644 index 0000000000..2e66f465b3 --- /dev/null +++ b/src/Symfony/Component/Serializer/Tests/Fixtures/Attributes/AbstractDummy.php @@ -0,0 +1,29 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Serializer\Tests\Fixtures\Attributes; + +use Symfony\Component\Serializer\Annotation\DiscriminatorMap; + +#[DiscriminatorMap(typeProperty: 'type', mapping: [ + 'first' => AbstractDummyFirstChild::class, + 'second' => AbstractDummySecondChild::class, + 'third' => AbstractDummyThirdChild::class, +])] +abstract class AbstractDummy +{ + public $foo; + + public function __construct($foo = null) + { + $this->foo = $foo; + } +} diff --git a/src/Symfony/Component/Serializer/Tests/Fixtures/AbstractDummyFirstChild.php b/src/Symfony/Component/Serializer/Tests/Fixtures/Attributes/AbstractDummyFirstChild.php similarity index 83% rename from src/Symfony/Component/Serializer/Tests/Fixtures/AbstractDummyFirstChild.php rename to src/Symfony/Component/Serializer/Tests/Fixtures/Attributes/AbstractDummyFirstChild.php index 20672c39b5..b5cd2faba2 100644 --- a/src/Symfony/Component/Serializer/Tests/Fixtures/AbstractDummyFirstChild.php +++ b/src/Symfony/Component/Serializer/Tests/Fixtures/Attributes/AbstractDummyFirstChild.php @@ -9,7 +9,9 @@ * file that was distributed with this source code. */ -namespace Symfony\Component\Serializer\Tests\Fixtures; +namespace Symfony\Component\Serializer\Tests\Fixtures\Attributes; + +use Symfony\Component\Serializer\Tests\Fixtures\DummyFirstChildQuux; class AbstractDummyFirstChild extends AbstractDummy { diff --git a/src/Symfony/Component/Serializer/Tests/Fixtures/AbstractDummySecondChild.php b/src/Symfony/Component/Serializer/Tests/Fixtures/Attributes/AbstractDummySecondChild.php similarity index 83% rename from src/Symfony/Component/Serializer/Tests/Fixtures/AbstractDummySecondChild.php rename to src/Symfony/Component/Serializer/Tests/Fixtures/Attributes/AbstractDummySecondChild.php index 67a72977e2..e245033cc2 100644 --- a/src/Symfony/Component/Serializer/Tests/Fixtures/AbstractDummySecondChild.php +++ b/src/Symfony/Component/Serializer/Tests/Fixtures/Attributes/AbstractDummySecondChild.php @@ -9,7 +9,9 @@ * file that was distributed with this source code. */ -namespace Symfony\Component\Serializer\Tests\Fixtures; +namespace Symfony\Component\Serializer\Tests\Fixtures\Attributes; + +use Symfony\Component\Serializer\Tests\Fixtures\DummySecondChildQuux; class AbstractDummySecondChild extends AbstractDummy { diff --git a/src/Symfony/Component/Serializer/Tests/Fixtures/AbstractDummyThirdChild.php b/src/Symfony/Component/Serializer/Tests/Fixtures/Attributes/AbstractDummyThirdChild.php similarity index 82% rename from src/Symfony/Component/Serializer/Tests/Fixtures/AbstractDummyThirdChild.php rename to src/Symfony/Component/Serializer/Tests/Fixtures/Attributes/AbstractDummyThirdChild.php index 3701e8c3f3..d5ef40c644 100644 --- a/src/Symfony/Component/Serializer/Tests/Fixtures/AbstractDummyThirdChild.php +++ b/src/Symfony/Component/Serializer/Tests/Fixtures/Attributes/AbstractDummyThirdChild.php @@ -9,7 +9,7 @@ * file that was distributed with this source code. */ -namespace Symfony\Component\Serializer\Tests\Fixtures; +namespace Symfony\Component\Serializer\Tests\Fixtures\Attributes; final class AbstractDummyThirdChild extends AbstractDummyFirstChild { diff --git a/src/Symfony/Component/Serializer/Tests/Fixtures/Attributes/GroupDummy.php b/src/Symfony/Component/Serializer/Tests/Fixtures/Attributes/GroupDummy.php new file mode 100644 index 0000000000..eef1c55be9 --- /dev/null +++ b/src/Symfony/Component/Serializer/Tests/Fixtures/Attributes/GroupDummy.php @@ -0,0 +1,71 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Serializer\Tests\Fixtures\Attributes; + +use Symfony\Component\Serializer\Annotation\Groups; +use Symfony\Component\Serializer\Tests\Fixtures\GroupDummyInterface; + +/** + * @author Kévin Dunglas + */ +class GroupDummy extends GroupDummyParent implements GroupDummyInterface +{ + #[Groups(["a"])] + private $foo; + #[Groups(["b", "c", "name_converter"])] + protected $bar; + private $fooBar; + private $symfony; + + #[Groups(["b"])] + public function setBar($bar) + { + $this->bar = $bar; + } + + #[Groups(["c"])] + public function getBar() + { + return $this->bar; + } + + public function setFoo($foo) + { + $this->foo = $foo; + } + + public function getFoo() + { + return $this->foo; + } + + public function setFooBar($fooBar) + { + $this->fooBar = $fooBar; + } + + #[Groups(["a", "b", "name_converter"])] + public function isFooBar() + { + return $this->fooBar; + } + + public function setSymfony($symfony) + { + $this->symfony = $symfony; + } + + public function getSymfony() + { + return $this->symfony; + } +} diff --git a/src/Symfony/Component/Serializer/Tests/Fixtures/GroupDummyChild.php b/src/Symfony/Component/Serializer/Tests/Fixtures/Attributes/GroupDummyChild.php similarity index 88% rename from src/Symfony/Component/Serializer/Tests/Fixtures/GroupDummyChild.php rename to src/Symfony/Component/Serializer/Tests/Fixtures/Attributes/GroupDummyChild.php index fa72160ec6..5a7423914c 100644 --- a/src/Symfony/Component/Serializer/Tests/Fixtures/GroupDummyChild.php +++ b/src/Symfony/Component/Serializer/Tests/Fixtures/Attributes/GroupDummyChild.php @@ -9,7 +9,7 @@ * file that was distributed with this source code. */ -namespace Symfony\Component\Serializer\Tests\Fixtures; +namespace Symfony\Component\Serializer\Tests\Fixtures\Attributes; class GroupDummyChild extends GroupDummy { diff --git a/src/Symfony/Component/Serializer/Tests/Fixtures/Attributes/GroupDummyParent.php b/src/Symfony/Component/Serializer/Tests/Fixtures/Attributes/GroupDummyParent.php new file mode 100644 index 0000000000..51a89acc47 --- /dev/null +++ b/src/Symfony/Component/Serializer/Tests/Fixtures/Attributes/GroupDummyParent.php @@ -0,0 +1,45 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Serializer\Tests\Fixtures\Attributes; + +use Symfony\Component\Serializer\Annotation\Groups; + +/** + * @author Kévin Dunglas + */ +class GroupDummyParent +{ + #[Groups(["a"])] + private $kevin; + private $coopTilleuls; + + public function setKevin($kevin) + { + $this->kevin = $kevin; + } + + public function getKevin() + { + return $this->kevin; + } + + public function setCoopTilleuls($coopTilleuls) + { + $this->coopTilleuls = $coopTilleuls; + } + + #[Groups(["a", "b"])] + public function getCoopTilleuls() + { + return $this->coopTilleuls; + } +} diff --git a/src/Symfony/Component/Serializer/Tests/Fixtures/Attributes/IgnoreDummy.php b/src/Symfony/Component/Serializer/Tests/Fixtures/Attributes/IgnoreDummy.php new file mode 100644 index 0000000000..85d7a9ca41 --- /dev/null +++ b/src/Symfony/Component/Serializer/Tests/Fixtures/Attributes/IgnoreDummy.php @@ -0,0 +1,31 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Serializer\Tests\Fixtures\Attributes; + +use Symfony\Component\Serializer\Annotation\Ignore; + +/** + * @author Kévin Dunglas + */ +class IgnoreDummy +{ + public $notIgnored; + #[Ignore] + public $ignored1; + private $ignored2; + + #[Ignore] + public function getIgnored2() + { + return $this->ignored2; + } +} diff --git a/src/Symfony/Component/Serializer/Tests/Fixtures/Attributes/MaxDepthDummy.php b/src/Symfony/Component/Serializer/Tests/Fixtures/Attributes/MaxDepthDummy.php new file mode 100644 index 0000000000..7a1dc42c2f --- /dev/null +++ b/src/Symfony/Component/Serializer/Tests/Fixtures/Attributes/MaxDepthDummy.php @@ -0,0 +1,46 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Serializer\Tests\Fixtures\Attributes; + +use Symfony\Component\Serializer\Annotation\MaxDepth; + +/** + * @author Kévin Dunglas + */ +class MaxDepthDummy +{ + #[MaxDepth(2)] + public $foo; + + public $bar; + + /** + * @var self + */ + public $child; + + #[MaxDepth(3)] + public function getBar() + { + return $this->bar; + } + + public function getChild() + { + return $this->child; + } + + public function getFoo() + { + return $this->foo; + } +} diff --git a/src/Symfony/Component/Serializer/Tests/Fixtures/Attributes/SerializedNameDummy.php b/src/Symfony/Component/Serializer/Tests/Fixtures/Attributes/SerializedNameDummy.php new file mode 100644 index 0000000000..39ded47731 --- /dev/null +++ b/src/Symfony/Component/Serializer/Tests/Fixtures/Attributes/SerializedNameDummy.php @@ -0,0 +1,43 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Serializer\Tests\Fixtures\Attributes; + +use Symfony\Component\Serializer\Annotation\SerializedName; + +/** + * @author Fabien Bourigault + */ +class SerializedNameDummy +{ + #[SerializedName("baz")] + public $foo; + + public $bar; + + public $quux; + + /** + * @var self + */ + public $child; + + #[SerializedName("qux")] + public function getBar() + { + return $this->bar; + } + + public function getChild() + { + return $this->child; + } +} diff --git a/src/Symfony/Component/Serializer/Tests/Fixtures/invalid-ignore.yml b/src/Symfony/Component/Serializer/Tests/Fixtures/invalid-ignore.yml index d245021d8c..00aa0fa962 100644 --- a/src/Symfony/Component/Serializer/Tests/Fixtures/invalid-ignore.yml +++ b/src/Symfony/Component/Serializer/Tests/Fixtures/invalid-ignore.yml @@ -1,4 +1,4 @@ -'Symfony\Component\Serializer\Tests\Fixtures\IgnoreDummy': +'Symfony\Component\Serializer\Tests\Fixtures\Annotations\IgnoreDummy': attributes: ignored1: ignore: foo diff --git a/src/Symfony/Component/Serializer/Tests/Fixtures/serialization.xml b/src/Symfony/Component/Serializer/Tests/Fixtures/serialization.xml index 257d838b49..635253dd8e 100644 --- a/src/Symfony/Component/Serializer/Tests/Fixtures/serialization.xml +++ b/src/Symfony/Component/Serializer/Tests/Fixtures/serialization.xml @@ -4,7 +4,7 @@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/serializer-mapping https://symfony.com/schema/dic/serializer-mapping/serializer-mapping-1.0.xsd"> - + group1 group2 @@ -15,26 +15,26 @@ - + - + - + - - + + - + diff --git a/src/Symfony/Component/Serializer/Tests/Fixtures/serialization.yml b/src/Symfony/Component/Serializer/Tests/Fixtures/serialization.yml index 4d98c73b04..5b212c8914 100644 --- a/src/Symfony/Component/Serializer/Tests/Fixtures/serialization.yml +++ b/src/Symfony/Component/Serializer/Tests/Fixtures/serialization.yml @@ -1,30 +1,30 @@ -'Symfony\Component\Serializer\Tests\Fixtures\GroupDummy': +'Symfony\Component\Serializer\Tests\Fixtures\Annotations\GroupDummy': attributes: foo: groups: ['group1', 'group2'] bar: groups: ['group2'] -'Symfony\Component\Serializer\Tests\Fixtures\MaxDepthDummy': +'Symfony\Component\Serializer\Tests\Fixtures\Annotations\MaxDepthDummy': attributes: foo: max_depth: 2 bar: max_depth: 3 -'Symfony\Component\Serializer\Tests\Fixtures\SerializedNameDummy': +'Symfony\Component\Serializer\Tests\Fixtures\Annotations\SerializedNameDummy': attributes: foo: serialized_name: 'baz' bar: serialized_name: 'qux' -'Symfony\Component\Serializer\Tests\Fixtures\AbstractDummy': +'Symfony\Component\Serializer\Tests\Fixtures\Annotations\AbstractDummy': discriminator_map: type_property: type mapping: - first: 'Symfony\Component\Serializer\Tests\Fixtures\AbstractDummyFirstChild' - second: 'Symfony\Component\Serializer\Tests\Fixtures\AbstractDummySecondChild' + first: 'Symfony\Component\Serializer\Tests\Fixtures\Annotations\AbstractDummyFirstChild' + second: 'Symfony\Component\Serializer\Tests\Fixtures\Annotations\AbstractDummySecondChild' attributes: foo: ~ -'Symfony\Component\Serializer\Tests\Fixtures\IgnoreDummy': +'Symfony\Component\Serializer\Tests\Fixtures\Annotations\IgnoreDummy': attributes: ignored1: ignore: true diff --git a/src/Symfony/Component/Serializer/Tests/Mapping/ClassDiscriminatorMappingTest.php b/src/Symfony/Component/Serializer/Tests/Mapping/ClassDiscriminatorMappingTest.php index aed2d221d4..9c94a0463d 100644 --- a/src/Symfony/Component/Serializer/Tests/Mapping/ClassDiscriminatorMappingTest.php +++ b/src/Symfony/Component/Serializer/Tests/Mapping/ClassDiscriminatorMappingTest.php @@ -13,9 +13,9 @@ namespace Symfony\Component\Serializer\Tests\Mapping; use PHPUnit\Framework\TestCase; use Symfony\Component\Serializer\Mapping\ClassDiscriminatorMapping; -use Symfony\Component\Serializer\Tests\Fixtures\AbstractDummyFirstChild; -use Symfony\Component\Serializer\Tests\Fixtures\AbstractDummySecondChild; -use Symfony\Component\Serializer\Tests\Fixtures\AbstractDummyThirdChild; +use Symfony\Component\Serializer\Tests\Fixtures\Annotations\AbstractDummyFirstChild; +use Symfony\Component\Serializer\Tests\Fixtures\Annotations\AbstractDummySecondChild; +use Symfony\Component\Serializer\Tests\Fixtures\Annotations\AbstractDummyThirdChild; /** * @author Samuel Roze diff --git a/src/Symfony/Component/Serializer/Tests/Mapping/Factory/ClassMetadataFactoryCompilerTest.php b/src/Symfony/Component/Serializer/Tests/Mapping/Factory/ClassMetadataFactoryCompilerTest.php index 87feb8f99c..2ce24718df 100644 --- a/src/Symfony/Component/Serializer/Tests/Mapping/Factory/ClassMetadataFactoryCompilerTest.php +++ b/src/Symfony/Component/Serializer/Tests/Mapping/Factory/ClassMetadataFactoryCompilerTest.php @@ -8,8 +8,8 @@ use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory; use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryCompiler; use Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader; use Symfony\Component\Serializer\Tests\Fixtures\Dummy; -use Symfony\Component\Serializer\Tests\Fixtures\MaxDepthDummy; -use Symfony\Component\Serializer\Tests\Fixtures\SerializedNameDummy; +use Symfony\Component\Serializer\Tests\Fixtures\Annotations\MaxDepthDummy; +use Symfony\Component\Serializer\Tests\Fixtures\Annotations\SerializedNameDummy; final class ClassMetadataFactoryCompilerTest extends TestCase { diff --git a/src/Symfony/Component/Serializer/Tests/Mapping/Factory/ClassMetadataFactoryTest.php b/src/Symfony/Component/Serializer/Tests/Mapping/Factory/ClassMetadataFactoryTest.php index db0e887fb5..3034eb4c4a 100644 --- a/src/Symfony/Component/Serializer/Tests/Mapping/Factory/ClassMetadataFactoryTest.php +++ b/src/Symfony/Component/Serializer/Tests/Mapping/Factory/ClassMetadataFactoryTest.php @@ -32,16 +32,16 @@ class ClassMetadataFactoryTest extends TestCase public function testGetMetadataFor() { $factory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader())); - $classMetadata = $factory->getMetadataFor('Symfony\Component\Serializer\Tests\Fixtures\GroupDummy'); + $classMetadata = $factory->getMetadataFor('Symfony\Component\Serializer\Tests\Fixtures\Annotations\GroupDummy'); - $this->assertEquals(TestClassMetadataFactory::createClassMetadata(true, true), $classMetadata); + $this->assertEquals(TestClassMetadataFactory::createClassMetadata('Symfony\Component\Serializer\Tests\Fixtures\Annotations', true, true), $classMetadata); } public function testHasMetadataFor() { $factory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader())); - $this->assertTrue($factory->hasMetadataFor('Symfony\Component\Serializer\Tests\Fixtures\GroupDummy')); - $this->assertTrue($factory->hasMetadataFor('Symfony\Component\Serializer\Tests\Fixtures\GroupDummyParent')); + $this->assertTrue($factory->hasMetadataFor('Symfony\Component\Serializer\Tests\Fixtures\Annotations\GroupDummy')); + $this->assertTrue($factory->hasMetadataFor('Symfony\Component\Serializer\Tests\Fixtures\Annotations\GroupDummyParent')); $this->assertTrue($factory->hasMetadataFor('Symfony\Component\Serializer\Tests\Fixtures\GroupDummyInterface')); $this->assertFalse($factory->hasMetadataFor('Dunglas\Entity')); } diff --git a/src/Symfony/Component/Serializer/Tests/Mapping/Factory/CompiledClassMetadataFactoryTest.php b/src/Symfony/Component/Serializer/Tests/Mapping/Factory/CompiledClassMetadataFactoryTest.php index 21beddd3f6..034e8aa088 100644 --- a/src/Symfony/Component/Serializer/Tests/Mapping/Factory/CompiledClassMetadataFactoryTest.php +++ b/src/Symfony/Component/Serializer/Tests/Mapping/Factory/CompiledClassMetadataFactoryTest.php @@ -8,7 +8,7 @@ use Symfony\Component\Serializer\Mapping\ClassMetadata; use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface; use Symfony\Component\Serializer\Mapping\Factory\CompiledClassMetadataFactory; use Symfony\Component\Serializer\Tests\Fixtures\Dummy; -use Symfony\Component\Serializer\Tests\Fixtures\SerializedNameDummy; +use Symfony\Component\Serializer\Tests\Fixtures\Annotations\SerializedNameDummy; /** * @author Fabien Bourigault diff --git a/src/Symfony/Component/Serializer/Tests/Mapping/Loader/AnnotationLoaderTest.php b/src/Symfony/Component/Serializer/Tests/Mapping/Loader/AnnotationLoaderTest.php index 9c5a9943e3..690f1c4b81 100644 --- a/src/Symfony/Component/Serializer/Tests/Mapping/Loader/AnnotationLoaderTest.php +++ b/src/Symfony/Component/Serializer/Tests/Mapping/Loader/AnnotationLoaderTest.php @@ -11,23 +11,17 @@ namespace Symfony\Component\Serializer\Tests\Mapping\Loader; -use Doctrine\Common\Annotations\AnnotationReader; use PHPUnit\Framework\TestCase; use Symfony\Component\Serializer\Mapping\AttributeMetadata; use Symfony\Component\Serializer\Mapping\ClassDiscriminatorMapping; use Symfony\Component\Serializer\Mapping\ClassMetadata; use Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader; -use Symfony\Component\Serializer\Tests\Fixtures\AbstractDummy; -use Symfony\Component\Serializer\Tests\Fixtures\AbstractDummyFirstChild; -use Symfony\Component\Serializer\Tests\Fixtures\AbstractDummySecondChild; -use Symfony\Component\Serializer\Tests\Fixtures\IgnoreDummy; -use Symfony\Component\Serializer\Tests\Fixtures\AbstractDummyThirdChild; use Symfony\Component\Serializer\Tests\Mapping\TestClassMetadataFactory; /** * @author Kévin Dunglas */ -class AnnotationLoaderTest extends TestCase +abstract class AnnotationLoaderTest extends TestCase { /** * @var AnnotationLoader @@ -36,7 +30,7 @@ class AnnotationLoaderTest extends TestCase protected function setUp(): void { - $this->loader = new AnnotationLoader(new AnnotationReader()); + $this->loader = $this->createLoader(); } public function testInterface() @@ -46,28 +40,28 @@ class AnnotationLoaderTest extends TestCase public function testLoadClassMetadataReturnsTrueIfSuccessful() { - $classMetadata = new ClassMetadata('Symfony\Component\Serializer\Tests\Fixtures\GroupDummy'); + $classMetadata = new ClassMetadata($this->getNamespace().'\GroupDummy'); $this->assertTrue($this->loader->loadClassMetadata($classMetadata)); } public function testLoadGroups() { - $classMetadata = new ClassMetadata('Symfony\Component\Serializer\Tests\Fixtures\GroupDummy'); + $classMetadata = new ClassMetadata($this->getNamespace().'\GroupDummy'); $this->loader->loadClassMetadata($classMetadata); - $this->assertEquals(TestClassMetadataFactory::createClassMetadata(), $classMetadata); + $this->assertEquals(TestClassMetadataFactory::createClassMetadata($this->getNamespace()), $classMetadata); } public function testLoadDiscriminatorMap() { - $classMetadata = new ClassMetadata(AbstractDummy::class); + $classMetadata = new ClassMetadata($this->getNamespace().'\AbstractDummy'); $this->loader->loadClassMetadata($classMetadata); - $expected = new ClassMetadata(AbstractDummy::class, new ClassDiscriminatorMapping('type', [ - 'first' => AbstractDummyFirstChild::class, - 'second' => AbstractDummySecondChild::class, - 'third' => AbstractDummyThirdChild::class, + $expected = new ClassMetadata($this->getNamespace().'\AbstractDummy', new ClassDiscriminatorMapping('type', [ + 'first' => $this->getNamespace().'\AbstractDummyFirstChild', + 'second' => $this->getNamespace().'\AbstractDummySecondChild', + 'third' => $this->getNamespace().'\AbstractDummyThirdChild', ])); $expected->addAttributeMetadata(new AttributeMetadata('foo')); @@ -78,7 +72,7 @@ class AnnotationLoaderTest extends TestCase public function testLoadMaxDepth() { - $classMetadata = new ClassMetadata('Symfony\Component\Serializer\Tests\Fixtures\MaxDepthDummy'); + $classMetadata = new ClassMetadata($this->getNamespace().'\MaxDepthDummy'); $this->loader->loadClassMetadata($classMetadata); $attributesMetadata = $classMetadata->getAttributesMetadata(); @@ -88,7 +82,7 @@ class AnnotationLoaderTest extends TestCase public function testLoadSerializedName() { - $classMetadata = new ClassMetadata('Symfony\Component\Serializer\Tests\Fixtures\SerializedNameDummy'); + $classMetadata = new ClassMetadata($this->getNamespace().'\SerializedNameDummy'); $this->loader->loadClassMetadata($classMetadata); $attributesMetadata = $classMetadata->getAttributesMetadata(); @@ -98,24 +92,27 @@ class AnnotationLoaderTest extends TestCase public function testLoadClassMetadataAndMerge() { - $classMetadata = new ClassMetadata('Symfony\Component\Serializer\Tests\Fixtures\GroupDummy'); - $parentClassMetadata = new ClassMetadata('Symfony\Component\Serializer\Tests\Fixtures\GroupDummyParent'); + $classMetadata = new ClassMetadata($this->getNamespace().'\GroupDummy'); + $parentClassMetadata = new ClassMetadata($this->getNamespace().'\GroupDummyParent'); $this->loader->loadClassMetadata($parentClassMetadata); $classMetadata->merge($parentClassMetadata); $this->loader->loadClassMetadata($classMetadata); - $this->assertEquals(TestClassMetadataFactory::createClassMetadata(true), $classMetadata); + $this->assertEquals(TestClassMetadataFactory::createClassMetadata($this->getNamespace(), true), $classMetadata); } public function testLoadIgnore() { - $classMetadata = new ClassMetadata(IgnoreDummy::class); + $classMetadata = new ClassMetadata($this->getNamespace().'\IgnoreDummy'); $this->loader->loadClassMetadata($classMetadata); $attributesMetadata = $classMetadata->getAttributesMetadata(); $this->assertTrue($attributesMetadata['ignored1']->isIgnored()); $this->assertTrue($attributesMetadata['ignored2']->isIgnored()); } + + abstract protected function createLoader(): AnnotationLoader; + abstract protected function getNamespace(): string; } diff --git a/src/Symfony/Component/Serializer/Tests/Mapping/Loader/AnnotationLoaderWithAttributesTest.php b/src/Symfony/Component/Serializer/Tests/Mapping/Loader/AnnotationLoaderWithAttributesTest.php new file mode 100644 index 0000000000..c5836f26f9 --- /dev/null +++ b/src/Symfony/Component/Serializer/Tests/Mapping/Loader/AnnotationLoaderWithAttributesTest.php @@ -0,0 +1,30 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Serializer\Tests\Mapping\Loader; + +use Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader; + +/** + * @requires PHP 8 + */ +class AnnotationLoaderWithAttributesTest extends AnnotationLoaderTest +{ + protected function createLoader(): AnnotationLoader + { + return new AnnotationLoader(); + } + + protected function getNamespace(): string + { + return 'Symfony\Component\Serializer\Tests\Fixtures\Attributes'; + } +} diff --git a/src/Symfony/Component/Serializer/Tests/Mapping/Loader/AnnotationLoaderWithDoctrineAnnotationsTest.php b/src/Symfony/Component/Serializer/Tests/Mapping/Loader/AnnotationLoaderWithDoctrineAnnotationsTest.php new file mode 100644 index 0000000000..37675a49d9 --- /dev/null +++ b/src/Symfony/Component/Serializer/Tests/Mapping/Loader/AnnotationLoaderWithDoctrineAnnotationsTest.php @@ -0,0 +1,28 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Serializer\Tests\Mapping\Loader; + +use Doctrine\Common\Annotations\AnnotationReader; +use Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader; + +class AnnotationLoaderWithDoctrineAnnotationsTest extends AnnotationLoaderTest +{ + protected function createLoader(): AnnotationLoader + { + return new AnnotationLoader(new AnnotationReader()); + } + + protected function getNamespace(): string + { + return 'Symfony\Component\Serializer\Tests\Fixtures\Annotations'; + } +} diff --git a/src/Symfony/Component/Serializer/Tests/Mapping/Loader/XmlFileLoaderTest.php b/src/Symfony/Component/Serializer/Tests/Mapping/Loader/XmlFileLoaderTest.php index 4d30c8e2cb..8a5e0e13f7 100644 --- a/src/Symfony/Component/Serializer/Tests/Mapping/Loader/XmlFileLoaderTest.php +++ b/src/Symfony/Component/Serializer/Tests/Mapping/Loader/XmlFileLoaderTest.php @@ -16,10 +16,10 @@ use Symfony\Component\Serializer\Mapping\AttributeMetadata; use Symfony\Component\Serializer\Mapping\ClassDiscriminatorMapping; use Symfony\Component\Serializer\Mapping\ClassMetadata; use Symfony\Component\Serializer\Mapping\Loader\XmlFileLoader; -use Symfony\Component\Serializer\Tests\Fixtures\AbstractDummy; -use Symfony\Component\Serializer\Tests\Fixtures\AbstractDummyFirstChild; -use Symfony\Component\Serializer\Tests\Fixtures\AbstractDummySecondChild; -use Symfony\Component\Serializer\Tests\Fixtures\IgnoreDummy; +use Symfony\Component\Serializer\Tests\Fixtures\Annotations\AbstractDummy; +use Symfony\Component\Serializer\Tests\Fixtures\Annotations\AbstractDummyFirstChild; +use Symfony\Component\Serializer\Tests\Fixtures\Annotations\AbstractDummySecondChild; +use Symfony\Component\Serializer\Tests\Fixtures\Annotations\IgnoreDummy; use Symfony\Component\Serializer\Tests\Mapping\TestClassMetadataFactory; /** @@ -39,7 +39,7 @@ class XmlFileLoaderTest extends TestCase protected function setUp(): void { $this->loader = new XmlFileLoader(__DIR__.'/../../Fixtures/serialization.xml'); - $this->metadata = new ClassMetadata('Symfony\Component\Serializer\Tests\Fixtures\GroupDummy'); + $this->metadata = new ClassMetadata('Symfony\Component\Serializer\Tests\Fixtures\Annotations\GroupDummy'); } public function testInterface() @@ -61,7 +61,7 @@ class XmlFileLoaderTest extends TestCase public function testMaxDepth() { - $classMetadata = new ClassMetadata('Symfony\Component\Serializer\Tests\Fixtures\MaxDepthDummy'); + $classMetadata = new ClassMetadata('Symfony\Component\Serializer\Tests\Fixtures\Annotations\MaxDepthDummy'); $this->loader->loadClassMetadata($classMetadata); $attributesMetadata = $classMetadata->getAttributesMetadata(); @@ -71,7 +71,7 @@ class XmlFileLoaderTest extends TestCase public function testSerializedName() { - $classMetadata = new ClassMetadata('Symfony\Component\Serializer\Tests\Fixtures\SerializedNameDummy'); + $classMetadata = new ClassMetadata('Symfony\Component\Serializer\Tests\Fixtures\Annotations\SerializedNameDummy'); $this->loader->loadClassMetadata($classMetadata); $attributesMetadata = $classMetadata->getAttributesMetadata(); diff --git a/src/Symfony/Component/Serializer/Tests/Mapping/Loader/YamlFileLoaderTest.php b/src/Symfony/Component/Serializer/Tests/Mapping/Loader/YamlFileLoaderTest.php index 6d2ff5c0bd..2e708665f5 100644 --- a/src/Symfony/Component/Serializer/Tests/Mapping/Loader/YamlFileLoaderTest.php +++ b/src/Symfony/Component/Serializer/Tests/Mapping/Loader/YamlFileLoaderTest.php @@ -17,10 +17,10 @@ use Symfony\Component\Serializer\Mapping\AttributeMetadata; use Symfony\Component\Serializer\Mapping\ClassDiscriminatorMapping; use Symfony\Component\Serializer\Mapping\ClassMetadata; use Symfony\Component\Serializer\Mapping\Loader\YamlFileLoader; -use Symfony\Component\Serializer\Tests\Fixtures\AbstractDummy; -use Symfony\Component\Serializer\Tests\Fixtures\AbstractDummyFirstChild; -use Symfony\Component\Serializer\Tests\Fixtures\AbstractDummySecondChild; -use Symfony\Component\Serializer\Tests\Fixtures\IgnoreDummy; +use Symfony\Component\Serializer\Tests\Fixtures\Annotations\AbstractDummy; +use Symfony\Component\Serializer\Tests\Fixtures\Annotations\AbstractDummyFirstChild; +use Symfony\Component\Serializer\Tests\Fixtures\Annotations\AbstractDummySecondChild; +use Symfony\Component\Serializer\Tests\Fixtures\Annotations\IgnoreDummy; use Symfony\Component\Serializer\Tests\Mapping\TestClassMetadataFactory; /** @@ -40,7 +40,7 @@ class YamlFileLoaderTest extends TestCase protected function setUp(): void { $this->loader = new YamlFileLoader(__DIR__.'/../../Fixtures/serialization.yml'); - $this->metadata = new ClassMetadata('Symfony\Component\Serializer\Tests\Fixtures\GroupDummy'); + $this->metadata = new ClassMetadata('Symfony\Component\Serializer\Tests\Fixtures\Annotations\GroupDummy'); } public function testInterface() @@ -75,7 +75,7 @@ class YamlFileLoaderTest extends TestCase public function testMaxDepth() { - $classMetadata = new ClassMetadata('Symfony\Component\Serializer\Tests\Fixtures\MaxDepthDummy'); + $classMetadata = new ClassMetadata('Symfony\Component\Serializer\Tests\Fixtures\Annotations\MaxDepthDummy'); $this->loader->loadClassMetadata($classMetadata); $attributesMetadata = $classMetadata->getAttributesMetadata(); @@ -85,7 +85,7 @@ class YamlFileLoaderTest extends TestCase public function testSerializedName() { - $classMetadata = new ClassMetadata('Symfony\Component\Serializer\Tests\Fixtures\SerializedNameDummy'); + $classMetadata = new ClassMetadata('Symfony\Component\Serializer\Tests\Fixtures\Annotations\SerializedNameDummy'); $this->loader->loadClassMetadata($classMetadata); $attributesMetadata = $classMetadata->getAttributesMetadata(); diff --git a/src/Symfony/Component/Serializer/Tests/Mapping/TestClassMetadataFactory.php b/src/Symfony/Component/Serializer/Tests/Mapping/TestClassMetadataFactory.php index 71fa42be8a..4fcb8e5436 100644 --- a/src/Symfony/Component/Serializer/Tests/Mapping/TestClassMetadataFactory.php +++ b/src/Symfony/Component/Serializer/Tests/Mapping/TestClassMetadataFactory.php @@ -19,9 +19,9 @@ use Symfony\Component\Serializer\Mapping\ClassMetadata; */ class TestClassMetadataFactory { - public static function createClassMetadata(bool $withParent = false, bool $withInterface = false) + public static function createClassMetadata(string $namespace, bool $withParent = false, bool $withInterface = false): ClassMetadata { - $expected = new ClassMetadata('Symfony\Component\Serializer\Tests\Fixtures\GroupDummy'); + $expected = new ClassMetadata($namespace.'\GroupDummy'); $foo = new AttributeMetadata('foo'); $foo->addGroup('a'); @@ -64,9 +64,9 @@ class TestClassMetadataFactory return $expected; } - public static function createXmlCLassMetadata() + public static function createXmlCLassMetadata(): ClassMetadata { - $expected = new ClassMetadata('Symfony\Component\Serializer\Tests\Fixtures\GroupDummy'); + $expected = new ClassMetadata('Symfony\Component\Serializer\Tests\Fixtures\Annotations\GroupDummy'); $foo = new AttributeMetadata('foo'); $foo->addGroup('group1'); diff --git a/src/Symfony/Component/Serializer/Tests/NameConverter/MetadataAwareNameConverterTest.php b/src/Symfony/Component/Serializer/Tests/NameConverter/MetadataAwareNameConverterTest.php index 9b0ad4d79c..ba20bc7b0d 100644 --- a/src/Symfony/Component/Serializer/Tests/NameConverter/MetadataAwareNameConverterTest.php +++ b/src/Symfony/Component/Serializer/Tests/NameConverter/MetadataAwareNameConverterTest.php @@ -19,7 +19,7 @@ use Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader; use Symfony\Component\Serializer\NameConverter\MetadataAwareNameConverter; use Symfony\Component\Serializer\NameConverter\NameConverterInterface; use Symfony\Component\Serializer\Tests\Fixtures\OtherSerializedNameDummy; -use Symfony\Component\Serializer\Tests\Fixtures\SerializedNameDummy; +use Symfony\Component\Serializer\Tests\Fixtures\Annotations\SerializedNameDummy; /** * @author Fabien Bourigault diff --git a/src/Symfony/Component/Serializer/Tests/Normalizer/AbstractNormalizerTest.php b/src/Symfony/Component/Serializer/Tests/Normalizer/AbstractNormalizerTest.php index 082b427ab7..3aa4d1c63d 100644 --- a/src/Symfony/Component/Serializer/Tests/Normalizer/AbstractNormalizerTest.php +++ b/src/Symfony/Component/Serializer/Tests/Normalizer/AbstractNormalizerTest.php @@ -15,7 +15,7 @@ use Symfony\Component\Serializer\Normalizer\PropertyNormalizer; use Symfony\Component\Serializer\Serializer; use Symfony\Component\Serializer\Tests\Fixtures\AbstractNormalizerDummy; use Symfony\Component\Serializer\Tests\Fixtures\Dummy; -use Symfony\Component\Serializer\Tests\Fixtures\IgnoreDummy; +use Symfony\Component\Serializer\Tests\Fixtures\Annotations\IgnoreDummy; use Symfony\Component\Serializer\Tests\Fixtures\NullableConstructorArgumentDummy; use Symfony\Component\Serializer\Tests\Fixtures\StaticConstructorDummy; use Symfony\Component\Serializer\Tests\Fixtures\StaticConstructorNormalizer; diff --git a/src/Symfony/Component/Serializer/Tests/Normalizer/AbstractObjectNormalizerTest.php b/src/Symfony/Component/Serializer/Tests/Normalizer/AbstractObjectNormalizerTest.php index 71d999a466..55fced0ab3 100644 --- a/src/Symfony/Component/Serializer/Tests/Normalizer/AbstractObjectNormalizerTest.php +++ b/src/Symfony/Component/Serializer/Tests/Normalizer/AbstractObjectNormalizerTest.php @@ -31,9 +31,9 @@ use Symfony\Component\Serializer\Normalizer\ObjectNormalizer; use Symfony\Component\Serializer\Serializer; use Symfony\Component\Serializer\SerializerAwareInterface; use Symfony\Component\Serializer\SerializerInterface; -use Symfony\Component\Serializer\Tests\Fixtures\AbstractDummy; -use Symfony\Component\Serializer\Tests\Fixtures\AbstractDummyFirstChild; -use Symfony\Component\Serializer\Tests\Fixtures\AbstractDummySecondChild; +use Symfony\Component\Serializer\Tests\Fixtures\Annotations\AbstractDummy; +use Symfony\Component\Serializer\Tests\Fixtures\Annotations\AbstractDummyFirstChild; +use Symfony\Component\Serializer\Tests\Fixtures\Annotations\AbstractDummySecondChild; use Symfony\Component\Serializer\Tests\Fixtures\DummySecondChildQuux; class AbstractObjectNormalizerTest extends TestCase diff --git a/src/Symfony/Component/Serializer/Tests/Normalizer/Features/GroupsTestTrait.php b/src/Symfony/Component/Serializer/Tests/Normalizer/Features/GroupsTestTrait.php index 1d9777d7a0..619cb4e885 100644 --- a/src/Symfony/Component/Serializer/Tests/Normalizer/Features/GroupsTestTrait.php +++ b/src/Symfony/Component/Serializer/Tests/Normalizer/Features/GroupsTestTrait.php @@ -4,7 +4,7 @@ namespace Symfony\Component\Serializer\Tests\Normalizer\Features; use Symfony\Component\Serializer\Normalizer\DenormalizerInterface; use Symfony\Component\Serializer\Normalizer\NormalizerInterface; -use Symfony\Component\Serializer\Tests\Fixtures\GroupDummy; +use Symfony\Component\Serializer\Tests\Fixtures\Annotations\GroupDummy; /** * Test AbstractNormalizer::GROUPS. diff --git a/src/Symfony/Component/Serializer/Tests/Normalizer/Features/MaxDepthTestTrait.php b/src/Symfony/Component/Serializer/Tests/Normalizer/Features/MaxDepthTestTrait.php index 229a1f822a..0b7f115ace 100644 --- a/src/Symfony/Component/Serializer/Tests/Normalizer/Features/MaxDepthTestTrait.php +++ b/src/Symfony/Component/Serializer/Tests/Normalizer/Features/MaxDepthTestTrait.php @@ -3,7 +3,7 @@ namespace Symfony\Component\Serializer\Tests\Normalizer\Features; use Symfony\Component\Serializer\Normalizer\NormalizerInterface; -use Symfony\Component\Serializer\Tests\Fixtures\MaxDepthDummy; +use Symfony\Component\Serializer\Tests\Fixtures\Annotations\MaxDepthDummy; /** * Covers AbstractObjectNormalizer::ENABLE_MAX_DEPTH and AbstractObjectNormalizer::MAX_DEPTH_HANDLER. diff --git a/src/Symfony/Component/Serializer/Tests/Normalizer/GetSetMethodNormalizerTest.php b/src/Symfony/Component/Serializer/Tests/Normalizer/GetSetMethodNormalizerTest.php index d6beb7c19e..b2efafc348 100644 --- a/src/Symfony/Component/Serializer/Tests/Normalizer/GetSetMethodNormalizerTest.php +++ b/src/Symfony/Component/Serializer/Tests/Normalizer/GetSetMethodNormalizerTest.php @@ -28,7 +28,7 @@ use Symfony\Component\Serializer\Normalizer\NormalizerInterface; use Symfony\Component\Serializer\Serializer; use Symfony\Component\Serializer\SerializerInterface; use Symfony\Component\Serializer\Tests\Fixtures\CircularReferenceDummy; -use Symfony\Component\Serializer\Tests\Fixtures\GroupDummy; +use Symfony\Component\Serializer\Tests\Fixtures\Annotations\GroupDummy; use Symfony\Component\Serializer\Tests\Fixtures\SiblingHolder; use Symfony\Component\Serializer\Tests\Normalizer\Features\CallbacksTestTrait; use Symfony\Component\Serializer\Tests\Normalizer\Features\CircularReferenceTestTrait; @@ -306,7 +306,7 @@ class GetSetMethodNormalizerTest extends TestCase 'foo_bar' => '@dunglas', 'symfony' => '@coopTilleuls', 'coop_tilleuls' => 'les-tilleuls.coop', - ], 'Symfony\Component\Serializer\Tests\Fixtures\GroupDummy', null, [GetSetMethodNormalizer::GROUPS => ['name_converter']]) + ], 'Symfony\Component\Serializer\Tests\Fixtures\Annotations\GroupDummy', null, [GetSetMethodNormalizer::GROUPS => ['name_converter']]) ); } diff --git a/src/Symfony/Component/Serializer/Tests/Normalizer/ObjectNormalizerTest.php b/src/Symfony/Component/Serializer/Tests/Normalizer/ObjectNormalizerTest.php index 6b2474c145..66d578f1eb 100644 --- a/src/Symfony/Component/Serializer/Tests/Normalizer/ObjectNormalizerTest.php +++ b/src/Symfony/Component/Serializer/Tests/Normalizer/ObjectNormalizerTest.php @@ -29,7 +29,7 @@ use Symfony\Component\Serializer\Normalizer\ObjectNormalizer; use Symfony\Component\Serializer\Serializer; use Symfony\Component\Serializer\SerializerInterface; use Symfony\Component\Serializer\Tests\Fixtures\CircularReferenceDummy; -use Symfony\Component\Serializer\Tests\Fixtures\GroupDummy; +use Symfony\Component\Serializer\Tests\Fixtures\Annotations\GroupDummy; use Symfony\Component\Serializer\Tests\Fixtures\OtherSerializedNameDummy; use Symfony\Component\Serializer\Tests\Fixtures\Php74Dummy; use Symfony\Component\Serializer\Tests\Fixtures\SiblingHolder; @@ -435,7 +435,7 @@ class ObjectNormalizerTest extends TestCase 'foo_bar' => '@dunglas', 'symfony' => '@coopTilleuls', 'coop_tilleuls' => 'les-tilleuls.coop', - ], 'Symfony\Component\Serializer\Tests\Fixtures\GroupDummy', null, [ObjectNormalizer::GROUPS => ['name_converter']]) + ], 'Symfony\Component\Serializer\Tests\Fixtures\Annotations\GroupDummy', null, [ObjectNormalizer::GROUPS => ['name_converter']]) ); } diff --git a/src/Symfony/Component/Serializer/Tests/Normalizer/PropertyNormalizerTest.php b/src/Symfony/Component/Serializer/Tests/Normalizer/PropertyNormalizerTest.php index 78d75471da..67989d200e 100644 --- a/src/Symfony/Component/Serializer/Tests/Normalizer/PropertyNormalizerTest.php +++ b/src/Symfony/Component/Serializer/Tests/Normalizer/PropertyNormalizerTest.php @@ -26,8 +26,8 @@ use Symfony\Component\Serializer\Normalizer\PropertyNormalizer; use Symfony\Component\Serializer\Serializer; use Symfony\Component\Serializer\SerializerInterface; use Symfony\Component\Serializer\Tests\Fixtures\Dummy; -use Symfony\Component\Serializer\Tests\Fixtures\GroupDummy; -use Symfony\Component\Serializer\Tests\Fixtures\GroupDummyChild; +use Symfony\Component\Serializer\Tests\Fixtures\Annotations\GroupDummy; +use Symfony\Component\Serializer\Tests\Fixtures\Annotations\GroupDummyChild; use Symfony\Component\Serializer\Tests\Fixtures\Php74Dummy; use Symfony\Component\Serializer\Tests\Fixtures\PropertyCircularReferenceDummy; use Symfony\Component\Serializer\Tests\Fixtures\PropertySiblingHolder; @@ -253,7 +253,7 @@ class PropertyNormalizerTest extends TestCase 'foo_bar' => '@dunglas', 'symfony' => '@coopTilleuls', 'coop_tilleuls' => 'les-tilleuls.coop', - ], 'Symfony\Component\Serializer\Tests\Fixtures\GroupDummy', null, [PropertyNormalizer::GROUPS => ['name_converter']]) + ], 'Symfony\Component\Serializer\Tests\Fixtures\Annotations\GroupDummy', null, [PropertyNormalizer::GROUPS => ['name_converter']]) ); } diff --git a/src/Symfony/Component/Serializer/Tests/SerializerTest.php b/src/Symfony/Component/Serializer/Tests/SerializerTest.php index 80c430a7d4..36cd098af8 100644 --- a/src/Symfony/Component/Serializer/Tests/SerializerTest.php +++ b/src/Symfony/Component/Serializer/Tests/SerializerTest.php @@ -39,9 +39,9 @@ use Symfony\Component\Serializer\Normalizer\ObjectNormalizer; use Symfony\Component\Serializer\Normalizer\PropertyNormalizer; use Symfony\Component\Serializer\Normalizer\UnwrappingDenormalizer; use Symfony\Component\Serializer\Serializer; -use Symfony\Component\Serializer\Tests\Fixtures\AbstractDummy; -use Symfony\Component\Serializer\Tests\Fixtures\AbstractDummyFirstChild; -use Symfony\Component\Serializer\Tests\Fixtures\AbstractDummySecondChild; +use Symfony\Component\Serializer\Tests\Fixtures\Annotations\AbstractDummy; +use Symfony\Component\Serializer\Tests\Fixtures\Annotations\AbstractDummyFirstChild; +use Symfony\Component\Serializer\Tests\Fixtures\Annotations\AbstractDummySecondChild; use Symfony\Component\Serializer\Tests\Fixtures\DummyFirstChildQuux; use Symfony\Component\Serializer\Tests\Fixtures\DummyMessageInterface; use Symfony\Component\Serializer\Tests\Fixtures\DummyMessageNumberOne;