GraphNavigatorTest.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. namespace JMS\Serializer\Tests\Serializer;
  3. use Doctrine\Common\Annotations\AnnotationReader;
  4. use JMS\Serializer\Construction\UnserializeObjectConstructor;
  5. use JMS\Serializer\EventDispatcher\EventDispatcher;
  6. use JMS\Serializer\GraphNavigator;
  7. use JMS\Serializer\Handler\HandlerRegistry;
  8. use JMS\Serializer\Handler\SubscribingHandlerInterface;
  9. use JMS\Serializer\Metadata\Driver\AnnotationDriver;
  10. use Metadata\MetadataFactory;
  11. class GraphNavigatorTest extends \PHPUnit_Framework_TestCase
  12. {
  13. private $metadataFactory;
  14. private $handlerRegistry;
  15. private $objectConstructor;
  16. private $dispatcher;
  17. private $navigator;
  18. private $context;
  19. /**
  20. * @expectedException JMS\Serializer\Exception\RuntimeException
  21. * @expectedExceptionMessage Resources are not supported in serialized data.
  22. */
  23. public function testResourceThrowsException()
  24. {
  25. $this->context->expects($this->any())
  26. ->method('getDirection')
  27. ->will($this->returnValue(GraphNavigator::DIRECTION_SERIALIZATION));
  28. $this->navigator->accept(STDIN, null, $this->context);
  29. }
  30. public function testNavigatorPassesInstanceOnSerialization()
  31. {
  32. $object = new SerializableClass;
  33. $metadata = $this->metadataFactory->getMetadataForClass(get_class($object));
  34. $self = $this;
  35. $context = $this->context;
  36. $exclusionStrategy = $this->getMockBuilder('JMS\Serializer\Exclusion\ExclusionStrategyInterface')->getMock();
  37. $exclusionStrategy->expects($this->once())
  38. ->method('shouldSkipClass')
  39. ->will($this->returnCallback(function ($passedMetadata, $passedContext) use ($metadata, $context, $self) {
  40. $self->assertSame($metadata, $passedMetadata);
  41. $self->assertSame($context, $passedContext);
  42. }));
  43. $exclusionStrategy->expects($this->once())
  44. ->method('shouldSkipProperty')
  45. ->will($this->returnCallback(function ($propertyMetadata, $passedContext) use ($context, $metadata, $self) {
  46. $self->assertSame($metadata->propertyMetadata['foo'], $propertyMetadata);
  47. $self->assertSame($context, $passedContext);
  48. }));
  49. $this->context->expects($this->once())
  50. ->method('getExclusionStrategy')
  51. ->will($this->returnValue($exclusionStrategy));
  52. $this->context->expects($this->any())
  53. ->method('getDirection')
  54. ->will($this->returnValue(GraphNavigator::DIRECTION_SERIALIZATION));
  55. $this->context->expects($this->any())
  56. ->method('getVisitor')
  57. ->will($this->returnValue($this->getMockBuilder('JMS\Serializer\VisitorInterface')->getMock()));
  58. $this->navigator = new GraphNavigator($this->metadataFactory, $this->handlerRegistry, $this->objectConstructor, $this->dispatcher);
  59. $this->navigator->accept($object, null, $this->context);
  60. }
  61. public function testNavigatorPassesNullOnDeserialization()
  62. {
  63. $class = __NAMESPACE__ . '\SerializableClass';
  64. $metadata = $this->metadataFactory->getMetadataForClass($class);
  65. $context = $this->context;
  66. $exclusionStrategy = $this->getMockBuilder('JMS\Serializer\Exclusion\ExclusionStrategyInterface')->getMock();
  67. $exclusionStrategy->expects($this->once())
  68. ->method('shouldSkipClass')
  69. ->with($metadata, $this->callback(function ($navigatorContext) use ($context) {
  70. return $navigatorContext === $context;
  71. }));
  72. $exclusionStrategy->expects($this->once())
  73. ->method('shouldSkipProperty')
  74. ->with($metadata->propertyMetadata['foo'], $this->callback(function ($navigatorContext) use ($context) {
  75. return $navigatorContext === $context;
  76. }));
  77. $this->context->expects($this->once())
  78. ->method('getExclusionStrategy')
  79. ->will($this->returnValue($exclusionStrategy));
  80. $this->context->expects($this->any())
  81. ->method('getDirection')
  82. ->will($this->returnValue(GraphNavigator::DIRECTION_DESERIALIZATION));
  83. $this->context->expects($this->any())
  84. ->method('getVisitor')
  85. ->will($this->returnValue($this->getMockBuilder('JMS\Serializer\VisitorInterface')->getMock()));
  86. $this->navigator = new GraphNavigator($this->metadataFactory, $this->handlerRegistry, $this->objectConstructor, $this->dispatcher);
  87. $this->navigator->accept('random', array('name' => $class, 'params' => array()), $this->context);
  88. }
  89. public function testNavigatorChangeTypeOnSerialization()
  90. {
  91. $object = new SerializableClass;
  92. $typeName = 'JsonSerializable';
  93. $this->dispatcher->addListener('serializer.pre_serialize', function ($event) use ($typeName) {
  94. $type = $event->getType();
  95. $type['name'] = $typeName;
  96. $event->setType($type['name'], $type['params']);
  97. });
  98. $this->handlerRegistry->registerSubscribingHandler(new TestSubscribingHandler());
  99. $this->context->expects($this->any())
  100. ->method('getDirection')
  101. ->will($this->returnValue(GraphNavigator::DIRECTION_SERIALIZATION));
  102. $this->context->expects($this->any())
  103. ->method('getVisitor')
  104. ->will($this->returnValue($this->getMockBuilder('JMS\Serializer\VisitorInterface')->getMock()));
  105. $this->navigator = new GraphNavigator($this->metadataFactory, $this->handlerRegistry, $this->objectConstructor, $this->dispatcher);
  106. $this->navigator->accept($object, null, $this->context);
  107. }
  108. protected function setUp()
  109. {
  110. $this->context = $this->getMockBuilder('JMS\Serializer\Context')->getMock();
  111. $this->dispatcher = new EventDispatcher();
  112. $this->handlerRegistry = new HandlerRegistry();
  113. $this->objectConstructor = new UnserializeObjectConstructor();
  114. $this->metadataFactory = new MetadataFactory(new AnnotationDriver(new AnnotationReader()));
  115. $this->navigator = new GraphNavigator($this->metadataFactory, $this->handlerRegistry, $this->objectConstructor, $this->dispatcher);
  116. }
  117. }
  118. class SerializableClass
  119. {
  120. public $foo = 'bar';
  121. }
  122. class TestSubscribingHandler implements SubscribingHandlerInterface
  123. {
  124. public static function getSubscribingMethods()
  125. {
  126. return array(array(
  127. 'type' => 'JsonSerializable',
  128. 'format' => 'foo',
  129. 'direction' => GraphNavigator::DIRECTION_SERIALIZATION,
  130. 'method' => 'serialize'
  131. ));
  132. }
  133. }