1 | <?php |
||
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' => [MarshalXml::CDATA_KEY => $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 testSerializeRootNodeWithCDataSection() { |
||
119 | $xml = MarshalXml::serializeItemCallable(function() { |
||
120 | return [ |
||
121 | 'root' => [ |
||
122 | MarshalXml::CDATA_KEY => 'Hello World!', |
||
123 | ], |
||
124 | ]; |
||
125 | }); |
||
126 | |||
127 | $this->assertXmlStringEqualsXmlString( |
||
128 | '<?xml version="1.0" encoding="UTF-8"?><root><![CDATA[Hello World!]]></root>', |
||
129 | $xml |
||
130 | ); |
||
131 | } |
||
132 | |||
133 | public function testSettingProlog() { |
||
134 | MarshalXml::setVersion('1.1'); |
||
135 | MarshalXml::setEncoding('ISO-8859-15'); |
||
136 | |||
137 | $xml = MarshalXml::serializeItemCallable(function () { |
||
138 | return [ |
||
139 | 'root' => [ |
||
140 | 'currency' => '€', |
||
141 | ] |
||
142 | ]; |
||
143 | }); |
||
144 | |||
145 | $this->assertXmlStringEqualsXmlFile(__DIR__ . '/Fixtures/Currency.xml', $xml); |
||
146 | } |
||
147 | |||
148 | private function createUser() { |
||
149 | $user = new \stdClass(); |
||
150 | $user->id = 123; |
||
151 | $user->score = 3.0; |
||
152 | $user->email = '[email protected]'; |
||
153 | $user->nicknames = ['pfefferkuchenmann', 'lululu']; |
||
154 | |||
155 | return $user; |
||
156 | } |
||
157 | |||
158 | private function getServices() { |
||
159 | $argumentMapperService = new Service( |
||
160 | 'marshal.mapper.argument', |
||
161 | ArgumentMapper::class |
||
162 | ); |
||
163 | $serviceMapperService = new Service( |
||
164 | 'marshal.mapper.service', |
||
165 | ServiceMapper::class, |
||
166 | $argumentMapperService |
||
167 | ); |
||
168 | $containerMapperService = new Service( |
||
169 | 'marshal.mapper.container', |
||
170 | ContainerMapper::class, |
||
171 | $argumentMapperService, |
||
172 | $serviceMapperService |
||
173 | ); |
||
174 | |||
175 | return [$containerMapperService, $serviceMapperService, $argumentMapperService]; |
||
176 | } |
||
177 | } |
||
178 |