GraphNavigatorTest.php 2.8 KB

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