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\HandlerContainer\HandlerContainer; |
10
|
|
|
use Thunder\Serializard\HandlerContainer\HandlerContainerInterface as Handlers; |
11
|
|
|
use Thunder\Serializard\Normalizer\ReflectionNormalizer; |
12
|
|
|
use Thunder\Serializard\Serializard; |
13
|
|
|
use Thunder\Serializard\Tests\Fake\FakeTag; |
14
|
|
|
use Thunder\Serializard\Tests\Fake\FakeUser; |
15
|
|
|
use Thunder\Serializard\Tests\Fake\Interfaces\TypeA; |
16
|
|
|
use Thunder\Serializard\Tests\Fake\Interfaces\TypeB; |
17
|
|
|
use Thunder\Serializard\Tests\Fake\Interfaces\TypeInterface; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @author Tomasz Kowalczyk <[email protected]> |
21
|
|
|
*/ |
22
|
|
|
class SerializardTest extends \PHPUnit_Framework_TestCase |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @param string $prefix |
26
|
|
|
* @param callable $factory |
27
|
|
|
* |
28
|
|
|
* @dataProvider provideExamples |
29
|
|
|
*/ |
30
|
|
|
public function testSerializard($prefix, $factory) |
31
|
|
|
{ |
32
|
|
|
$object = $factory(); |
33
|
|
|
|
34
|
|
|
$serializard = $this->getSerializard(); |
35
|
|
|
|
36
|
|
|
$file = __DIR__.'/examples/'.$prefix; |
37
|
|
|
|
38
|
|
|
$json = $serializard->serialize($object, 'json'); |
39
|
|
|
$yaml = $serializard->serialize($object, 'yaml'); |
40
|
|
|
$xml = $serializard->serialize($object, 'xml'); |
41
|
|
|
$array = $serializard->serialize($object, 'array'); |
42
|
|
|
|
43
|
|
|
$this->assertSame(file_get_contents($file.'.json'), $json."\n"); |
44
|
|
|
$this->assertSame(file_get_contents($file.'.yaml'), $yaml); |
45
|
|
|
$this->assertSame(file_get_contents($file.'.xml'), $xml); |
46
|
|
|
$this->assertSame(require($file.'.php'), $array); |
47
|
|
|
|
48
|
|
|
$userClass = 'Thunder\Serializard\Tests\Fake\FakeUser'; |
49
|
|
|
|
50
|
|
|
$this->assertSame($json, $serializard->serialize($serializard->unserialize($json, $userClass, 'json'), 'json')); |
51
|
|
|
$this->assertSame($yaml, $serializard->serialize($serializard->unserialize($yaml, $userClass, 'yaml'), 'yaml')); |
52
|
|
|
$this->assertSame($xml, $serializard->serialize($serializard->unserialize($xml, $userClass, 'xml'), 'xml')); |
53
|
|
|
$this->assertSame($array, $serializard->serialize($serializard->unserialize($array, $userClass, 'array'), 'array')); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function provideExamples() |
57
|
|
|
{ |
58
|
|
|
return array( |
59
|
|
|
array('simple', function() { |
60
|
|
|
$user = new FakeUser(1, 'Thomas', new FakeTag(100, 'various')); |
61
|
|
|
$user->addTag(new FakeTag(10, 'sth')); |
62
|
|
|
$user->addTag(new FakeTag(11, 'xyz')); |
63
|
|
|
$user->addTag(new FakeTag(12, 'rnd')); |
64
|
|
|
|
65
|
|
|
return $user; |
66
|
|
|
}), |
67
|
|
|
); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function testInterfaces() |
71
|
|
|
{ |
72
|
|
|
$interface = 'Thunder\Serializard\Tests\Fake\Interfaces\TypeInterface'; |
73
|
|
|
$handlers = new HandlerContainer(); |
74
|
|
|
$handlers->add($interface, 'type', function(TypeInterface $type) { |
75
|
|
|
return array( |
76
|
|
|
'type' => $type->getType(), |
77
|
|
|
'value' => $type->getValue(), |
78
|
|
|
); |
79
|
|
|
}); |
80
|
|
|
|
81
|
|
|
$normalizers = new HandlerContainer(); |
82
|
|
|
|
83
|
|
|
$formats = new FormatContainer(); |
84
|
|
|
$formats->add('array', new ArrayFormat()); |
85
|
|
|
|
86
|
|
|
$serializard = new Serializard($formats, $handlers, $normalizers); |
87
|
|
|
|
88
|
|
|
$this->assertSame(array('type' => 'typeA', 'value' => 'valueA'), $serializard->serialize(new TypeA(), 'array')); |
89
|
|
|
$this->assertSame(array('type' => 'typeB', 'value' => 'valueB'), $serializard->serialize(new TypeB(), 'array')); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** @dataProvider provideCycles */ |
93
|
|
|
public function testCycleException($var, $format) |
94
|
|
|
{ |
95
|
|
|
$userClass = 'Thunder\Serializard\Tests\Fake\FakeUser'; |
96
|
|
|
$tagClass = 'Thunder\Serializard\Tests\Fake\FakeTag'; |
97
|
|
|
|
98
|
|
|
$normalizers = new HandlerContainer(); |
99
|
|
|
$normalizers->add($userClass, 'user', new ReflectionNormalizer()); |
100
|
|
|
$normalizers->add($tagClass, 'tag', new ReflectionNormalizer()); |
101
|
|
|
|
102
|
|
|
$hydrators = new HandlerContainer(); |
103
|
|
|
|
104
|
|
|
$formats = new FormatContainer(); |
105
|
|
|
$formats->add('xml', new XmlFormat()); |
106
|
|
|
$formats->add('yaml', new YamlFormat()); |
107
|
|
|
$formats->add('json', new JsonFormat()); |
108
|
|
|
$formats->add('array', new ArrayFormat()); |
109
|
|
|
|
110
|
|
|
$serializard = new Serializard($formats, $normalizers, $hydrators); |
111
|
|
|
|
112
|
|
|
|
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 HandlerContainer(); |
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 HandlerContainer(); |
148
|
|
|
$hydrators->add($userClass, 'user', function(array $data, Handlers $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, Handlers $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 testInvalidSerializationFormat() |
172
|
|
|
{ |
173
|
|
|
$this->setExpectedException('RuntimeException'); |
|
|
|
|
174
|
|
|
$this->getSerializard()->serialize(new \stdClass(), 'invalid'); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
public function testInvalidUnserializationFormat() |
178
|
|
|
{ |
179
|
|
|
$this->setExpectedException('RuntimeException'); |
|
|
|
|
180
|
|
|
$this->getSerializard()->unserialize(new \stdClass(), 'stdClass', 'invalid'); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
public function testMissingClassSerializationHandler() |
184
|
|
|
{ |
185
|
|
|
$this->setExpectedException('RuntimeException'); |
|
|
|
|
186
|
|
|
$this->getSerializard()->serialize(new \stdClass(), 'json'); |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
|
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.