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\FallbackHydratorContainer; |
10
|
|
|
use Thunder\Serializard\HydratorContainer\HydratorContainerInterface as Hydrators; |
11
|
|
|
use Thunder\Serializard\Normalizer\ReflectionNormalizer; |
12
|
|
|
use Thunder\Serializard\NormalizerContainer\FallbackNormalizerContainer; |
13
|
|
|
use Thunder\Serializard\NormalizerContext\NormalizerContextInterface; |
14
|
|
|
use Thunder\Serializard\Serializard; |
15
|
|
|
use Thunder\Serializard\Tests\Fake\FakeArticle; |
16
|
|
|
use Thunder\Serializard\Tests\Fake\FakeTag; |
17
|
|
|
use Thunder\Serializard\Tests\Fake\FakeUser; |
18
|
|
|
use Thunder\Serializard\Tests\Fake\FakeUserParent; |
19
|
|
|
use Thunder\Serializard\Tests\Fake\FakeUserParentParent; |
20
|
|
|
use Thunder\Serializard\Tests\Fake\Interfaces\TypeA; |
21
|
|
|
use Thunder\Serializard\Tests\Fake\Interfaces\TypeB; |
22
|
|
|
use Thunder\Serializard\Tests\Fake\Interfaces\TypeInterface; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @author Tomasz Kowalczyk <[email protected]> |
26
|
|
|
*/ |
27
|
|
|
final class SerializardTest extends \PHPUnit_Framework_TestCase |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @dataProvider provideExamples |
31
|
|
|
*/ |
32
|
|
|
public function testSerializard($prefix, $factory) |
33
|
|
|
{ |
34
|
|
|
$object = $factory(); |
35
|
|
|
|
36
|
|
|
$serializard = $this->getSerializard(); |
37
|
|
|
|
38
|
|
|
$file = __DIR__.'/examples/'.$prefix; |
39
|
|
|
|
40
|
|
|
$json = $serializard->serialize($object, 'json'); |
41
|
|
|
$yaml = $serializard->serialize($object, 'yaml'); |
42
|
|
|
$xml = $serializard->serialize($object, 'xml'); |
43
|
|
|
$array = $serializard->serialize($object, 'array'); |
44
|
|
|
|
45
|
|
|
$this->assertSame(file_get_contents($file.'.json'), $json."\n"); |
46
|
|
|
$this->assertSame(file_get_contents($file.'.yaml'), $yaml); |
47
|
|
|
$this->assertSame(file_get_contents($file.'.xml'), $xml); |
48
|
|
|
$this->assertSame(require($file.'.php'), $array); |
49
|
|
|
|
50
|
|
|
$userClass = 'Thunder\Serializard\Tests\Fake\FakeUser'; |
51
|
|
|
|
52
|
|
|
$this->assertSame($json, $serializard->serialize($serializard->unserialize($json, $userClass, 'json'), 'json')); |
53
|
|
|
$this->assertSame($yaml, $serializard->serialize($serializard->unserialize($yaml, $userClass, 'yaml'), 'yaml')); |
54
|
|
|
$this->assertSame($xml, $serializard->serialize($serializard->unserialize($xml, $userClass, 'xml'), 'xml')); |
55
|
|
|
$this->assertSame($array, $serializard->serialize($serializard->unserialize($array, $userClass, 'array'), 'array')); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function provideExamples() |
59
|
|
|
{ |
60
|
|
|
return array( |
61
|
|
|
array('simple', function() { |
62
|
|
|
$user = new FakeUser(1, 'Thomas', new FakeTag(100, 'various')); |
63
|
|
|
$user->addTag(new FakeTag(10, 'sth')); |
64
|
|
|
$user->addTag(new FakeTag(11, 'xyz')); |
65
|
|
|
$user->addTag(new FakeTag(12, 'rnd')); |
66
|
|
|
|
67
|
|
|
return $user; |
68
|
|
|
}), |
69
|
|
|
); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function testInterfaces() |
73
|
|
|
{ |
74
|
|
|
$interface = 'Thunder\Serializard\Tests\Fake\Interfaces\TypeInterface'; |
75
|
|
|
$normalizers = new FallbackNormalizerContainer(); |
76
|
|
|
$normalizers->add($interface, 'type', function(TypeInterface $type) { |
77
|
|
|
return array( |
78
|
|
|
'type' => $type->getType(), |
79
|
|
|
'value' => $type->getValue(), |
80
|
|
|
); |
81
|
|
|
}); |
82
|
|
|
|
83
|
|
|
$hydrators = new FallbackHydratorContainer(); |
84
|
|
|
|
85
|
|
|
$formats = new FormatContainer(); |
86
|
|
|
$formats->add('array', new ArrayFormat()); |
87
|
|
|
|
88
|
|
|
$serializard = new Serializard($formats, $normalizers, $hydrators); |
89
|
|
|
|
90
|
|
|
$this->assertSame(array('type' => 'typeA', 'value' => 'valueA'), $serializard->serialize(new TypeA(), 'array')); |
91
|
|
|
$this->assertSame(array('type' => 'typeB', 'value' => 'valueB'), $serializard->serialize(new TypeB(), 'array')); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** @dataProvider provideCycles */ |
95
|
|
|
public function testCycleException($var, $format) |
96
|
|
|
{ |
97
|
|
|
$userClass = 'Thunder\Serializard\Tests\Fake\FakeUser'; |
98
|
|
|
$tagClass = 'Thunder\Serializard\Tests\Fake\FakeTag'; |
99
|
|
|
|
100
|
|
|
$normalizers = new FallbackNormalizerContainer(); |
101
|
|
|
$normalizers->add($userClass, 'user', new ReflectionNormalizer()); |
102
|
|
|
$normalizers->add($tagClass, 'tag', new ReflectionNormalizer()); |
103
|
|
|
|
104
|
|
|
$hydrators = new FallbackHydratorContainer(); |
105
|
|
|
|
106
|
|
|
$formats = new FormatContainer(); |
107
|
|
|
$formats->add('xml', new XmlFormat()); |
108
|
|
|
$formats->add('yaml', new YamlFormat()); |
109
|
|
|
$formats->add('json', new JsonFormat()); |
110
|
|
|
$formats->add('array', new ArrayFormat()); |
111
|
|
|
|
112
|
|
|
$serializard = new Serializard($formats, $normalizers, $hydrators); |
113
|
|
|
|
114
|
|
|
$this->setExpectedException('RuntimeException'); |
|
|
|
|
115
|
|
|
$serializard->serialize($var, $format); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function provideCycles() |
119
|
|
|
{ |
120
|
|
|
$user = new FakeUser(1, 'Thomas', new FakeTag(100, 'various')); |
121
|
|
|
$user->addTag(new FakeTag(10, 'sth')); |
122
|
|
|
$user->addTag(new FakeTag(11, 'xyz')); |
123
|
|
|
$user->addTag(new FakeTag(12, 'rnd')); |
124
|
|
|
|
125
|
|
|
return array( |
126
|
|
|
array($user, 'xml'), |
127
|
|
|
array($user, 'json'), |
128
|
|
|
array($user, 'yaml'), |
129
|
|
|
array($user, 'array'), |
130
|
|
|
); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
private function getSerializard() |
134
|
|
|
{ |
135
|
|
|
$userClass = 'Thunder\Serializard\Tests\Fake\FakeUser'; |
136
|
|
|
$tagClass = 'Thunder\Serializard\Tests\Fake\FakeTag'; |
137
|
|
|
|
138
|
|
|
$normalizers = new FallbackNormalizerContainer(); |
139
|
|
|
$normalizers->add($userClass, 'user', new ReflectionNormalizer()); |
140
|
|
|
$normalizers->add($tagClass, 'tag', function(FakeTag $tag) { |
141
|
|
|
return array( |
142
|
|
|
'id' => $tag->getId(), |
143
|
|
|
'name' => $tag->getName(), |
144
|
|
|
); |
145
|
|
|
}); |
146
|
|
|
|
147
|
|
|
$hydrators = new FallbackHydratorContainer(); |
148
|
|
|
$hydrators->add($userClass, 'user', function(array $data, Hydrators $handlers) use($tagClass) { |
149
|
|
|
$tagHandler = $handlers->getHandler($tagClass); |
150
|
|
|
|
151
|
|
|
$user = new FakeUser($data['id'], $data['name'], $tagHandler($data['tag'], $handlers)); |
152
|
|
|
foreach($data['tags'] as $tag) { |
153
|
|
|
$user->addTag($tagHandler($tag, $handlers)); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
return $user; |
157
|
|
|
}); |
158
|
|
|
$hydrators->add($tagClass, 'tag', function(array $data, Hydrators $handlers) { |
|
|
|
|
159
|
|
|
return new FakeTag($data['id'], $data['name']); |
160
|
|
|
}); |
161
|
|
|
|
162
|
|
|
$formats = new FormatContainer(); |
163
|
|
|
$formats->add('xml', new XmlFormat()); |
164
|
|
|
$formats->add('yaml', new YamlFormat()); |
165
|
|
|
$formats->add('json', new JsonFormat()); |
166
|
|
|
$formats->add('array', new ArrayFormat()); |
167
|
|
|
|
168
|
|
|
return new Serializard($formats, $normalizers, $hydrators); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
public function testParent() |
172
|
|
|
{ |
173
|
|
|
$userClass = 'Thunder\Serializard\Tests\Fake\FakeUser'; |
174
|
|
|
$user = new FakeUser(1, '[email protected]', new FakeTag(1, 'tag')); |
175
|
|
|
|
176
|
|
|
$formats = new FormatContainer(); |
177
|
|
|
$formats->add('array', new ArrayFormat()); |
178
|
|
|
$normalizers = new FallbackNormalizerContainer(); |
179
|
|
|
$hydrators = new FallbackHydratorContainer(); |
180
|
|
|
$serializard = new Serializard($formats, $normalizers, $hydrators); |
181
|
|
|
|
182
|
|
|
$normalizers->add($userClass.'ParentParent', 'user', function(FakeUserParentParent $user) { return 'ancestor'; }); |
|
|
|
|
183
|
|
|
$this->assertSame('ancestor', $serializard->serialize($user, 'array')); |
184
|
|
|
|
185
|
|
|
$normalizers->add($userClass.'Parent', 'user', function(FakeUserParent $user) { return 'parent'; }); |
|
|
|
|
186
|
|
|
$this->assertSame('parent', $serializard->serialize($user, 'array')); |
187
|
|
|
|
188
|
|
|
$normalizers->add($userClass, 'user', function(FakeUser $user) { return 'user'; }); |
|
|
|
|
189
|
|
|
$this->assertSame('user', $serializard->serialize($user, 'array')); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
public function testContext() |
193
|
|
|
{ |
194
|
|
|
$userClass = 'Thunder\Serializard\Tests\Fake\FakeUser'; |
195
|
|
|
$articleClass = 'Thunder\Serializard\Tests\Fake\FakeArticle'; |
196
|
|
|
$tagClass = 'Thunder\Serializard\Tests\Fake\FakeTag'; |
197
|
|
|
|
198
|
|
|
$formats = new FormatContainer(); |
199
|
|
|
$formats->add('format', new ArrayFormat()); |
200
|
|
|
|
201
|
|
|
$hydrators = new FallbackHydratorContainer(); |
202
|
|
|
$normalizers = new FallbackNormalizerContainer(); |
203
|
|
|
$normalizers->add($articleClass, 'article', function(FakeArticle $article) { |
204
|
|
|
return $article->getTag(); |
205
|
|
|
}); |
206
|
|
|
$normalizers->add($userClass, 'user', function(FakeUser $user) { |
207
|
|
|
return $user->getTag(); |
208
|
|
|
}); |
209
|
|
|
$normalizers->add($tagClass, 'tag', function(FakeTag $tag, NormalizerContextInterface $context) { |
210
|
|
|
return get_class($context->getParent()); |
211
|
|
|
}); |
212
|
|
|
|
213
|
|
|
$serializard = new Serializard($formats, $normalizers, $hydrators); |
214
|
|
|
|
215
|
|
|
$tag = new FakeTag(1, 'name'); |
216
|
|
|
$user = new FakeUser(1, 'name', $tag); |
217
|
|
|
$article = new FakeArticle(1, 'title', $user, $tag); |
218
|
|
|
|
219
|
|
|
$this->assertSame($userClass, $serializard->serialize($user, 'format')); |
220
|
|
|
$this->assertSame($articleClass, $serializard->serialize($article, 'format')); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
public function testInvalidSerializationFormat() |
224
|
|
|
{ |
225
|
|
|
$this->setExpectedException('RuntimeException'); |
|
|
|
|
226
|
|
|
$this->getSerializard()->serialize(new \stdClass(), 'invalid'); |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
public function testInvalidUnserializationFormat() |
230
|
|
|
{ |
231
|
|
|
$this->setExpectedException('RuntimeException'); |
|
|
|
|
232
|
|
|
$this->getSerializard()->unserialize(new \stdClass(), 'stdClass', 'invalid'); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
public function testMissingClassSerializationHandler() |
236
|
|
|
{ |
237
|
|
|
$this->setExpectedException('RuntimeException'); |
|
|
|
|
238
|
|
|
$this->getSerializard()->serialize(new \stdClass(), 'json'); |
239
|
|
|
} |
240
|
|
|
} |
241
|
|
|
|
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.