|
12 | 12 |
|
13 | 13 | namespace Superbrave\GdprBundle\Tests\Serializer\Normalizer; |
14 | 14 |
|
15 | | -use Doctrine\Common\Collections\ArrayCollection; |
16 | 15 | use PHPUnit_Framework_MockObject_MockObject; |
17 | 16 | use ReflectionClass; |
18 | 17 | use Superbrave\GdprBundle\Annotation\AnnotationReader; |
19 | 18 | use Superbrave\GdprBundle\Annotation\Export; |
20 | 19 | use Superbrave\GdprBundle\Manipulator\PropertyManipulator; |
21 | 20 | use Superbrave\GdprBundle\Serializer\Normalizer\AnnotationNormalizer; |
| 21 | +use Superbrave\GdprBundle\Serializer\Normalizer\IterableNormalizer; |
22 | 22 | use Superbrave\GdprBundle\Tests\AnnotatedMock; |
23 | 23 | use Symfony\Component\PropertyAccess\PropertyAccess; |
| 24 | +use Symfony\Component\Serializer\Encoder\JsonEncoder; |
24 | 25 | use Symfony\Component\Serializer\Encoder\XmlEncoder; |
| 26 | +use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer; |
25 | 27 | use Symfony\Component\Serializer\Serializer; |
26 | 28 |
|
27 | 29 | /** |
28 | 30 | * AnnotationNormalizerTest. |
29 | 31 | * |
30 | 32 | * @author Niels Nijens <nn@superbrave.nl> |
| 33 | + * @author Jelle van Oosterbosch <jvo@superbrave.nl> |
31 | 34 | */ |
32 | 35 | class AnnotationNormalizerTest extends \PHPUnit_Framework_TestCase |
33 | 36 | { |
@@ -153,47 +156,91 @@ public function testNormalize() |
153 | 156 | ); |
154 | 157 |
|
155 | 158 | $normalizer = new AnnotationNormalizer($annotationReader, Export::class, $propertyManipulator); |
| 159 | + $serializer = new Serializer([ |
| 160 | + new DateTimeNormalizer(), |
| 161 | + new IterableNormalizer(), |
| 162 | + $normalizer, |
| 163 | + ]); |
| 164 | + $normalizer->setNormalizer($serializer); |
156 | 165 |
|
157 | 166 | $annotatedMock = new AnnotatedMock(); |
158 | 167 |
|
159 | 168 | $this->assertEquals( |
160 | 169 | array( |
161 | 170 | 'foo' => 'bar', |
162 | 171 | 'baz' => 1, |
163 | | - 'qux' => array(), |
164 | | - 'quuxs' => new ArrayCollection(), |
| 172 | + 'qux' => [], |
| 173 | + 'quuxs' => [], |
| 174 | + 'quuz' => '2016-01-01T00:00:00+00:00', |
165 | 175 | 'annotatedPropertyWithoutMethod' => 'Yes', |
166 | 176 | ), |
167 | 177 | $normalizer->normalize($annotatedMock) |
168 | 178 | ); |
169 | 179 | } |
170 | 180 |
|
171 | 181 | /** |
172 | | - * Tests if AnnotationNormalizer::normalize returns the expected normalized data |
| 182 | + * Tests if @see AnnotationNormalizer::normalize returns the expected xml normalized data |
173 | 183 | * for serialization through the Serializer. |
174 | 184 | * |
175 | 185 | * @return void |
176 | 186 | */ |
177 | | - public function testNormalizeThroughSerializer() |
| 187 | + public function testNormalizeThroughXmlSerializer() |
178 | 188 | { |
179 | 189 | $annotationReader = new AnnotationReader(); |
180 | 190 | $propertyManipulator = new PropertyManipulator( |
181 | 191 | PropertyAccess::createPropertyAccessor() |
182 | 192 | ); |
183 | 193 |
|
184 | | - $normalizer = new AnnotationNormalizer($annotationReader, Export::class, $propertyManipulator); |
185 | | - $encoder = new XmlEncoder('mock'); |
| 194 | + $normalizers = [ |
| 195 | + new DateTimeNormalizer(), |
| 196 | + new IterableNormalizer(), |
| 197 | + new AnnotationNormalizer($annotationReader, Export::class, $propertyManipulator), |
| 198 | + ]; |
| 199 | + $encoders = [new XmlEncoder('mock')]; |
186 | 200 |
|
187 | 201 | $serializer = new Serializer( |
188 | | - array($normalizer), |
189 | | - array($encoder) |
| 202 | + $normalizers, |
| 203 | + $encoders |
190 | 204 | ); |
191 | 205 |
|
192 | 206 | $data = new AnnotatedMock(new AnnotatedMock()); |
193 | 207 |
|
194 | 208 | $this->assertStringEqualsFile( |
195 | | - __DIR__.'/../../Resources/xml/annotation_normalizer_result.xml', |
| 209 | + __DIR__ . '/../../Resources/xml/annotation_normalizer_result.xml', |
196 | 210 | $serializer->serialize($data, 'xml') |
197 | 211 | ); |
198 | 212 | } |
| 213 | + |
| 214 | + /** |
| 215 | + * Test if @see AnnotationNormalizer::normalize returns the expected json normalized data |
| 216 | + * for serialization through the Serializer. |
| 217 | + * |
| 218 | + * @return void |
| 219 | + */ |
| 220 | + public function testNormalizeThroughJsonSerializer() |
| 221 | + { |
| 222 | + $annotationReader = new AnnotationReader(); |
| 223 | + $propertyManipulator = new PropertyManipulator( |
| 224 | + PropertyAccess::createPropertyAccessor() |
| 225 | + ); |
| 226 | + |
| 227 | + $normalizers = [ |
| 228 | + new DateTimeNormalizer(), |
| 229 | + new IterableNormalizer(), |
| 230 | + new AnnotationNormalizer($annotationReader, Export::class, $propertyManipulator), |
| 231 | + ]; |
| 232 | + $encoders = [new JsonEncoder()]; |
| 233 | + |
| 234 | + $serializer = new Serializer( |
| 235 | + $normalizers, |
| 236 | + $encoders |
| 237 | + ); |
| 238 | + |
| 239 | + $data = new AnnotatedMock(new AnnotatedMock()); |
| 240 | + |
| 241 | + $this->assertStringEqualsFile( |
| 242 | + __DIR__ . '/../../Resources/json/annotation_normalize_result.json', |
| 243 | + $serializer->serialize($data, 'json') |
| 244 | + ); |
| 245 | + } |
199 | 246 | } |
0 commit comments