GraphNavigatorTest.php 2.6 KB

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