Completed
Push — master ( 303994...b7dcca )
by Martin
02:06
created

MarshalXmlTest::testSettingProlog()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 0
loc 14
rs 9.4285
c 1
b 0
f 1
cc 1
eloc 8
nc 1
nop 0
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace KingsonDe\Marshal;
6
7
use KingsonDe\Marshal\Data\CollectionCallable;
8
use KingsonDe\Marshal\Example\Mapper\ArgumentMapper;
9
use KingsonDe\Marshal\Example\Mapper\ContainerMapper;
10
use KingsonDe\Marshal\Example\Mapper\ServiceMapper;
11
use KingsonDe\Marshal\Example\Model\Service;
12
use PHPUnit\Framework\TestCase;
13
14
class MarshalXmlTest extends TestCase {
15
16
    public function testSerializeXml() {
17
        $xml = MarshalXml::serializeItemCallable(function(\stdClass $user) {
18
            return [
19
                'root' => [
20
                    MarshalXml::ATTRIBUTES_KEY => [
21
                        'year' => 2017,
22
                    ],
23
                    'id'        => $user->id,
24
                    'score'     => [
25
                        MarshalXml::ATTRIBUTES_KEY => [
26
                            'public' => true,
27
                            'highscore' => 'yes',
28
                        ],
29
                        MarshalXml::DATA_KEY => $user->score,
30
                    ],
31
                    'email'     => $user->email,
32
                    'null'      => null,
33
                    'nicknames' => new CollectionCallable(function ($nickname) {
34
                        return [
35
                            'nickname' => $nickname,
36
                        ];
37
                    }, $user->nicknames),
38
                ],
39
            ];
40
        }, $this->createUser());
41
42
        $this->assertXmlStringEqualsXmlFile(__DIR__ . '/Fixtures/User.xml', $xml);
43
    }
44
45
    public function testSerializeRootNodeWithScalarValue() {
46
        $xml = MarshalXml::serializeItemCallable(function() {
0 ignored issues
show
Bug introduced by
The call to serializeItemCallable() misses a required argument $...$data.

This check looks for function calls that miss required arguments.

Loading history...
47
            return [
48
                'root' => [
49
                    MarshalXml::ATTRIBUTES_KEY => [
50
                        'id' => 123,
51
                    ],
52
                    MarshalXml::DATA_KEY => 'Hello World!',
53
                ],
54
            ];
55
        });
56
57
        $this->assertXmlStringEqualsXmlString(
58
            '<?xml version="1.0" encoding="UTF-8"?><root id="123">Hello World!</root>',
59
            $xml
60
        );
61
    }
62
63
    public function testXmlMapper() {
64
        $xml = MarshalXml::serializeItem(new ContainerMapper(), ...$this->getServices());
65
66
        $this->assertXmlStringEqualsXmlFile(__DIR__ . '/Fixtures/Services.xml', $xml);
67
    }
68
69
    public function testBuildDataStructureIsNull() {
70
        $xml = MarshalXml::serializeItemCallable(function () {
0 ignored issues
show
Bug introduced by
The call to serializeItemCallable() misses a required argument $...$data.

This check looks for function calls that miss required arguments.

Loading history...
71
            return null;
72
        });
73
74
        $this->assertXmlStringEqualsXmlString('<?xml version="1.0" encoding="UTF-8"?><root/>', $xml);
75
    }
76
77
    public function testSettingProlog() {
78
        MarshalXml::setVersion('1.1');
79
        MarshalXml::setEncoding('ISO-8859-15');
80
81
        $xml = MarshalXml::serializeItemCallable(function () {
0 ignored issues
show
Bug introduced by
The call to serializeItemCallable() misses a required argument $...$data.

This check looks for function calls that miss required arguments.

Loading history...
82
            return [
83
                'root' => [
84
                    'currency' => '€',
85
                ]
86
            ];
87
        });
88
89
        $this->assertXmlStringEqualsXmlFile(__DIR__ . '/Fixtures/Currency.xml', $xml);
90
    }
91
92
    private function createUser() {
93
        $user            = new \stdClass();
94
        $user->id        = 123;
95
        $user->score     = 3.0;
96
        $user->email     = '[email protected]';
97
        $user->nicknames = ['pfefferkuchenmann', 'lululu'];
98
99
        return $user;
100
    }
101
102
    private function getServices() {
103
        $argumentMapperService  = new Service(
104
            'marshal.mapper.argument',
105
            ArgumentMapper::class
106
        );
107
        $serviceMapperService   = new Service(
108
            'marshal.mapper.service',
109
            ServiceMapper::class,
110
            $argumentMapperService
111
        );
112
        $containerMapperService = new Service(
113
            'marshal.mapper.container',
114
            ContainerMapper::class,
115
            $argumentMapperService,
116
            $serviceMapperService
117
        );
118
119
        return [$containerMapperService, $serviceMapperService, $argumentMapperService];
120
    }
121
}
122