1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
namespace KingsonDe\Marshal; |
6
|
|
|
|
7
|
|
|
use KingsonDe\Marshal\Data\Collection; |
8
|
|
|
use KingsonDe\Marshal\Data\CollectionCallable; |
9
|
|
|
use KingsonDe\Marshal\Example\Mapper\ArgumentMapper; |
10
|
|
|
use KingsonDe\Marshal\Example\Mapper\ContainerMapper; |
11
|
|
|
use KingsonDe\Marshal\Example\Mapper\ServiceMapper; |
12
|
|
|
use KingsonDe\Marshal\Example\Model\Service; |
13
|
|
|
use PHPUnit\Framework\TestCase; |
14
|
|
|
|
15
|
|
|
class MarshalXmlTest extends TestCase { |
16
|
|
|
|
17
|
|
|
public function testSerializeXml() { |
18
|
|
|
$xml = MarshalXml::serializeItemCallable(function(\stdClass $user) { |
19
|
|
|
return [ |
20
|
|
|
'root' => [ |
21
|
|
|
MarshalXml::ATTRIBUTES_KEY => [ |
22
|
|
|
'year' => 2017, |
23
|
|
|
], |
24
|
|
|
'id' => $user->id, |
25
|
|
|
'score' => [ |
26
|
|
|
MarshalXml::ATTRIBUTES_KEY => [ |
27
|
|
|
'public' => true, |
28
|
|
|
'highscore' => 'yes', |
29
|
|
|
], |
30
|
|
|
MarshalXml::DATA_KEY => $user->score, |
31
|
|
|
], |
32
|
|
|
'email' => $user->email, |
33
|
|
|
'null' => null, |
34
|
|
|
'nicknames' => new CollectionCallable(function ($nickname) { |
35
|
|
|
return [ |
36
|
|
|
'nickname' => $nickname, |
37
|
|
|
]; |
38
|
|
|
}, $user->nicknames), |
39
|
|
|
], |
40
|
|
|
]; |
41
|
|
|
}, $this->createUser()); |
42
|
|
|
|
43
|
|
|
$this->assertXmlStringEqualsXmlFile(__DIR__ . '/Fixtures/User.xml', $xml); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function testSerializeRootNodeWithScalarValue() { |
47
|
|
|
$xml = MarshalXml::serializeItemCallable(function() { |
|
|
|
|
48
|
|
|
return [ |
49
|
|
|
'root' => [ |
50
|
|
|
MarshalXml::ATTRIBUTES_KEY => [ |
51
|
|
|
'id' => 123, |
52
|
|
|
], |
53
|
|
|
MarshalXml::DATA_KEY => 'Hello World!', |
54
|
|
|
], |
55
|
|
|
]; |
56
|
|
|
}); |
57
|
|
|
|
58
|
|
|
$this->assertXmlStringEqualsXmlString( |
59
|
|
|
'<?xml version="1.0" encoding="UTF-8"?><root id="123">Hello World!</root>', |
60
|
|
|
$xml |
61
|
|
|
); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function testXmlMapper() { |
65
|
|
|
$xml = MarshalXml::serializeItem(new ContainerMapper(), ...$this->getServices()); |
66
|
|
|
|
67
|
|
|
$this->assertXmlStringEqualsXmlFile(__DIR__ . '/Fixtures/Services.xml', $xml); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function testBuildDataStructureIsNull() { |
71
|
|
|
$xml = MarshalXml::serializeItemCallable(function () { |
|
|
|
|
72
|
|
|
return null; |
73
|
|
|
}); |
74
|
|
|
|
75
|
|
|
$this->assertXmlStringEqualsXmlString('<?xml version="1.0" encoding="UTF-8"?><root/>', $xml); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @expectedException \KingsonDe\Marshal\Exception\XmlSerializeException |
80
|
|
|
*/ |
81
|
|
|
public function testSerializationFailed() { |
82
|
|
|
MarshalXml::serializeItemCallable(function () { |
|
|
|
|
83
|
|
|
return [ |
84
|
|
|
'malformedXml' => [ |
85
|
|
|
'@node' => 'some value', |
86
|
|
|
], |
87
|
|
|
]; |
88
|
|
|
}); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @expectedException \KingsonDe\Marshal\Exception\XmlSerializeException |
93
|
|
|
*/ |
94
|
|
|
public function testSerializeWithCollection() { |
95
|
|
|
$collection = new Collection(new ArgumentMapper(), [new Service('marshal.mapper.dummy')]); |
96
|
|
|
|
97
|
|
|
MarshalXml::serialize($collection); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @expectedException \KingsonDe\Marshal\Exception\XmlSerializeException |
102
|
|
|
*/ |
103
|
|
|
public function testCollectionAtRootLevel() { |
104
|
|
|
MarshalXml::serializeCollection(new ArgumentMapper(), [new Service('marshal.mapper.dummy')]); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @expectedException \KingsonDe\Marshal\Exception\XmlSerializeException |
109
|
|
|
*/ |
110
|
|
|
public function testCollectionCallableAtRootLevel() { |
111
|
|
|
MarshalXml::serializeCollectionCallable(function (Service $service) { |
112
|
|
|
return [ |
113
|
|
|
'id' => $service->getId(), |
114
|
|
|
]; |
115
|
|
|
}, [new Service('marshal.mapper.dummy')]); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function testSettingProlog() { |
119
|
|
|
MarshalXml::setVersion('1.1'); |
120
|
|
|
MarshalXml::setEncoding('ISO-8859-15'); |
121
|
|
|
|
122
|
|
|
$xml = MarshalXml::serializeItemCallable(function () { |
|
|
|
|
123
|
|
|
return [ |
124
|
|
|
'root' => [ |
125
|
|
|
'currency' => '€', |
126
|
|
|
] |
127
|
|
|
]; |
128
|
|
|
}); |
129
|
|
|
|
130
|
|
|
$this->assertXmlStringEqualsXmlFile(__DIR__ . '/Fixtures/Currency.xml', $xml); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
private function createUser() { |
134
|
|
|
$user = new \stdClass(); |
135
|
|
|
$user->id = 123; |
136
|
|
|
$user->score = 3.0; |
137
|
|
|
$user->email = '[email protected]'; |
138
|
|
|
$user->nicknames = ['pfefferkuchenmann', 'lululu']; |
139
|
|
|
|
140
|
|
|
return $user; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
private function getServices() { |
144
|
|
|
$argumentMapperService = new Service( |
145
|
|
|
'marshal.mapper.argument', |
146
|
|
|
ArgumentMapper::class |
147
|
|
|
); |
148
|
|
|
$serviceMapperService = new Service( |
149
|
|
|
'marshal.mapper.service', |
150
|
|
|
ServiceMapper::class, |
151
|
|
|
$argumentMapperService |
152
|
|
|
); |
153
|
|
|
$containerMapperService = new Service( |
154
|
|
|
'marshal.mapper.container', |
155
|
|
|
ContainerMapper::class, |
156
|
|
|
$argumentMapperService, |
157
|
|
|
$serviceMapperService |
158
|
|
|
); |
159
|
|
|
|
160
|
|
|
return [$containerMapperService, $serviceMapperService, $argumentMapperService]; |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|
This check looks for function calls that miss required arguments.