GraphNavigatorTest.php 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. namespace JMS\SerializerBundle\Tests\Serializer;
  3. use JMS\SerializerBundle\Serializer\Construction\UnserializeObjectConstructor;
  4. use JMS\SerializerBundle\Serializer\Handler\HandlerRegistry;
  5. use JMS\SerializerBundle\Serializer\EventDispatcher\EventDispatcher;
  6. use Doctrine\Common\Annotations\AnnotationReader;
  7. use JMS\SerializerBundle\Metadata\Driver\AnnotationDriver;
  8. use JMS\SerializerBundle\Serializer\GraphNavigator;
  9. use Metadata\MetadataFactory;
  10. class GraphNavigatorTest extends \PHPUnit_Framework_TestCase
  11. {
  12. private $metadataFactory;
  13. private $handlerRegistry;
  14. private $objectConstructor;
  15. private $exclusionStrategy;
  16. private $dispatcher;
  17. private $navigator;
  18. private $visitor;
  19. /**
  20. * @expectedException \RuntimeException
  21. * @expectedExceptionMessage Resources are not supported in serialized data.
  22. */
  23. public function testResourceThrowsException()
  24. {
  25. $this->navigator->accept(STDIN, null, $this->visitor);
  26. }
  27. public function testNavigatorPassesInstanceOnSerialization()
  28. {
  29. $object = new SerializableClass;
  30. $metadata = $this->metadataFactory->getMetadataForClass(get_class($object));
  31. $exclusionStrategy = $this->getMock('JMS\SerializerBundle\Serializer\Exclusion\ExclusionStrategyInterface');
  32. $exclusionStrategy->expects($this->once())
  33. ->method('shouldSkipClass')
  34. ->with($metadata, $object);
  35. $exclusionStrategy->expects($this->once())
  36. ->method('shouldSkipProperty')
  37. ->with($metadata->propertyMetadata['foo'], $object);
  38. $this->navigator = new GraphNavigator(GraphNavigator::DIRECTION_SERIALIZATION, $this->metadataFactory, 'foo', $this->handlerRegistry, $this->objectConstructor, $exclusionStrategy, $this->dispatcher);
  39. $this->navigator->accept($object, null, $this->visitor);
  40. }
  41. public function testNavigatorPassesNullOnDeserialization()
  42. {
  43. $class = __NAMESPACE__.'\SerializableClass';
  44. $metadata = $this->metadataFactory->getMetadataForClass($class);
  45. $exclusionStrategy = $this->getMock('JMS\SerializerBundle\Serializer\Exclusion\ExclusionStrategyInterface');
  46. $exclusionStrategy->expects($this->once())
  47. ->method('shouldSkipClass')
  48. ->with($metadata, null);
  49. $exclusionStrategy->expects($this->once())
  50. ->method('shouldSkipProperty')
  51. ->with($metadata->propertyMetadata['foo'], null);
  52. $this->navigator = new GraphNavigator(GraphNavigator::DIRECTION_DESERIALIZATION, $this->metadataFactory, 'foo', $this->handlerRegistry, $this->objectConstructor, $exclusionStrategy, $this->dispatcher);
  53. $this->navigator->accept('random', array('name' => $class, 'params' => array()), $this->visitor);
  54. }
  55. protected function setUp()
  56. {
  57. $this->visitor = $this->getMock('JMS\SerializerBundle\Serializer\VisitorInterface');
  58. $this->dispatcher = new EventDispatcher();
  59. $this->handlerRegistry = new HandlerRegistry();
  60. $this->objectConstructor = new UnserializeObjectConstructor();
  61. $this->metadataFactory = new MetadataFactory(new AnnotationDriver(new AnnotationReader()));
  62. $this->navigator = new GraphNavigator(GraphNavigator::DIRECTION_SERIALIZATION, $this->metadataFactory, 'foo', $this->handlerRegistry, $this->objectConstructor, null, $this->dispatcher);
  63. }
  64. }
  65. class SerializableClass
  66. {
  67. public $foo = 'bar';
  68. }