Completed
Push — master ( 5ba1dc...b85ec4 )
by Tomasz
02:09
created

testMissingClassSerializationHandler()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
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\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');
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 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) {
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 NormalizerContainer();
180
        $hydrators = new HydratorContainer();
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 testInvalidSerializationFormat()
194
    {
195
        $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...
196
        $this->getSerializard()->serialize(new \stdClass(), 'invalid');
197
    }
198
199
    public function testInvalidUnserializationFormat()
200
    {
201
        $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...
202
        $this->getSerializard()->unserialize(new \stdClass(), 'stdClass', 'invalid');
203
    }
204
205
    public function testMissingClassSerializationHandler()
206
    {
207
        $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...
208
        $this->getSerializard()->serialize(new \stdClass(), 'json');
209
    }
210
}
211