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