1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Hateoas\Tests\Representation; |
4
|
|
|
|
5
|
|
|
use Hateoas\Representation\CollectionRepresentation; |
6
|
|
|
use Hateoas\Representation\RouteAwareRepresentation; |
7
|
|
|
|
8
|
|
|
class RouteAwareRepresentationTest extends RepresentationTestCase |
9
|
|
|
{ |
10
|
|
View Code Duplication |
public function testSerialize() |
|
|
|
|
11
|
|
|
{ |
12
|
|
|
$collection = new RouteAwareRepresentation( |
13
|
|
|
new CollectionRepresentation( |
14
|
|
|
array( |
15
|
|
|
'Adrien', |
16
|
|
|
'William', |
17
|
|
|
), |
18
|
|
|
'authors', |
19
|
|
|
'users' |
20
|
|
|
), |
21
|
|
|
'/authors', |
22
|
|
|
array( |
23
|
|
|
'query' => 'willdurand/Hateoas', |
24
|
|
|
) |
25
|
|
|
); |
26
|
|
|
|
27
|
|
|
$this->assertSame( |
28
|
|
|
<<<XML |
29
|
|
|
<?xml version="1.0" encoding="UTF-8"?> |
30
|
|
|
<result> |
31
|
|
|
<users rel="authors"> |
32
|
|
|
<entry><![CDATA[Adrien]]></entry> |
33
|
|
|
<entry><![CDATA[William]]></entry> |
34
|
|
|
</users> |
35
|
|
|
<link rel="self" href="/authors?query=willdurand%2FHateoas"/> |
36
|
|
|
</result> |
37
|
|
|
|
38
|
|
|
XML |
39
|
|
|
, |
40
|
|
|
$this->hateoas->serialize($collection, 'xml') |
41
|
|
|
); |
42
|
|
|
$this->assertSame( |
43
|
|
|
<<<XML |
44
|
|
|
<?xml version="1.0" encoding="UTF-8"?> |
45
|
|
|
<result href="/authors?query=willdurand%2FHateoas"> |
46
|
|
|
<resource rel="authors"><![CDATA[Adrien]]></resource> |
47
|
|
|
<resource rel="authors"><![CDATA[William]]></resource> |
48
|
|
|
</result> |
49
|
|
|
|
50
|
|
|
XML |
51
|
|
|
, |
52
|
|
|
$this->halHateoas->serialize($collection, 'xml') |
53
|
|
|
); |
54
|
|
|
|
55
|
|
|
$this->assertSame( |
56
|
|
|
<<<JSON |
57
|
|
|
{ |
58
|
|
|
"_links": { |
59
|
|
|
"self": { |
60
|
|
|
"href": "\/authors?query=willdurand%2FHateoas" |
61
|
|
|
} |
62
|
|
|
}, |
63
|
|
|
"_embedded": { |
64
|
|
|
"authors": [ |
65
|
|
|
"Adrien", |
66
|
|
|
"William" |
67
|
|
|
] |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
JSON |
71
|
|
|
, |
72
|
|
|
$this->json($this->halHateoas->serialize($collection, 'json')) |
73
|
|
|
); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
View Code Duplication |
public function testGenerateAbsoluteURIs() |
|
|
|
|
77
|
|
|
{ |
78
|
|
|
$collection = new RouteAwareRepresentation( |
79
|
|
|
new CollectionRepresentation( |
80
|
|
|
array( |
81
|
|
|
'Adrien', |
82
|
|
|
'William', |
83
|
|
|
), |
84
|
|
|
'authors', |
85
|
|
|
'users' |
86
|
|
|
), |
87
|
|
|
'/authors', |
88
|
|
|
array( |
89
|
|
|
'query' => 'willdurand/Hateoas', |
90
|
|
|
), |
91
|
|
|
true // absolute |
92
|
|
|
); |
93
|
|
|
|
94
|
|
|
$this->assertSame( |
95
|
|
|
<<<XML |
96
|
|
|
<?xml version="1.0" encoding="UTF-8"?> |
97
|
|
|
<result> |
98
|
|
|
<users rel="authors"> |
99
|
|
|
<entry><![CDATA[Adrien]]></entry> |
100
|
|
|
<entry><![CDATA[William]]></entry> |
101
|
|
|
</users> |
102
|
|
|
<link rel="self" href="http://example.com/authors?query=willdurand%2FHateoas"/> |
103
|
|
|
</result> |
104
|
|
|
|
105
|
|
|
XML |
106
|
|
|
, |
107
|
|
|
$this->hateoas->serialize($collection, 'xml') |
108
|
|
|
); |
109
|
|
|
$this->assertSame( |
110
|
|
|
<<<XML |
111
|
|
|
<?xml version="1.0" encoding="UTF-8"?> |
112
|
|
|
<result href="http://example.com/authors?query=willdurand%2FHateoas"> |
113
|
|
|
<resource rel="authors"><![CDATA[Adrien]]></resource> |
114
|
|
|
<resource rel="authors"><![CDATA[William]]></resource> |
115
|
|
|
</result> |
116
|
|
|
|
117
|
|
|
XML |
118
|
|
|
, |
119
|
|
|
$this->halHateoas->serialize($collection, 'xml') |
120
|
|
|
); |
121
|
|
|
|
122
|
|
|
$this->assertSame( |
123
|
|
|
<<<JSON |
124
|
|
|
{ |
125
|
|
|
"_links": { |
126
|
|
|
"self": { |
127
|
|
|
"href": "http:\/\/example.com\/authors?query=willdurand%2FHateoas" |
128
|
|
|
} |
129
|
|
|
}, |
130
|
|
|
"_embedded": { |
131
|
|
|
"authors": [ |
132
|
|
|
"Adrien", |
133
|
|
|
"William" |
134
|
|
|
] |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
JSON |
138
|
|
|
, |
139
|
|
|
$this->json($this->halHateoas->serialize($collection, 'json')) |
140
|
|
|
); |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
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.