-
-
Notifications
You must be signed in to change notification settings - Fork 123
/
Copy pathSerializerTest.php
119 lines (97 loc) · 3.64 KB
/
SerializerTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Bundle\FrameworkBundle\Tests\Functional;
use Symfony\Bundle\FrameworkBundle\Tests\Fixtures\TranslatableBackedEnum;
use Symfony\Bundle\FrameworkBundle\Tests\Functional\app\AppKernel;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
/**
* @author Kévin Dunglas <[email protected]>
*/
class SerializerTest extends AbstractWebTestCase
{
public function testDeserializeArrayOfObject()
{
static::bootKernel(['test_case' => 'Serializer']);
$result = static::getContainer()->get('serializer.alias')->deserialize('{"bars": [{"id": 1}, {"id": 2}]}', Foo::class, 'json');
$bar1 = new Bar();
$bar1->id = 1;
$bar2 = new Bar();
$bar2->id = 2;
$expected = new Foo();
$expected->bars = [$bar1, $bar2];
$this->assertEquals($expected, $result);
}
public function testNormalizersAndEncodersUseDefaultContextConfigOption()
{
/** @var SerializerKernel $kernel */
$kernel = static::bootKernel(['test_case' => 'Serializer', 'root_config' => 'default_context.yaml']);
foreach ($kernel->normalizersAndEncoders as $normalizerOrEncoderId) {
if (!static::getContainer()->has($normalizerOrEncoderId)) {
continue;
}
$normalizerOrEncoder = static::getContainer()->get($normalizerOrEncoderId);
$reflectionObject = new \ReflectionObject($normalizerOrEncoder);
$property = $reflectionObject->getProperty('defaultContext');
$defaultContext = $property->getValue($normalizerOrEncoder);
self::assertArrayHasKey('fake_context_option', $defaultContext);
self::assertEquals('foo', $defaultContext['fake_context_option']);
}
}
protected static function getKernelClass(): string
{
return SerializerKernel::class;
}
}
class SerializerKernel extends AppKernel implements CompilerPassInterface
{
public $normalizersAndEncoders = [
'serializer.normalizer.property.alias', // Special case as this normalizer isn't tagged
];
public function process(ContainerBuilder $container): void
{
$services = array_merge(
$container->findTaggedServiceIds('serializer.normalizer'),
$container->findTaggedServiceIds('serializer.encoder')
);
foreach ($services as $serviceId => $attributes) {
$class = $container->getDefinition($serviceId)->getClass();
if (null === $reflectionConstructor = (new \ReflectionClass($class))->getConstructor()) {
continue;
}
foreach ($reflectionConstructor->getParameters() as $reflectionParam) {
if ('array $defaultContext' === $reflectionParam->getType()->getName().' $'.$reflectionParam->getName()) {
$this->normalizersAndEncoders[] = $serviceId.'.alias';
break;
}
}
}
}
public function testSerializeTranslatableBackedEnum()
{
static::bootKernel(['test_case' => 'Serializer']);
$serializer = static::getContainer()->get('serializer.alias');
$this->assertEquals('GET', $serializer->serialize(TranslatableBackedEnum::Get, 'yaml'));
}
}
class Foo
{
/**
* @var Bar[]
*/
public $bars;
}
class Bar
{
/**
* @var int
*/
public $id;
}