Completed
Pull Request — master (#2)
by Tomasz
02:43
created

SerializardTest::provideCycles()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 10
nc 1
nop 0
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\NormalizerContext\ParentNormalizerContext;
15
use Thunder\Serializard\Serializard;
16
use Thunder\Serializard\Tests\Fake\FakeArticle;
17
use Thunder\Serializard\Tests\Fake\FakeTag;
18
use Thunder\Serializard\Tests\Fake\FakeUser;
19
use Thunder\Serializard\Tests\Fake\FakeUserParent;
20
use Thunder\Serializard\Tests\Fake\FakeUserParentParent;
21
use Thunder\Serializard\Tests\Fake\Interfaces\TypeA;
22
use Thunder\Serializard\Tests\Fake\Interfaces\TypeB;
23
use Thunder\Serializard\Tests\Fake\Interfaces\TypeInterface;
24
25
/**
26
 * @author Tomasz Kowalczyk <[email protected]>
27
 */
28
final class SerializardTest extends \PHPUnit_Framework_TestCase
29
{
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 FallbackNormalizerContainer();
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 FallbackHydratorContainer();
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 FallbackNormalizerContainer();
102
        $normalizers->add($userClass, 'user', new ReflectionNormalizer());
103
        $normalizers->add($tagClass, 'tag', new ReflectionNormalizer());
104
105
        $hydrators = new FallbackHydratorContainer();
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');
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::setExpectedException() has been deprecated with message: Method deprecated since Release 5.2.0

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.

Loading history...
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 FallbackNormalizerContainer();
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 FallbackHydratorContainer();
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) {
0 ignored issues
show
Unused Code introduced by
The parameter $handlers is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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 FallbackNormalizerContainer();
180
        $hydrators = new FallbackHydratorContainer();
181
        $serializard = new Serializard($formats, $normalizers, $hydrators);
182
183
        $normalizers->add($userClass.'ParentParent', 'user', function(FakeUserParentParent $user) { return 'ancestor'; });
0 ignored issues
show
Unused Code introduced by
The parameter $user is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
184
        $this->assertSame('ancestor', $serializard->serialize($user, 'array'));
185
186
        $normalizers->add($userClass.'Parent', 'user', function(FakeUserParent $user) { return 'parent'; });
0 ignored issues
show
Unused Code introduced by
The parameter $user is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
187
        $this->assertSame('parent', $serializard->serialize($user, 'array'));
188
189
        $normalizers->add($userClass, 'user', function(FakeUser $user) { return 'user'; });
0 ignored issues
show
Unused Code introduced by
The parameter $user is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
190
        $this->assertSame('user', $serializard->serialize($user, 'array'));
191
    }
192
193
    public function testContext()
194
    {
195
        $formats = new FormatContainer();
196
        $formats->add('format', new ArrayFormat());
197
198
        $hydrators = new FallbackHydratorContainer();
199
        $normalizers = new FallbackNormalizerContainer();
200
        $normalizers->add(FakeArticle::class, 'article', function(FakeArticle $article) {
201
            return $article->getTag();
202
        });
203
        $normalizers->add(FakeUser::class, 'user', function(FakeUser $user) {
204
            return $user->getTag();
205
        });
206
        $normalizers->add(FakeTag::class, 'tag', function(FakeTag $tag, NormalizerContextInterface $context) {
207
            return get_class($context->getParent());
208
        });
209
210
        $serializard = new Serializard($formats, $normalizers, $hydrators);
211
212
        $tag = new FakeTag(1, 'name');
213
        $user = new FakeUser(1, 'name', $tag);
214
        $this->assertSame(FakeUser::class, $serializard->serialize($user, 'format'));
215
        $this->assertSame(FakeArticle::class, $serializard->serialize(new FakeArticle(1, 'title', $user, $tag), 'format'));
216
    }
217
218
    public function testInvalidSerializationFormat()
219
    {
220
        $this->setExpectedException('RuntimeException');
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::setExpectedException() has been deprecated with message: Method deprecated since Release 5.2.0

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.

Loading history...
221
        $this->getSerializard()->serialize(new \stdClass(), 'invalid');
222
    }
223
224
    public function testInvalidUnserializationFormat()
225
    {
226
        $this->setExpectedException('RuntimeException');
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::setExpectedException() has been deprecated with message: Method deprecated since Release 5.2.0

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.

Loading history...
227
        $this->getSerializard()->unserialize(new \stdClass(), 'stdClass', 'invalid');
228
    }
229
230
    public function testMissingClassSerializationHandler()
231
    {
232
        $this->setExpectedException('RuntimeException');
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::setExpectedException() has been deprecated with message: Method deprecated since Release 5.2.0

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.

Loading history...
233
        $this->getSerializard()->serialize(new \stdClass(), 'json');
234
    }
235
}
236