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() { |
|
|
|
|
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 () { |
|
|
|
|
71
|
|
|
return null; |
72
|
|
|
}); |
73
|
|
|
|
74
|
|
|
$this->assertXmlStringEqualsXmlString('<?xml version="1.0" encoding="UTF-8"?><root/>', $xml); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @expectedException \KingsonDe\Marshal\Exception\XmlSerializeException |
79
|
|
|
*/ |
80
|
|
|
public function testSerializationFailed() { |
81
|
|
|
MarshalXml::serializeItemCallable(function () { |
|
|
|
|
82
|
|
|
return [ |
83
|
|
|
'malformedXml' => [ |
84
|
|
|
'@node' => 'some value', |
85
|
|
|
], |
86
|
|
|
]; |
87
|
|
|
}); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function testSettingProlog() { |
91
|
|
|
MarshalXml::setVersion('1.1'); |
92
|
|
|
MarshalXml::setEncoding('ISO-8859-15'); |
93
|
|
|
|
94
|
|
|
$xml = MarshalXml::serializeItemCallable(function () { |
|
|
|
|
95
|
|
|
return [ |
96
|
|
|
'root' => [ |
97
|
|
|
'currency' => '€', |
98
|
|
|
] |
99
|
|
|
]; |
100
|
|
|
}); |
101
|
|
|
|
102
|
|
|
$this->assertXmlStringEqualsXmlFile(__DIR__ . '/Fixtures/Currency.xml', $xml); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
private function createUser() { |
106
|
|
|
$user = new \stdClass(); |
107
|
|
|
$user->id = 123; |
108
|
|
|
$user->score = 3.0; |
109
|
|
|
$user->email = '[email protected]'; |
110
|
|
|
$user->nicknames = ['pfefferkuchenmann', 'lululu']; |
111
|
|
|
|
112
|
|
|
return $user; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
private function getServices() { |
116
|
|
|
$argumentMapperService = new Service( |
117
|
|
|
'marshal.mapper.argument', |
118
|
|
|
ArgumentMapper::class |
119
|
|
|
); |
120
|
|
|
$serviceMapperService = new Service( |
121
|
|
|
'marshal.mapper.service', |
122
|
|
|
ServiceMapper::class, |
123
|
|
|
$argumentMapperService |
124
|
|
|
); |
125
|
|
|
$containerMapperService = new Service( |
126
|
|
|
'marshal.mapper.container', |
127
|
|
|
ContainerMapper::class, |
128
|
|
|
$argumentMapperService, |
129
|
|
|
$serviceMapperService |
130
|
|
|
); |
131
|
|
|
|
132
|
|
|
return [$containerMapperService, $serviceMapperService, $argumentMapperService]; |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
This check looks for function calls that miss required arguments.