|
11 | 11 |
|
12 | 12 | namespace Symfony\Component\Serializer\Tests;
|
13 | 13 |
|
| 14 | +use Doctrine\Common\Annotations\AnnotationReader; |
14 | 15 | use PHPUnit\Framework\TestCase;
|
15 | 16 | use Symfony\Component\Serializer\Mapping\ClassDiscriminatorFromClassMetadata;
|
16 | 17 | use Symfony\Component\Serializer\Mapping\ClassDiscriminatorMapping;
|
17 | 18 | use Symfony\Component\Serializer\Mapping\ClassMetadata;
|
| 19 | +use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory; |
18 | 20 | use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;
|
| 21 | +use Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader; |
19 | 22 | use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer;
|
20 | 23 | use Symfony\Component\Serializer\Normalizer\DenormalizerAwareInterface;
|
21 | 24 | use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
|
@@ -398,63 +401,58 @@ public function testDeserializeAndSerializeInterfacedObjectsWithTheClassMetadata
|
398 | 401 | $example = new DummyMessageNumberOne();
|
399 | 402 | $example->one = 1;
|
400 | 403 |
|
401 |
| - $jsonData = '{"message-type":"one","one":1}'; |
402 |
| - |
403 |
| - $discriminatorResolver = new ClassDiscriminatorFromClassMetadata($this->metadataFactoryMockForDummyInterface()); |
404 |
| - $serializer = new Serializer(array(new ObjectNormalizer(null, null, null, null, $discriminatorResolver)), array('json' => new JsonEncoder())); |
| 404 | + $jsonData = '{"type":"one","one":1,"two":null}'; |
405 | 405 |
|
| 406 | + $serializer = $this->serializerWithClassDiscriminator(); |
406 | 407 | $deserialized = $serializer->deserialize($jsonData, DummyMessageInterface::class, 'json');
|
407 | 408 | $this->assertEquals($example, $deserialized);
|
408 | 409 |
|
409 | 410 | $serialized = $serializer->serialize($deserialized, 'json');
|
410 | 411 | $this->assertEquals($jsonData, $serialized);
|
411 | 412 | }
|
412 | 413 |
|
| 414 | + public function testDeserializeAndSerializeInterfacedObjectsWithTheClassMetadataDiscriminatorResolverAndGroups() |
| 415 | + { |
| 416 | + $example = new DummyMessageNumberOne(); |
| 417 | + $example->two = 2; |
| 418 | + |
| 419 | + $serializer = $this->serializerWithClassDiscriminator(); |
| 420 | + $deserialized = $serializer->deserialize('{"type":"one","one":1,"two":2}', DummyMessageInterface::class, 'json', array( |
| 421 | + 'groups' => array('two'), |
| 422 | + )); |
| 423 | + |
| 424 | + $this->assertEquals($example, $deserialized); |
| 425 | + |
| 426 | + $serialized = $serializer->serialize($deserialized, 'json', array( |
| 427 | + 'groups' => array('two'), |
| 428 | + )); |
| 429 | + |
| 430 | + $this->assertEquals('{"two":2,"type":"one"}', $serialized); |
| 431 | + } |
| 432 | + |
413 | 433 | /**
|
414 | 434 | * @expectedException \Symfony\Component\Serializer\Exception\RuntimeException
|
415 | 435 | * @expectedExceptionMessage The type "second" has no mapped class for the abstract object "Symfony\Component\Serializer\Tests\Fixtures\DummyMessageInterface"
|
416 | 436 | */
|
417 | 437 | public function testExceptionWhenTypeIsNotKnownInDiscriminator()
|
418 | 438 | {
|
419 |
| - $discriminatorResolver = new ClassDiscriminatorFromClassMetadata($this->metadataFactoryMockForDummyInterface()); |
420 |
| - $serializer = new Serializer(array(new ObjectNormalizer(null, null, null, null, $discriminatorResolver)), array('json' => new JsonEncoder())); |
421 |
| - $serializer->deserialize('{"message-type":"second","one":1}', DummyMessageInterface::class, 'json'); |
| 439 | + $this->serializerWithClassDiscriminator()->deserialize('{"type":"second","one":1}', DummyMessageInterface::class, 'json'); |
422 | 440 | }
|
423 | 441 |
|
424 | 442 | /**
|
425 | 443 | * @expectedException \Symfony\Component\Serializer\Exception\RuntimeException
|
426 |
| - * @expectedExceptionMessage Type property "message-type" not found for the abstract object "Symfony\Component\Serializer\Tests\Fixtures\DummyMessageInterface" |
| 444 | + * @expectedExceptionMessage Type property "type" not found for the abstract object "Symfony\Component\Serializer\Tests\Fixtures\DummyMessageInterface" |
427 | 445 | */
|
428 | 446 | public function testExceptionWhenTypeIsNotInTheBodyToDeserialiaze()
|
429 | 447 | {
|
430 |
| - $discriminatorResolver = new ClassDiscriminatorFromClassMetadata($this->metadataFactoryMockForDummyInterface()); |
431 |
| - $serializer = new Serializer(array(new ObjectNormalizer(null, null, null, null, $discriminatorResolver)), array('json' => new JsonEncoder())); |
432 |
| - $serializer->deserialize('{"one":1}', DummyMessageInterface::class, 'json'); |
| 448 | + $this->serializerWithClassDiscriminator()->deserialize('{"one":1}', DummyMessageInterface::class, 'json'); |
433 | 449 | }
|
434 | 450 |
|
435 |
| - private function metadataFactoryMockForDummyInterface() |
| 451 | + private function serializerWithClassDiscriminator() |
436 | 452 | {
|
437 |
| - $factoryMock = $this->getMockBuilder(ClassMetadataFactoryInterface::class)->getMock(); |
438 |
| - $factoryMock->method('hasMetadataFor')->will($this->returnValueMap(array( |
439 |
| - array( |
440 |
| - DummyMessageInterface::class, |
441 |
| - true, |
442 |
| - ), |
443 |
| - ))); |
444 |
| - |
445 |
| - $factoryMock->method('getMetadataFor')->will($this->returnValueMap(array( |
446 |
| - array( |
447 |
| - DummyMessageInterface::class, |
448 |
| - new ClassMetadata( |
449 |
| - DummyMessageInterface::class, |
450 |
| - new ClassDiscriminatorMapping('message-type', array( |
451 |
| - 'one' => DummyMessageNumberOne::class, |
452 |
| - )) |
453 |
| - ), |
454 |
| - ), |
455 |
| - ))); |
| 453 | + $classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader())); |
456 | 454 |
|
457 |
| - return $factoryMock; |
| 455 | + return new Serializer(array(new ObjectNormalizer($classMetadataFactory, null, null, null, new ClassDiscriminatorFromClassMetadata($classMetadataFactory))), array('json' => new JsonEncoder())); |
458 | 456 | }
|
459 | 457 | }
|
460 | 458 |
|
|
0 commit comments