|
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 [ |
|
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
|
|
|
private function getSerializard() |
|
93
|
|
|
{ |
|
94
|
|
|
$userClass = 'Thunder\Serializard\Tests\Fake\FakeUser'; |
|
95
|
|
|
$tagClass = 'Thunder\Serializard\Tests\Fake\FakeTag'; |
|
96
|
|
|
|
|
97
|
|
|
$normalizers = new HandlerContainer(); |
|
98
|
|
|
$normalizers->add($userClass, 'user', new ReflectionNormalizer()); |
|
99
|
|
|
$normalizers->add($tagClass, 'tag', function(FakeTag $tag) { |
|
100
|
|
|
return array( |
|
101
|
|
|
'id' => $tag->getId(), |
|
102
|
|
|
'name' => $tag->getName(), |
|
103
|
|
|
); |
|
104
|
|
|
}); |
|
105
|
|
|
|
|
106
|
|
|
$hydrators = new HandlerContainer(); |
|
107
|
|
|
$hydrators->add($userClass, 'user', function(array $data, Handlers $handlers) use($tagClass) { |
|
108
|
|
|
$tagHandler = $handlers->getHandler($tagClass); |
|
109
|
|
|
|
|
110
|
|
|
$user = new FakeUser($data['id'], $data['name'], $tagHandler($data['tag'], $handlers)); |
|
111
|
|
|
foreach($data['tags'] as $tag) { |
|
112
|
|
|
$user->addTag($tagHandler($tag, $handlers)); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
return $user; |
|
116
|
|
|
}); |
|
117
|
|
|
$hydrators->add($tagClass, 'tag', function(array $data, Handlers $handlers) { |
|
|
|
|
|
|
118
|
|
|
return new FakeTag($data['id'], $data['name']); |
|
119
|
|
|
}); |
|
120
|
|
|
|
|
121
|
|
|
$formats = new FormatContainer(); |
|
122
|
|
|
$formats->add('xml', new XmlFormat()); |
|
123
|
|
|
$formats->add('yaml', new YamlFormat()); |
|
124
|
|
|
$formats->add('json', new JsonFormat()); |
|
125
|
|
|
$formats->add('array', new ArrayFormat()); |
|
126
|
|
|
|
|
127
|
|
|
return new Serializard($formats, $normalizers, $hydrators); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
public function testInvalidSerializationFormat() |
|
131
|
|
|
{ |
|
132
|
|
|
$this->setExpectedException('RuntimeException'); |
|
133
|
|
|
$this->getSerializard()->serialize(new \stdClass(), 'invalid'); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
public function testInvalidUnserializationFormat() |
|
137
|
|
|
{ |
|
138
|
|
|
$this->setExpectedException('RuntimeException'); |
|
139
|
|
|
$this->getSerializard()->unserialize(new \stdClass(), 'stdClass', 'invalid'); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
public function testMissingClassSerializationHandler() |
|
143
|
|
|
{ |
|
144
|
|
|
$this->setExpectedException('RuntimeException'); |
|
145
|
|
|
$this->getSerializard()->serialize(new \stdClass(), 'json'); |
|
146
|
|
|
} |
|
147
|
|
|
} |
|
148
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.