feature #38525 [Serializer] Enabled mapping configuration via attributes (derrabus)
This PR was merged into the 5.x branch.
Discussion
----------
[Serializer] Enabled mapping configuration via attributes
| Q | A
| ------------- | ---
| Branch? | 5.x
| Bug fix? | no
| New feature? | yes
| Deprecations? | no
| Tickets | N/A
| License | MIT
| Doc PR | TODO
Let's use attributes for the serializer mapping configuration!
```php
class MyEntity
{
#[Groups(['list', 'detail])]
#[SerializedName('some-property')]
public $someProperty;
#[Ignore]
public string $secret;
}
```
Commits
-------
cfb9986203
[Serializer] Enabled mapping configuration via attributes.
This commit is contained in:
commit
a319090109
1
.github/patch-types.php
vendored
1
.github/patch-types.php
vendored
@ -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'):
|
||||
|
@ -21,6 +21,7 @@ use Symfony\Component\Serializer\Exception\InvalidArgumentException;
|
||||
*
|
||||
* @author Samuel Roze <samuel.roze@gmail.com>
|
||||
*/
|
||||
#[\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
|
||||
|
@ -21,6 +21,7 @@ use Symfony\Component\Serializer\Exception\InvalidArgumentException;
|
||||
*
|
||||
* @author Kévin Dunglas <dunglas@gmail.com>
|
||||
*/
|
||||
#[\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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -19,6 +19,7 @@ namespace Symfony\Component\Serializer\Annotation;
|
||||
*
|
||||
* @author Kévin Dunglas <dunglas@gmail.com>
|
||||
*/
|
||||
#[\Attribute(\Attribute::TARGET_METHOD | \Attribute::TARGET_PROPERTY)]
|
||||
final class Ignore
|
||||
{
|
||||
}
|
||||
|
@ -21,6 +21,7 @@ use Symfony\Component\Serializer\Exception\InvalidArgumentException;
|
||||
*
|
||||
* @author Kévin Dunglas <dunglas@gmail.com>
|
||||
*/
|
||||
#[\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()
|
||||
|
@ -21,6 +21,7 @@ use Symfony\Component\Serializer\Exception\InvalidArgumentException;
|
||||
*
|
||||
* @author Fabien Bourigault <bourigaultfabien@gmail.com>
|
||||
*/
|
||||
#[\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
|
||||
|
@ -8,6 +8,7 @@ CHANGELOG
|
||||
* added `UidNormalizer`
|
||||
* added `FormErrorNormalizer`
|
||||
* added `MimeMessageNormalizer`
|
||||
* serializer mapping can be configured using php attributes
|
||||
|
||||
5.1.0
|
||||
-----
|
||||
|
@ -26,12 +26,21 @@ use Symfony\Component\Serializer\Mapping\ClassMetadataInterface;
|
||||
* Annotation loader.
|
||||
*
|
||||
* @author Kévin Dunglas <dunglas@gmail.com>
|
||||
* @author Alexander M. Turek <me@derrabus.de>
|
||||
*/
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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
|
@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* 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;
|
||||
}
|
||||
}
|
@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* 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;
|
||||
}
|
||||
}
|
@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* 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
|
||||
{
|
||||
}
|
@ -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 <dunglas@gmail.com>
|
@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* 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;
|
||||
}
|
||||
}
|
@ -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;
|
||||
|
@ -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;
|
||||
|
@ -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;
|
||||
|
@ -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;
|
||||
|
@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* 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;
|
||||
}
|
||||
}
|
@ -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
|
||||
{
|
@ -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
|
||||
{
|
@ -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
|
||||
{
|
@ -0,0 +1,71 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* 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 <dunglas@gmail.com>
|
||||
*/
|
||||
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;
|
||||
}
|
||||
}
|
@ -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
|
||||
{
|
@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* 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 <dunglas@gmail.com>
|
||||
*/
|
||||
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;
|
||||
}
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* 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 <dunglas@gmail.com>
|
||||
*/
|
||||
class IgnoreDummy
|
||||
{
|
||||
public $notIgnored;
|
||||
#[Ignore]
|
||||
public $ignored1;
|
||||
private $ignored2;
|
||||
|
||||
#[Ignore]
|
||||
public function getIgnored2()
|
||||
{
|
||||
return $this->ignored2;
|
||||
}
|
||||
}
|
@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* 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 <dunglas@gmail.com>
|
||||
*/
|
||||
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;
|
||||
}
|
||||
}
|
@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* 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 <bourigaultfabien@gmail.com>
|
||||
*/
|
||||
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;
|
||||
}
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
'Symfony\Component\Serializer\Tests\Fixtures\IgnoreDummy':
|
||||
'Symfony\Component\Serializer\Tests\Fixtures\Annotations\IgnoreDummy':
|
||||
attributes:
|
||||
ignored1:
|
||||
ignore: foo
|
||||
|
@ -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">
|
||||
|
||||
<class name="Symfony\Component\Serializer\Tests\Fixtures\GroupDummy">
|
||||
<class name="Symfony\Component\Serializer\Tests\Fixtures\Annotations\GroupDummy">
|
||||
<attribute name="foo">
|
||||
<group>group1</group>
|
||||
<group>group2</group>
|
||||
@ -15,26 +15,26 @@
|
||||
</attribute>
|
||||
</class>
|
||||
|
||||
<class name="Symfony\Component\Serializer\Tests\Fixtures\MaxDepthDummy">
|
||||
<class name="Symfony\Component\Serializer\Tests\Fixtures\Annotations\MaxDepthDummy">
|
||||
<attribute name="foo" max-depth="2" />
|
||||
<attribute name="bar" max-depth="3" />
|
||||
</class>
|
||||
|
||||
<class name="Symfony\Component\Serializer\Tests\Fixtures\SerializedNameDummy">
|
||||
<class name="Symfony\Component\Serializer\Tests\Fixtures\Annotations\SerializedNameDummy">
|
||||
<attribute name="foo" serialized-name="baz" />
|
||||
<attribute name="bar" serialized-name="qux" />
|
||||
</class>
|
||||
|
||||
<class name="Symfony\Component\Serializer\Tests\Fixtures\AbstractDummy">
|
||||
<class name="Symfony\Component\Serializer\Tests\Fixtures\Annotations\AbstractDummy">
|
||||
<discriminator-map type-property="type">
|
||||
<mapping type="first" class="Symfony\Component\Serializer\Tests\Fixtures\AbstractDummyFirstChild" />
|
||||
<mapping type="second" class="Symfony\Component\Serializer\Tests\Fixtures\AbstractDummySecondChild" />
|
||||
<mapping type="first" class="Symfony\Component\Serializer\Tests\Fixtures\Annotations\AbstractDummyFirstChild" />
|
||||
<mapping type="second" class="Symfony\Component\Serializer\Tests\Fixtures\Annotations\AbstractDummySecondChild" />
|
||||
</discriminator-map>
|
||||
|
||||
<attribute name="foo" />
|
||||
</class>
|
||||
|
||||
<class name="Symfony\Component\Serializer\Tests\Fixtures\IgnoreDummy">
|
||||
<class name="Symfony\Component\Serializer\Tests\Fixtures\Annotations\IgnoreDummy">
|
||||
<attribute name="ignored1" ignore="true" />
|
||||
<attribute name="ignored2" ignore="true" />
|
||||
</class>
|
||||
|
@ -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
|
||||
|
@ -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 <samuel.roze@gmail.com>
|
||||
|
@ -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
|
||||
{
|
||||
|
@ -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'));
|
||||
}
|
||||
|
@ -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 <bourigaultfabien@gmail.com>
|
||||
|
@ -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 <dunglas@gmail.com>
|
||||
*/
|
||||
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;
|
||||
}
|
||||
|
@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* 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';
|
||||
}
|
||||
}
|
@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* 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';
|
||||
}
|
||||
}
|
@ -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();
|
||||
|
@ -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();
|
||||
|
@ -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');
|
||||
|
@ -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 <bourigaultfabien@gmail.com>
|
||||
|
@ -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;
|
||||
|
@ -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
|
||||
|
@ -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.
|
||||
|
@ -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.
|
||||
|
@ -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']])
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -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']])
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -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']])
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -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;
|
||||
|
Reference in New Issue
Block a user