GraphNavigatorTest.php 3.5 KB

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