1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Hateoas\Tests\Serializer; |
4
|
|
|
|
5
|
|
|
use Hateoas\HateoasBuilder; |
6
|
|
|
use Hateoas\Model\Embedded; |
7
|
|
|
use Hateoas\Model\Link; |
8
|
|
|
use Hateoas\Representation\CollectionRepresentation; |
9
|
|
|
use Hateoas\Serializer\Metadata\RelationPropertyMetadata; |
10
|
|
|
use Hateoas\Serializer\XmlSerializer; |
11
|
|
|
use Hateoas\Tests\Fixtures\AdrienBrault; |
12
|
|
|
use Hateoas\Tests\Fixtures\Gh236Foo; |
13
|
|
|
use Hateoas\Tests\TestCase; |
14
|
|
|
use JMS\Serializer\SerializationContext; |
15
|
|
|
use JMS\Serializer\XmlSerializationVisitor; |
16
|
|
|
|
17
|
|
|
class XmlSerializerTest extends TestCase |
18
|
|
|
{ |
19
|
|
View Code Duplication |
public function testSerializeLinks() |
|
|
|
|
20
|
|
|
{ |
21
|
|
|
$contextProphecy = $this->prophesize('JMS\Serializer\SerializationContext'); |
22
|
|
|
|
23
|
|
|
$xmlSerializer = new XmlSerializer(); |
24
|
|
|
$xmlSerializationVisitor = $this->createXmlSerializationVisitor(); |
25
|
|
|
|
26
|
|
|
$links = array( |
27
|
|
|
new Link('self', '/users/42'), |
28
|
|
|
new Link('foo', '/bar', array('type' => 'magic')), |
29
|
|
|
); |
30
|
|
|
|
31
|
|
|
$xmlSerializer->serializeLinks( |
32
|
|
|
$links, |
33
|
|
|
$xmlSerializationVisitor, |
34
|
|
|
$contextProphecy->reveal() |
35
|
|
|
); |
36
|
|
|
|
37
|
|
|
$this->assertSame( |
38
|
|
|
<<<XML |
39
|
|
|
<?xml version="1.0" encoding="UTF-8"?> |
40
|
|
|
<root> |
41
|
|
|
<link rel="self" href="/users/42"/> |
42
|
|
|
<link rel="foo" href="/bar" type="magic"/> |
43
|
|
|
</root> |
44
|
|
|
|
45
|
|
|
XML |
46
|
|
|
, |
47
|
|
|
$xmlSerializationVisitor->getResult() |
48
|
|
|
); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
View Code Duplication |
public function testSerializeEmbeddeds() |
|
|
|
|
52
|
|
|
{ |
53
|
|
|
$contextProphecy = $this->prophesize('JMS\Serializer\SerializationContext'); |
54
|
|
|
|
55
|
|
|
$embeddeds = array( |
56
|
|
|
new Embedded('friend', array('name' => 'John'), new RelationPropertyMetadata(), 'person'), |
57
|
|
|
); |
58
|
|
|
|
59
|
|
|
$xmlSerializationVisitor = $this->createXmlSerializationVisitor(); |
60
|
|
|
|
61
|
|
|
$xmlSerializer = new XmlSerializer(); |
62
|
|
|
$xmlSerializer->serializeEmbeddeds( |
63
|
|
|
$embeddeds, |
64
|
|
|
$xmlSerializationVisitor, |
65
|
|
|
$contextProphecy->reveal() |
66
|
|
|
); |
67
|
|
|
|
68
|
|
|
$this->assertSame( |
69
|
|
|
<<<XML |
70
|
|
|
<?xml version="1.0" encoding="UTF-8"?> |
71
|
|
|
<root> |
72
|
|
|
<person rel="friend"> |
73
|
|
|
<entry/> |
74
|
|
|
</person> |
75
|
|
|
</root> |
76
|
|
|
|
77
|
|
|
XML |
78
|
|
|
, |
79
|
|
|
$xmlSerializationVisitor->getResult() |
80
|
|
|
); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function testSerializeAdrienBrault() |
84
|
|
|
{ |
85
|
|
|
$hateoas = HateoasBuilder::buildHateoas(); |
86
|
|
|
$adrienBrault = new AdrienBrault(); |
87
|
|
|
|
88
|
|
|
$this->assertSame( |
89
|
|
|
<<<XML |
90
|
|
|
<?xml version="1.0" encoding="UTF-8"?> |
91
|
|
|
<result> |
92
|
|
|
<first_name><![CDATA[Adrien]]></first_name> |
93
|
|
|
<last_name><![CDATA[Brault]]></last_name> |
94
|
|
|
<link rel="self" href="http://adrienbrault.fr"/> |
95
|
|
|
<link rel="computer" href="http://www.apple.com/macbook-pro/"/> |
96
|
|
|
<link rel="dynamic-relation" href="awesome!!!"/> |
97
|
|
|
<computer rel="computer"> |
98
|
|
|
<name><![CDATA[MacBook Pro]]></name> |
99
|
|
|
</computer> |
100
|
|
|
<computer rel="broken-computer"> |
101
|
|
|
<name><![CDATA[Windows Computer]]></name> |
102
|
|
|
</computer> |
103
|
|
|
<smartphone rel="smartphone"> |
104
|
|
|
<name><![CDATA[iPhone 6]]></name> |
105
|
|
|
</smartphone> |
106
|
|
|
<smartphone rel="smartphone"> |
107
|
|
|
<name><![CDATA[Nexus 5]]></name> |
108
|
|
|
</smartphone> |
109
|
|
|
<entry rel="dynamic-relation"> |
110
|
|
|
<entry><![CDATA[wowowow]]></entry> |
111
|
|
|
</entry> |
112
|
|
|
</result> |
113
|
|
|
|
114
|
|
|
XML |
115
|
|
|
, |
116
|
|
|
$hateoas->serialize($adrienBrault, 'xml') |
117
|
|
|
); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
View Code Duplication |
public function testGh236() |
|
|
|
|
121
|
|
|
{ |
122
|
|
|
$data = new CollectionRepresentation([new Gh236Foo()]); |
123
|
|
|
|
124
|
|
|
$hateoas = HateoasBuilder::buildHateoas(); |
125
|
|
|
|
126
|
|
|
$this->assertSame( |
127
|
|
|
<<<XML |
128
|
|
|
<?xml version="1.0" encoding="UTF-8"?> |
129
|
|
|
<collection> |
130
|
|
|
<entry rel="items"> |
131
|
|
|
<entry> |
132
|
|
|
<a> |
133
|
|
|
<xxx><![CDATA[yyy]]></xxx> |
134
|
|
|
</a> |
135
|
|
|
<entry rel="b_embed"> |
136
|
|
|
<xxx><![CDATA[zzz]]></xxx> |
137
|
|
|
</entry> |
138
|
|
|
</entry> |
139
|
|
|
</entry> |
140
|
|
|
</collection> |
141
|
|
|
|
142
|
|
|
XML |
143
|
|
|
, |
144
|
|
|
$hateoas->serialize($data, 'xml', SerializationContext::create()->enableMaxDepthChecks()) |
145
|
|
|
); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
private function createXmlSerializationVisitor() |
149
|
|
|
{ |
150
|
|
|
$xmlSerializationVisitor = new XmlSerializationVisitor( |
151
|
|
|
$this->prophesize('JMS\Serializer\Naming\PropertyNamingStrategyInterface')->reveal() |
152
|
|
|
); |
153
|
|
|
$xmlSerializationVisitorClass = new \ReflectionClass('JMS\Serializer\XmlSerializationVisitor'); |
154
|
|
|
$stackProperty = $xmlSerializationVisitorClass->getProperty('stack'); |
155
|
|
|
$stackProperty->setAccessible('true'); |
156
|
|
|
$stackProperty->setValue($xmlSerializationVisitor, new \SplStack()); |
157
|
|
|
|
158
|
|
|
$xmlSerializationVisitor->document = $xmlSerializationVisitor->createDocument(null, null, false); |
159
|
|
|
$xmlRootNode = $xmlSerializationVisitor->document->createElement('root'); |
160
|
|
|
$xmlSerializationVisitor->document->appendChild($xmlRootNode); |
161
|
|
|
$xmlSerializationVisitor->setCurrentNode($xmlRootNode); |
162
|
|
|
|
163
|
|
|
return $xmlSerializationVisitor; |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.