|
4 | 4 |
|
5 | 5 | use PHPUnit\Framework\MockObject\MockObject;
|
6 | 6 | use PHPUnit\Framework\TestCase;
|
| 7 | +use Symfony\Component\PropertyInfo\Extractor\PhpDocExtractor; |
| 8 | +use Symfony\Component\Serializer\Encoder\JsonEncoder; |
7 | 9 | use Symfony\Component\Serializer\Mapping\AttributeMetadata;
|
8 | 10 | use Symfony\Component\Serializer\Mapping\ClassMetadata;
|
9 | 11 | use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;
|
@@ -121,17 +123,48 @@ public function testObjectWithNullableConstructorArgument()
|
121 | 123 | $this->assertNull($dummy->getFoo());
|
122 | 124 | }
|
123 | 125 |
|
124 |
| - public function testObjectWithVariadicConstructorTypedArguments() |
| 126 | + /** |
| 127 | + * @dataProvider getNormalizer |
| 128 | + */ |
| 129 | + public function testObjectWithVariadicConstructorTypedArguments(AbstractNormalizer $normalizer) |
125 | 130 | {
|
126 |
| - $normalizer = new PropertyNormalizer(); |
127 |
| - $normalizer->setSerializer(new Serializer([$normalizer])); |
128 |
| - $data = ['foo' => [['foo' => 'Foo', 'bar' => 'Bar', 'baz' => 'Baz', 'qux' => 'Qux'], ['foo' => 'FOO', 'bar' => 'BAR', 'baz' => 'BAZ', 'qux' => 'QUX']]]; |
129 |
| - $dummy = $normalizer->denormalize($data, VariadicConstructorTypedArgsDummy::class); |
| 131 | + $d1 = new Dummy(); |
| 132 | + $d1->foo = 'Foo'; |
| 133 | + $d1->bar = 'Bar'; |
| 134 | + $d1->baz = 'Baz'; |
| 135 | + $d1->qux = 'Quz'; |
| 136 | + $d2 = new Dummy(); |
| 137 | + $d2->foo = 'FOO'; |
| 138 | + $d2->bar = 'BAR'; |
| 139 | + $d2->baz = 'BAZ'; |
| 140 | + $d2->qux = 'QUZ'; |
| 141 | + $obj = new VariadicConstructorTypedArgsDummy($d1, $d2); |
| 142 | + |
| 143 | + $serializer = new Serializer([$normalizer], [new JsonEncoder()]); |
| 144 | + $normalizer->setSerializer($serializer); |
| 145 | + $data = $serializer->serialize($obj, 'json'); |
| 146 | + $dummy = $normalizer->denormalize(json_decode($data, true), VariadicConstructorTypedArgsDummy::class); |
| 147 | + $this->assertInstanceOf(VariadicConstructorTypedArgsDummy::class, $dummy); |
| 148 | + $this->assertCount(2, $dummy->getFoo()); |
| 149 | + foreach ($dummy->getFoo() as $foo) { |
| 150 | + $this->assertInstanceOf(Dummy::class, $foo); |
| 151 | + } |
130 | 152 |
|
| 153 | + $dummy = $serializer->deserialize($data, VariadicConstructorTypedArgsDummy::class, 'json'); |
131 | 154 | $this->assertInstanceOf(VariadicConstructorTypedArgsDummy::class, $dummy);
|
132 | 155 | $this->assertCount(2, $dummy->getFoo());
|
133 | 156 | foreach ($dummy->getFoo() as $foo) {
|
134 | 157 | $this->assertInstanceOf(Dummy::class, $foo);
|
135 | 158 | }
|
136 | 159 | }
|
| 160 | + |
| 161 | + public function getNormalizer() |
| 162 | + { |
| 163 | + $extractor = new PhpDocExtractor(); |
| 164 | + |
| 165 | + yield [new PropertyNormalizer()]; |
| 166 | + yield [new PropertyNormalizer(null, null, $extractor)]; |
| 167 | + yield [new ObjectNormalizer()]; |
| 168 | + yield [new ObjectNormalizer(null, null, null, $extractor)]; |
| 169 | + } |
137 | 170 | }
|
0 commit comments