These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace Hateoas\Tests\Representation; |
||
4 | |||
5 | use Hateoas\Representation\CollectionRepresentation; |
||
6 | use Hateoas\Representation\OffsetRepresentation; |
||
7 | use Hateoas\Tests\Fixtures\UsersRepresentation; |
||
8 | |||
9 | class OffsetRepresentationTest extends RepresentationTestCase |
||
10 | { |
||
11 | View Code Duplication | public function testSerialize() |
|
0 ignored issues
–
show
|
|||
12 | { |
||
13 | $collection = new OffsetRepresentation( |
||
14 | new CollectionRepresentation( |
||
15 | array( |
||
16 | 'Adrien', |
||
17 | 'William', |
||
18 | ), |
||
19 | 'authors', |
||
20 | 'users' |
||
21 | ), |
||
22 | '/authors', |
||
23 | array( |
||
24 | 'query' => 'willdurand/Hateoas', |
||
25 | ), |
||
26 | 44, |
||
27 | 20, |
||
28 | 95, |
||
29 | null, |
||
30 | null, |
||
31 | false |
||
32 | ); |
||
33 | |||
34 | $this |
||
35 | ->string($this->hateoas->serialize($collection, 'xml')) |
||
36 | ->isEqualTo( |
||
37 | <<<XML |
||
38 | <?xml version="1.0" encoding="UTF-8"?> |
||
39 | <collection offset="44" limit="20" total="95"> |
||
40 | <users rel="authors"> |
||
41 | <entry><![CDATA[Adrien]]></entry> |
||
42 | <entry><![CDATA[William]]></entry> |
||
43 | </users> |
||
44 | <link rel="self" href="/authors?query=willdurand%2FHateoas&offset=44&limit=20"/> |
||
45 | <link rel="first" href="/authors?query=willdurand%2FHateoas&limit=20"/> |
||
46 | <link rel="last" href="/authors?query=willdurand%2FHateoas&offset=80&limit=20"/> |
||
47 | <link rel="next" href="/authors?query=willdurand%2FHateoas&offset=64&limit=20"/> |
||
48 | <link rel="previous" href="/authors?query=willdurand%2FHateoas&offset=24&limit=20"/> |
||
49 | </collection> |
||
50 | |||
51 | XML |
||
52 | ) |
||
53 | ->string($this->halHateoas->serialize($collection, 'xml')) |
||
54 | ->isEqualTo( |
||
55 | <<<XML |
||
56 | <?xml version="1.0" encoding="UTF-8"?> |
||
57 | <collection offset="44" limit="20" total="95" href="/authors?query=willdurand%2FHateoas&offset=44&limit=20"> |
||
58 | <resource rel="authors"><![CDATA[Adrien]]></resource> |
||
59 | <resource rel="authors"><![CDATA[William]]></resource> |
||
60 | <link rel="first" href="/authors?query=willdurand%2FHateoas&limit=20"/> |
||
61 | <link rel="last" href="/authors?query=willdurand%2FHateoas&offset=80&limit=20"/> |
||
62 | <link rel="next" href="/authors?query=willdurand%2FHateoas&offset=64&limit=20"/> |
||
63 | <link rel="previous" href="/authors?query=willdurand%2FHateoas&offset=24&limit=20"/> |
||
64 | </collection> |
||
65 | |||
66 | XML |
||
67 | ) |
||
68 | ->string($this->hateoas->serialize(new UsersRepresentation($collection), 'xml')) |
||
69 | ->isEqualTo( |
||
70 | <<<XML |
||
71 | <?xml version="1.0" encoding="UTF-8"?> |
||
72 | <users offset="44" limit="20" total="95"> |
||
73 | <users rel="authors"> |
||
74 | <entry><![CDATA[Adrien]]></entry> |
||
75 | <entry><![CDATA[William]]></entry> |
||
76 | </users> |
||
77 | <link rel="self" href="/authors?query=willdurand%2FHateoas&offset=44&limit=20"/> |
||
78 | <link rel="first" href="/authors?query=willdurand%2FHateoas&limit=20"/> |
||
79 | <link rel="last" href="/authors?query=willdurand%2FHateoas&offset=80&limit=20"/> |
||
80 | <link rel="next" href="/authors?query=willdurand%2FHateoas&offset=64&limit=20"/> |
||
81 | <link rel="previous" href="/authors?query=willdurand%2FHateoas&offset=24&limit=20"/> |
||
82 | </users> |
||
83 | |||
84 | XML |
||
85 | ) |
||
86 | ->string($this->halHateoas->serialize(new UsersRepresentation($collection), 'xml')) |
||
87 | ->isEqualTo( |
||
88 | <<<XML |
||
89 | <?xml version="1.0" encoding="UTF-8"?> |
||
90 | <users offset="44" limit="20" total="95" href="/authors?query=willdurand%2FHateoas&offset=44&limit=20"> |
||
91 | <resource rel="authors"><![CDATA[Adrien]]></resource> |
||
92 | <resource rel="authors"><![CDATA[William]]></resource> |
||
93 | <link rel="first" href="/authors?query=willdurand%2FHateoas&limit=20"/> |
||
94 | <link rel="last" href="/authors?query=willdurand%2FHateoas&offset=80&limit=20"/> |
||
95 | <link rel="next" href="/authors?query=willdurand%2FHateoas&offset=64&limit=20"/> |
||
96 | <link rel="previous" href="/authors?query=willdurand%2FHateoas&offset=24&limit=20"/> |
||
97 | </users> |
||
98 | |||
99 | XML |
||
100 | ) |
||
101 | ->string($this->halHateoas->serialize($collection, 'json')) |
||
102 | ->isEqualTo( |
||
103 | '{' |
||
104 | .'"offset":44,' |
||
105 | .'"limit":20,' |
||
106 | .'"total":95,' |
||
107 | .'"_links":{' |
||
108 | .'"self":{' |
||
109 | .'"href":"\/authors?query=willdurand%2FHateoas&offset=44&limit=20"' |
||
110 | .'},' |
||
111 | .'"first":{' |
||
112 | .'"href":"\/authors?query=willdurand%2FHateoas&limit=20"' |
||
113 | .'},' |
||
114 | .'"last":{' |
||
115 | .'"href":"\/authors?query=willdurand%2FHateoas&offset=80&limit=20"' |
||
116 | .'},' |
||
117 | .'"next":{' |
||
118 | .'"href":"\/authors?query=willdurand%2FHateoas&offset=64&limit=20"' |
||
119 | .'},' |
||
120 | .'"previous":{' |
||
121 | .'"href":"\/authors?query=willdurand%2FHateoas&offset=24&limit=20"' |
||
122 | .'}' |
||
123 | .'},' |
||
124 | .'"_embedded":{' |
||
125 | .'"authors":[' |
||
126 | .'"Adrien",' |
||
127 | .'"William"' |
||
128 | .']' |
||
129 | .'}' |
||
130 | .'}' |
||
131 | ) |
||
132 | ; |
||
133 | } |
||
134 | |||
135 | View Code Duplication | public function testGenerateAbsoluteURIs() |
|
136 | { |
||
137 | $collection = new OffsetRepresentation( |
||
138 | new CollectionRepresentation( |
||
139 | array( |
||
140 | 'Adrien', |
||
141 | 'William', |
||
142 | ), |
||
143 | 'authors', |
||
144 | 'users' |
||
145 | ), |
||
146 | '/authors', |
||
147 | array( |
||
148 | 'query' => 'willdurand/Hateoas', |
||
149 | ), |
||
150 | 44, |
||
151 | 20, |
||
152 | 95, |
||
153 | null, |
||
154 | null, |
||
155 | true // force absolute URIs |
||
156 | ); |
||
157 | |||
158 | $this |
||
159 | ->string($this->hateoas->serialize($collection, 'xml')) |
||
160 | ->isEqualTo( |
||
161 | <<<XML |
||
162 | <?xml version="1.0" encoding="UTF-8"?> |
||
163 | <collection offset="44" limit="20" total="95"> |
||
164 | <users rel="authors"> |
||
165 | <entry><![CDATA[Adrien]]></entry> |
||
166 | <entry><![CDATA[William]]></entry> |
||
167 | </users> |
||
168 | <link rel="self" href="http://example.com/authors?query=willdurand%2FHateoas&offset=44&limit=20"/> |
||
169 | <link rel="first" href="http://example.com/authors?query=willdurand%2FHateoas&limit=20"/> |
||
170 | <link rel="last" href="http://example.com/authors?query=willdurand%2FHateoas&offset=80&limit=20"/> |
||
171 | <link rel="next" href="http://example.com/authors?query=willdurand%2FHateoas&offset=64&limit=20"/> |
||
172 | <link rel="previous" href="http://example.com/authors?query=willdurand%2FHateoas&offset=24&limit=20"/> |
||
173 | </collection> |
||
174 | |||
175 | XML |
||
176 | ) |
||
177 | ->string($this->halHateoas->serialize($collection, 'xml')) |
||
178 | ->isEqualTo( |
||
179 | <<<XML |
||
180 | <?xml version="1.0" encoding="UTF-8"?> |
||
181 | <collection offset="44" limit="20" total="95" href="http://example.com/authors?query=willdurand%2FHateoas&offset=44&limit=20"> |
||
182 | <resource rel="authors"><![CDATA[Adrien]]></resource> |
||
183 | <resource rel="authors"><![CDATA[William]]></resource> |
||
184 | <link rel="first" href="http://example.com/authors?query=willdurand%2FHateoas&limit=20"/> |
||
185 | <link rel="last" href="http://example.com/authors?query=willdurand%2FHateoas&offset=80&limit=20"/> |
||
186 | <link rel="next" href="http://example.com/authors?query=willdurand%2FHateoas&offset=64&limit=20"/> |
||
187 | <link rel="previous" href="http://example.com/authors?query=willdurand%2FHateoas&offset=24&limit=20"/> |
||
188 | </collection> |
||
189 | |||
190 | XML |
||
191 | ) |
||
192 | ->string($this->halHateoas->serialize($collection, 'json')) |
||
193 | ->isEqualTo( |
||
194 | '{' |
||
195 | .'"offset":44,' |
||
196 | .'"limit":20,' |
||
197 | .'"total":95,' |
||
198 | .'"_links":{' |
||
199 | .'"self":{' |
||
200 | .'"href":"http:\/\/example.com\/authors?query=willdurand%2FHateoas&offset=44&limit=20"' |
||
201 | .'},' |
||
202 | .'"first":{' |
||
203 | .'"href":"http:\/\/example.com\/authors?query=willdurand%2FHateoas&limit=20"' |
||
204 | .'},' |
||
205 | .'"last":{' |
||
206 | .'"href":"http:\/\/example.com\/authors?query=willdurand%2FHateoas&offset=80&limit=20"' |
||
207 | .'},' |
||
208 | .'"next":{' |
||
209 | .'"href":"http:\/\/example.com\/authors?query=willdurand%2FHateoas&offset=64&limit=20"' |
||
210 | .'},' |
||
211 | .'"previous":{' |
||
212 | .'"href":"http:\/\/example.com\/authors?query=willdurand%2FHateoas&offset=24&limit=20"' |
||
213 | .'}' |
||
214 | .'},' |
||
215 | .'"_embedded":{' |
||
216 | .'"authors":[' |
||
217 | .'"Adrien",' |
||
218 | .'"William"' |
||
219 | .']' |
||
220 | .'}' |
||
221 | .'}' |
||
222 | ) |
||
223 | ; |
||
224 | } |
||
225 | |||
226 | public function testExclusion() |
||
227 | { |
||
228 | $inline = new CollectionRepresentation( |
||
229 | array( |
||
230 | 'Adrien', |
||
231 | 'William', |
||
232 | ), |
||
233 | 'authors', |
||
234 | 'users' |
||
235 | ); |
||
236 | |||
237 | /* |
||
238 | * no last entry since `total` is missing |
||
239 | * no previous only when offset is 0/null |
||
240 | */ |
||
241 | $collection = new OffsetRepresentation( |
||
242 | $inline, |
||
243 | '/authors', |
||
244 | array( |
||
245 | 'query' => 'willdurand/Hateoas', |
||
246 | ), |
||
247 | null, |
||
248 | 20 |
||
249 | ); |
||
250 | |||
251 | $this |
||
252 | ->string($this->hateoas->serialize($collection, 'xml')) |
||
253 | ->isEqualTo( |
||
254 | <<<XML |
||
255 | <?xml version="1.0" encoding="UTF-8"?> |
||
256 | <collection limit="20"> |
||
257 | <users rel="authors"> |
||
258 | <entry><![CDATA[Adrien]]></entry> |
||
259 | <entry><![CDATA[William]]></entry> |
||
260 | </users> |
||
261 | <link rel="self" href="/authors?query=willdurand%2FHateoas&limit=20"/> |
||
262 | <link rel="first" href="/authors?query=willdurand%2FHateoas&limit=20"/> |
||
263 | <link rel="next" href="/authors?query=willdurand%2FHateoas&offset=20&limit=20"/> |
||
264 | </collection> |
||
265 | |||
266 | XML |
||
267 | ) |
||
268 | ; |
||
269 | |||
270 | /* |
||
271 | * no next since on last block |
||
272 | */ |
||
273 | $collection = new OffsetRepresentation( |
||
274 | $inline, |
||
275 | '/authors', |
||
276 | array( |
||
277 | 'query' => 'willdurand/Hateoas', |
||
278 | ), |
||
279 | 80, |
||
280 | 20, |
||
281 | 100 |
||
282 | ); |
||
283 | |||
284 | $this |
||
285 | ->string($this->hateoas->serialize($collection, 'xml')) |
||
286 | ->isEqualTo( |
||
287 | <<<XML |
||
288 | <?xml version="1.0" encoding="UTF-8"?> |
||
289 | <collection offset="80" limit="20" total="100"> |
||
290 | <users rel="authors"> |
||
291 | <entry><![CDATA[Adrien]]></entry> |
||
292 | <entry><![CDATA[William]]></entry> |
||
293 | </users> |
||
294 | <link rel="self" href="/authors?query=willdurand%2FHateoas&offset=80&limit=20"/> |
||
295 | <link rel="first" href="/authors?query=willdurand%2FHateoas&limit=20"/> |
||
296 | <link rel="last" href="/authors?query=willdurand%2FHateoas&offset=80&limit=20"/> |
||
297 | <link rel="previous" href="/authors?query=willdurand%2FHateoas&offset=60&limit=20"/> |
||
298 | </collection> |
||
299 | |||
300 | XML |
||
301 | ) |
||
302 | ; |
||
303 | } |
||
304 | } |
||
305 |
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.