1
|
|
|
<?php |
2
|
|
|
namespace Thunder\Serializard\Tests; |
3
|
|
|
|
4
|
|
|
use Thunder\Serializard\Format\ArrayFormat; |
5
|
|
|
use Thunder\Serializard\Format\JsonFormat; |
6
|
|
|
use Thunder\Serializard\Format\XmlFormat; |
7
|
|
|
use Thunder\Serializard\Format\YamlFormat; |
8
|
|
|
use Thunder\Serializard\FormatContainer\FormatContainer; |
9
|
|
|
use Thunder\Serializard\HydratorContainer\HydratorContainer; |
10
|
|
|
use Thunder\Serializard\HydratorContainer\HydratorContainerInterface as Hydrators; |
11
|
|
|
use Thunder\Serializard\Normalizer\ReflectionNormalizer; |
12
|
|
|
use Thunder\Serializard\NormalizerContainer\NormalizerContainer; |
13
|
|
|
use Thunder\Serializard\Serializard; |
14
|
|
|
use Thunder\Serializard\Tests\Fake\FakeTag; |
15
|
|
|
use Thunder\Serializard\Tests\Fake\FakeUser; |
16
|
|
|
use Thunder\Serializard\Tests\Fake\FakeUserParent; |
17
|
|
|
use Thunder\Serializard\Tests\Fake\FakeUserParentParent; |
18
|
|
|
use Thunder\Serializard\Tests\Fake\Interfaces\TypeA; |
19
|
|
|
use Thunder\Serializard\Tests\Fake\Interfaces\TypeB; |
20
|
|
|
use Thunder\Serializard\Tests\Fake\Interfaces\TypeInterface; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @author Tomasz Kowalczyk <[email protected]> |
24
|
|
|
*/ |
25
|
|
|
class SerializardTest extends \PHPUnit_Framework_TestCase |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @param string $prefix |
29
|
|
|
* @param callable $factory |
30
|
|
|
* |
31
|
|
|
* @dataProvider provideExamples |
32
|
|
|
*/ |
33
|
|
|
public function testSerializard($prefix, $factory) |
34
|
|
|
{ |
35
|
|
|
$object = $factory(); |
36
|
|
|
|
37
|
|
|
$serializard = $this->getSerializard(); |
38
|
|
|
|
39
|
|
|
$file = __DIR__.'/examples/'.$prefix; |
40
|
|
|
|
41
|
|
|
$json = $serializard->serialize($object, 'json'); |
42
|
|
|
$yaml = $serializard->serialize($object, 'yaml'); |
43
|
|
|
$xml = $serializard->serialize($object, 'xml'); |
44
|
|
|
$array = $serializard->serialize($object, 'array'); |
45
|
|
|
|
46
|
|
|
$this->assertSame(file_get_contents($file.'.json'), $json."\n"); |
47
|
|
|
$this->assertSame(file_get_contents($file.'.yaml'), $yaml); |
48
|
|
|
$this->assertSame(file_get_contents($file.'.xml'), $xml); |
49
|
|
|
$this->assertSame(require($file.'.php'), $array); |
50
|
|
|
|
51
|
|
|
$userClass = 'Thunder\Serializard\Tests\Fake\FakeUser'; |
52
|
|
|
|
53
|
|
|
$this->assertSame($json, $serializard->serialize($serializard->unserialize($json, $userClass, 'json'), 'json')); |
54
|
|
|
$this->assertSame($yaml, $serializard->serialize($serializard->unserialize($yaml, $userClass, 'yaml'), 'yaml')); |
55
|
|
|
$this->assertSame($xml, $serializard->serialize($serializard->unserialize($xml, $userClass, 'xml'), 'xml')); |
56
|
|
|
$this->assertSame($array, $serializard->serialize($serializard->unserialize($array, $userClass, 'array'), 'array')); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function provideExamples() |
60
|
|
|
{ |
61
|
|
|
return array( |
62
|
|
|
array('simple', function() { |
63
|
|
|
$user = new FakeUser(1, 'Thomas', new FakeTag(100, 'various')); |
64
|
|
|
$user->addTag(new FakeTag(10, 'sth')); |
65
|
|
|
$user->addTag(new FakeTag(11, 'xyz')); |
66
|
|
|
$user->addTag(new FakeTag(12, 'rnd')); |
67
|
|
|
|
68
|
|
|
return $user; |
69
|
|
|
}), |
70
|
|
|
); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function testInterfaces() |
74
|
|
|
{ |
75
|
|
|
$interface = 'Thunder\Serializard\Tests\Fake\Interfaces\TypeInterface'; |
76
|
|
|
$normalizers = new NormalizerContainer(); |
77
|
|
|
$normalizers->add($interface, 'type', function(TypeInterface $type) { |
78
|
|
|
return array( |
79
|
|
|
'type' => $type->getType(), |
80
|
|
|
'value' => $type->getValue(), |
81
|
|
|
); |
82
|
|
|
}); |
83
|
|
|
|
84
|
|
|
$hydrators = new HydratorContainer(); |
85
|
|
|
|
86
|
|
|
$formats = new FormatContainer(); |
87
|
|
|
$formats->add('array', new ArrayFormat()); |
88
|
|
|
|
89
|
|
|
$serializard = new Serializard($formats, $normalizers, $hydrators); |
90
|
|
|
|
91
|
|
|
$this->assertSame(array('type' => 'typeA', 'value' => 'valueA'), $serializard->serialize(new TypeA(), 'array')); |
92
|
|
|
$this->assertSame(array('type' => 'typeB', 'value' => 'valueB'), $serializard->serialize(new TypeB(), 'array')); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** @dataProvider provideCycles */ |
96
|
|
|
public function testCycleException($var, $format) |
97
|
|
|
{ |
98
|
|
|
$userClass = 'Thunder\Serializard\Tests\Fake\FakeUser'; |
99
|
|
|
$tagClass = 'Thunder\Serializard\Tests\Fake\FakeTag'; |
100
|
|
|
|
101
|
|
|
$normalizers = new NormalizerContainer(); |
102
|
|
|
$normalizers->add($userClass, 'user', new ReflectionNormalizer()); |
103
|
|
|
$normalizers->add($tagClass, 'tag', new ReflectionNormalizer()); |
104
|
|
|
|
105
|
|
|
$hydrators = new HydratorContainer(); |
106
|
|
|
|
107
|
|
|
$formats = new FormatContainer(); |
108
|
|
|
$formats->add('xml', new XmlFormat()); |
109
|
|
|
$formats->add('yaml', new YamlFormat()); |
110
|
|
|
$formats->add('json', new JsonFormat()); |
111
|
|
|
$formats->add('array', new ArrayFormat()); |
112
|
|
|
|
113
|
|
|
$serializard = new Serializard($formats, $normalizers, $hydrators); |
114
|
|
|
|
115
|
|
|
$this->setExpectedException('RuntimeException'); |
|
|
|
|
116
|
|
|
$serializard->serialize($var, $format); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public function provideCycles() |
120
|
|
|
{ |
121
|
|
|
$user = new FakeUser(1, 'Thomas', new FakeTag(100, 'various')); |
122
|
|
|
$user->addTag(new FakeTag(10, 'sth')); |
123
|
|
|
$user->addTag(new FakeTag(11, 'xyz')); |
124
|
|
|
$user->addTag(new FakeTag(12, 'rnd')); |
125
|
|
|
|
126
|
|
|
return array( |
127
|
|
|
array($user, 'xml'), |
128
|
|
|
array($user, 'json'), |
129
|
|
|
array($user, 'yaml'), |
130
|
|
|
array($user, 'array'), |
131
|
|
|
); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
private function getSerializard() |
135
|
|
|
{ |
136
|
|
|
$userClass = 'Thunder\Serializard\Tests\Fake\FakeUser'; |
137
|
|
|
$tagClass = 'Thunder\Serializard\Tests\Fake\FakeTag'; |
138
|
|
|
|
139
|
|
|
$normalizers = new NormalizerContainer(); |
140
|
|
|
$normalizers->add($userClass, 'user', new ReflectionNormalizer()); |
141
|
|
|
$normalizers->add($tagClass, 'tag', function(FakeTag $tag) { |
142
|
|
|
return array( |
143
|
|
|
'id' => $tag->getId(), |
144
|
|
|
'name' => $tag->getName(), |
145
|
|
|
); |
146
|
|
|
}); |
147
|
|
|
|
148
|
|
|
$hydrators = new HydratorContainer(); |
149
|
|
|
$hydrators->add($userClass, 'user', function(array $data, Hydrators $handlers) use($tagClass) { |
150
|
|
|
$tagHandler = $handlers->getHandler($tagClass); |
151
|
|
|
|
152
|
|
|
$user = new FakeUser($data['id'], $data['name'], $tagHandler($data['tag'], $handlers)); |
153
|
|
|
foreach($data['tags'] as $tag) { |
154
|
|
|
$user->addTag($tagHandler($tag, $handlers)); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
return $user; |
158
|
|
|
}); |
159
|
|
|
$hydrators->add($tagClass, 'tag', function(array $data, Hydrators $handlers) { |
|
|
|
|
160
|
|
|
return new FakeTag($data['id'], $data['name']); |
161
|
|
|
}); |
162
|
|
|
|
163
|
|
|
$formats = new FormatContainer(); |
164
|
|
|
$formats->add('xml', new XmlFormat()); |
165
|
|
|
$formats->add('yaml', new YamlFormat()); |
166
|
|
|
$formats->add('json', new JsonFormat()); |
167
|
|
|
$formats->add('array', new ArrayFormat()); |
168
|
|
|
|
169
|
|
|
return new Serializard($formats, $normalizers, $hydrators); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
public function testParent() |
173
|
|
|
{ |
174
|
|
|
$userClass = 'Thunder\Serializard\Tests\Fake\FakeUser'; |
175
|
|
|
$user = new FakeUser(1, '[email protected]', new FakeTag(1, 'tag')); |
176
|
|
|
|
177
|
|
|
$formats = new FormatContainer(); |
178
|
|
|
$formats->add('array', new ArrayFormat()); |
179
|
|
|
$normalizers = new NormalizerContainer(); |
180
|
|
|
$hydrators = new HydratorContainer(); |
181
|
|
|
$serializard = new Serializard($formats, $normalizers, $hydrators); |
182
|
|
|
|
183
|
|
|
$normalizers->add($userClass.'ParentParent', 'user', function(FakeUserParentParent $user) { return 'ancestor'; }); |
|
|
|
|
184
|
|
|
$this->assertSame('ancestor', $serializard->serialize($user, 'array')); |
185
|
|
|
|
186
|
|
|
$normalizers->add($userClass.'Parent', 'user', function(FakeUserParent $user) { return 'parent'; }); |
|
|
|
|
187
|
|
|
$this->assertSame('parent', $serializard->serialize($user, 'array')); |
188
|
|
|
|
189
|
|
|
$normalizers->add($userClass, 'user', function(FakeUser $user) { return 'user'; }); |
|
|
|
|
190
|
|
|
$this->assertSame('user', $serializard->serialize($user, 'array')); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
public function testInvalidSerializationFormat() |
194
|
|
|
{ |
195
|
|
|
$this->setExpectedException('RuntimeException'); |
|
|
|
|
196
|
|
|
$this->getSerializard()->serialize(new \stdClass(), 'invalid'); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
public function testInvalidUnserializationFormat() |
200
|
|
|
{ |
201
|
|
|
$this->setExpectedException('RuntimeException'); |
|
|
|
|
202
|
|
|
$this->getSerializard()->unserialize(new \stdClass(), 'stdClass', 'invalid'); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
public function testMissingClassSerializationHandler() |
206
|
|
|
{ |
207
|
|
|
$this->setExpectedException('RuntimeException'); |
|
|
|
|
208
|
|
|
$this->getSerializard()->serialize(new \stdClass(), 'json'); |
209
|
|
|
} |
210
|
|
|
} |
211
|
|
|
|
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.