1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Hateoas\Tests\Serializer; |
4
|
|
|
|
5
|
|
|
use Hateoas\HateoasBuilder; |
6
|
|
|
use Hateoas\Representation\CollectionRepresentation; |
7
|
|
|
use Hateoas\Serializer\XmlHalSerializer; |
8
|
|
|
use Hateoas\Tests\Fixtures\AdrienBrault; |
9
|
|
|
use Hateoas\Tests\Fixtures\Gh236Foo; |
10
|
|
|
use Hateoas\Tests\TestCase; |
11
|
|
|
use JMS\Serializer\SerializationContext; |
12
|
|
|
|
13
|
|
|
class XmlHalSerializerTest extends TestCase |
14
|
|
|
{ |
15
|
|
|
public function testSerializeAdrienBrault() |
16
|
|
|
{ |
17
|
|
|
$hateoas = HateoasBuilder::create() |
18
|
|
|
->setXmlSerializer(new XmlHalSerializer()) |
19
|
|
|
->build(); |
20
|
|
|
$adrienBrault = new AdrienBrault(); |
21
|
|
|
|
22
|
|
|
$this->assertSame( |
23
|
|
|
<<<XML |
24
|
|
|
<?xml version="1.0" encoding="UTF-8"?> |
25
|
|
|
<result href="http://adrienbrault.fr"> |
26
|
|
|
<first_name><![CDATA[Adrien]]></first_name> |
27
|
|
|
<last_name><![CDATA[Brault]]></last_name> |
28
|
|
|
<link rel="computer" href="http://www.apple.com/macbook-pro/"/> |
29
|
|
|
<link rel="dynamic-relation" href="awesome!!!"/> |
30
|
|
|
<resource rel="computer"> |
31
|
|
|
<name><![CDATA[MacBook Pro]]></name> |
32
|
|
|
</resource> |
33
|
|
|
<resource rel="broken-computer"> |
34
|
|
|
<name><![CDATA[Windows Computer]]></name> |
35
|
|
|
</resource> |
36
|
|
|
<resource rel="smartphone"> |
37
|
|
|
<name><![CDATA[iPhone 6]]></name> |
38
|
|
|
</resource> |
39
|
|
|
<resource rel="smartphone"> |
40
|
|
|
<name><![CDATA[Nexus 5]]></name> |
41
|
|
|
</resource> |
42
|
|
|
<resource rel="dynamic-relation"><![CDATA[wowowow]]></resource> |
43
|
|
|
</result> |
44
|
|
|
|
45
|
|
|
XML |
46
|
|
|
, |
47
|
|
|
$hateoas->serialize($adrienBrault, 'xml') |
48
|
|
|
); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
View Code Duplication |
public function testGh236() |
|
|
|
|
52
|
|
|
{ |
53
|
|
|
$data = new CollectionRepresentation([new Gh236Foo()]); |
54
|
|
|
|
55
|
|
|
$hateoas = HateoasBuilder::create() |
56
|
|
|
->setXmlSerializer(new XmlHalSerializer()) |
57
|
|
|
->build(); |
58
|
|
|
|
59
|
|
|
$this->assertSame( |
60
|
|
|
<<<XML |
61
|
|
|
<?xml version="1.0" encoding="UTF-8"?> |
62
|
|
|
<collection> |
63
|
|
|
<resource rel="items"> |
64
|
|
|
<a> |
65
|
|
|
<xxx><![CDATA[yyy]]></xxx> |
66
|
|
|
</a> |
67
|
|
|
<resource rel="b_embed"> |
68
|
|
|
<xxx><![CDATA[zzz]]></xxx> |
69
|
|
|
</resource> |
70
|
|
|
</resource> |
71
|
|
|
</collection> |
72
|
|
|
|
73
|
|
|
XML |
74
|
|
|
, |
75
|
|
|
$hateoas->serialize($data, 'xml', SerializationContext::create()->enableMaxDepthChecks()) |
76
|
|
|
); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
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.