Completed
Branch master (8bfcf4)
by Tomasz
02:29
created

SerializardTest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 12

Importance

Changes 3
Bugs 0 Features 1
Metric Value
wmc 7
c 3
b 0
f 1
lcom 1
cbo 12
dl 0
loc 104
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
B testSerializard() 0 25 1
A provideExamples() 0 13 1
B getSerializard() 0 37 2
A testInvalidSerializationFormat() 0 5 1
A testInvalidUnserializationFormat() 0 5 1
A testMissingClassSerializationHandler() 0 5 1
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
16
/**
17
 * @author Tomasz Kowalczyk <[email protected]>
18
 */
19
class SerializardTest extends \PHPUnit_Framework_TestCase
20
{
21
    /**
22
     * @param string $prefix
23
     * @param callable $factory
24
     *
25
     * @dataProvider provideExamples
26
     */
27
    public function testSerializard($prefix, $factory)
28
    {
29
        $object = $factory();
30
31
        $serializard = $this->getSerializard();
32
33
        $file = __DIR__.'/examples/'.$prefix;
34
35
        $json = $serializard->serialize($object, 'json');
36
        $yaml = $serializard->serialize($object, 'yaml');
37
        $xml = $serializard->serialize($object, 'xml');
38
        $array = $serializard->serialize($object, 'array');
39
40
        $this->assertSame(file_get_contents($file.'.json'), $json."\n");
41
        $this->assertSame(file_get_contents($file.'.yaml'), $yaml);
42
        $this->assertSame(file_get_contents($file.'.xml'), $xml);
43
        $this->assertSame(require($file.'.php'), $array);
44
45
        $userClass = 'Thunder\Serializard\Tests\Fake\FakeUser';
46
47
        $this->assertSame($json, $serializard->serialize($serializard->unserialize($json, $userClass, 'json'), 'json'));
48
        $this->assertSame($yaml, $serializard->serialize($serializard->unserialize($yaml, $userClass, 'yaml'), 'yaml'));
49
        $this->assertSame($xml, $serializard->serialize($serializard->unserialize($xml, $userClass, 'xml'), 'xml'));
50
        $this->assertSame($array, $serializard->serialize($serializard->unserialize($array, $userClass, 'array'), 'array'));
51
    }
52
53
    public function provideExamples()
54
    {
55
        return array(
56
            array('simple', function() {
57
                $user = new FakeUser(1, 'Thomas', new FakeTag(100, 'various'));
58
                $user->addTag(new FakeTag(10, 'sth'));
59
                $user->addTag(new FakeTag(11, 'xyz'));
60
                $user->addTag(new FakeTag(12, 'rnd'));
61
62
                return $user;
63
            }),
64
        );
65
    }
66
67
    private function getSerializard()
68
    {
69
        $userClass = 'Thunder\Serializard\Tests\Fake\FakeUser';
70
        $tagClass = 'Thunder\Serializard\Tests\Fake\FakeTag';
71
72
        $normalizers = new HandlerContainer();
73
        $normalizers->add($userClass, 'user', new ReflectionNormalizer());
74
        $normalizers->add($tagClass, 'tag', function(FakeTag $tag) {
75
            return array(
76
                'id' => $tag->getId(),
77
                'name' => $tag->getName(),
78
            );
79
        });
80
81
        $hydrators = new HandlerContainer();
82
        $hydrators->add($userClass, 'user', function(array $data, Handlers $handlers) use($tagClass) {
83
            $tagHandler = $handlers->getHandler($tagClass);
84
85
            $user = new FakeUser($data['id'], $data['name'], $tagHandler($data['tag'], $handlers));
86
            foreach($data['tags'] as $tag) {
87
                $user->addTag($tagHandler($tag, $handlers));
88
            }
89
90
            return $user;
91
        });
92
        $hydrators->add($tagClass, 'tag', function(array $data, Handlers $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...
93
            return new FakeTag($data['id'], $data['name']);
94
        });
95
96
        $formats = new FormatContainer();
97
        $formats->add('xml', new XmlFormat());
98
        $formats->add('yaml', new YamlFormat());
99
        $formats->add('json', new JsonFormat());
100
        $formats->add('array', new ArrayFormat());
101
102
        return new Serializard($formats, $normalizers, $hydrators);
103
    }
104
105
    public function testInvalidSerializationFormat()
106
    {
107
        $this->setExpectedException('RuntimeException');
108
        $this->getSerializard()->serialize(new \stdClass(), 'invalid');
109
    }
110
111
    public function testInvalidUnserializationFormat()
112
    {
113
        $this->setExpectedException('RuntimeException');
114
        $this->getSerializard()->unserialize(new \stdClass(), 'stdClass', 'invalid');
115
    }
116
117
    public function testMissingClassSerializationHandler()
118
    {
119
        $this->setExpectedException('RuntimeException');
120
        $this->getSerializard()->serialize(new \stdClass(), 'json');
121
    }
122
}
123