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
|
|
|
private function createUser() { |
70
|
|
|
$user = new \stdClass(); |
71
|
|
|
$user->id = 123; |
72
|
|
|
$user->score = 3.4; |
73
|
|
|
$user->email = '[email protected]'; |
74
|
|
|
$user->nicknames = ['pfefferkuchenmann', 'lululu']; |
75
|
|
|
|
76
|
|
|
return $user; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
private function getServices() { |
80
|
|
|
$argumentMapperService = new Service( |
81
|
|
|
'marshal.mapper.argument', |
82
|
|
|
ArgumentMapper::class |
83
|
|
|
); |
84
|
|
|
$serviceMapperService = new Service( |
85
|
|
|
'marshal.mapper.service', |
86
|
|
|
ServiceMapper::class, |
87
|
|
|
$argumentMapperService |
88
|
|
|
); |
89
|
|
|
$containerMapperService = new Service( |
90
|
|
|
'marshal.mapper.container', |
91
|
|
|
ContainerMapper::class, |
92
|
|
|
$argumentMapperService, |
93
|
|
|
$serviceMapperService |
94
|
|
|
); |
95
|
|
|
|
96
|
|
|
return [$containerMapperService, $serviceMapperService, $argumentMapperService]; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
This check looks for function calls that miss required arguments.