1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Hateoas\Tests\Representation\Factory; |
4
|
|
|
|
5
|
|
|
use Hateoas\Configuration\Route; |
6
|
|
|
use Hateoas\Representation\CollectionRepresentation; |
7
|
|
|
use Hateoas\Representation\Factory\PagerfantaFactory; |
8
|
|
|
use Hateoas\Tests\Representation\RepresentationTestCase; |
9
|
|
|
use Pagerfanta\Adapter\ArrayAdapter; |
10
|
|
|
use Pagerfanta\Pagerfanta; |
11
|
|
|
|
12
|
|
|
class PagerfantaFactoryTest extends RepresentationTestCase |
13
|
|
|
{ |
14
|
|
|
public function test() |
15
|
|
|
{ |
16
|
|
|
$results = array( |
17
|
|
|
'Adrien', |
18
|
|
|
'William', |
19
|
|
|
); |
20
|
|
|
|
21
|
|
|
$pagerProphecy = $this->prophesize('Pagerfanta\Pagerfanta'); |
22
|
|
|
$pagerProphecy->getCurrentPageResults()->willReturn($results); |
23
|
|
|
$pagerProphecy->getCurrentPage()->willReturn(2); |
24
|
|
|
$pagerProphecy->getMaxPerPage()->willReturn(20); |
25
|
|
|
$pagerProphecy->getNbPages()->willReturn(4); |
26
|
|
|
$pagerProphecy->getNbResults()->willReturn(100); |
27
|
|
|
|
28
|
|
|
$factory = new PagerfantaFactory('p', 'l'); |
29
|
|
|
$representation1 = $factory->createRepresentation( |
30
|
|
|
$pagerProphecy->reveal(), |
31
|
|
|
new Route( |
32
|
|
|
'users', |
33
|
|
|
array( |
34
|
|
|
'query' => 'hateoas', |
35
|
|
|
) |
36
|
|
|
) |
37
|
|
|
); |
38
|
|
|
$representation2 = $factory->createRepresentation( |
39
|
|
|
$pagerProphecy->reveal(), |
40
|
|
|
new Route( |
41
|
|
|
'users', |
42
|
|
|
array( |
43
|
|
|
'query' => 'hateoas', |
44
|
|
|
) |
45
|
|
|
), |
46
|
|
|
array() |
47
|
|
|
); |
48
|
|
|
|
49
|
|
|
$this->assertEquals(new CollectionRepresentation($results), $representation1->getInline()); |
50
|
|
|
$this->assertSame([], $representation2->getInline()); |
51
|
|
|
|
52
|
|
|
foreach (array($representation1, $representation2) as $representation) { |
53
|
|
|
$this->assertInstanceOf('Hateoas\Representation\PaginatedRepresentation', $representation); |
54
|
|
|
$this->assertSame(2, $representation->getPage()); |
55
|
|
|
$this->assertSame(20, $representation->getLimit()); |
56
|
|
|
$this->assertSame(4, $representation->getPages()); |
57
|
|
|
$this->assertSame(100, $representation->getTotal()); |
58
|
|
|
$this->assertSame( |
59
|
|
|
[ |
60
|
|
|
'query' => 'hateoas', |
61
|
|
|
'p' => 2, |
62
|
|
|
'l' => 20, |
63
|
|
|
], |
64
|
|
|
$representation->getParameters() |
65
|
|
|
); |
66
|
|
|
$this->assertSame('p', $representation->getPageParameterName()); |
67
|
|
|
$this->assertSame('l', $representation->getLimitParameterName()); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function testSerialize() |
72
|
|
|
{ |
73
|
|
|
$factory = new PagerfantaFactory(); |
74
|
|
|
$pagerfanta = new Pagerfanta(new ArrayAdapter(array( |
75
|
|
|
'bim', |
76
|
|
|
'bam', |
77
|
|
|
'boom' |
78
|
|
|
))); |
79
|
|
|
|
80
|
|
|
$collection = $factory->createRepresentation($pagerfanta, new Route('my_route')); |
81
|
|
|
|
82
|
|
|
$this->assertSame( |
83
|
|
|
<<<XML |
84
|
|
|
<?xml version="1.0" encoding="UTF-8"?> |
85
|
|
|
<collection page="1" limit="10" pages="1" total="3"> |
86
|
|
|
<entry rel="items"> |
87
|
|
|
<entry><![CDATA[bim]]></entry> |
88
|
|
|
<entry><![CDATA[bam]]></entry> |
89
|
|
|
<entry><![CDATA[boom]]></entry> |
90
|
|
|
</entry> |
91
|
|
|
<link rel="self" href="my_route?page=1&limit=10"/> |
92
|
|
|
<link rel="first" href="my_route?page=1&limit=10"/> |
93
|
|
|
<link rel="last" href="my_route?page=1&limit=10"/> |
94
|
|
|
</collection> |
95
|
|
|
|
96
|
|
|
XML |
97
|
|
|
, |
98
|
|
|
$this->hateoas->serialize($collection, 'xml') |
99
|
|
|
); |
100
|
|
|
$this->assertSame( |
101
|
|
|
<<<JSON |
102
|
|
|
{ |
103
|
|
|
"page": 1, |
104
|
|
|
"limit": 10, |
105
|
|
|
"pages": 1, |
106
|
|
|
"total": 3, |
107
|
|
|
"_links": { |
108
|
|
|
"self": { |
109
|
|
|
"href": "my_route?page=1&limit=10" |
110
|
|
|
}, |
111
|
|
|
"first": { |
112
|
|
|
"href": "my_route?page=1&limit=10" |
113
|
|
|
}, |
114
|
|
|
"last": { |
115
|
|
|
"href": "my_route?page=1&limit=10" |
116
|
|
|
} |
117
|
|
|
}, |
118
|
|
|
"_embedded": { |
119
|
|
|
"items": [ |
120
|
|
|
"bim", |
121
|
|
|
"bam", |
122
|
|
|
"boom" |
123
|
|
|
] |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
JSON |
127
|
|
|
, |
128
|
|
|
$this->json($this->hateoas->serialize($collection, 'json')) |
129
|
|
|
); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
public function testGenerateAbsoluteURIs() |
133
|
|
|
{ |
134
|
|
|
$factory = new PagerfantaFactory(); |
135
|
|
|
$pagerfanta = new Pagerfanta(new ArrayAdapter(array( |
136
|
|
|
'bim', |
137
|
|
|
'bam', |
138
|
|
|
'boom' |
139
|
|
|
))); |
140
|
|
|
|
141
|
|
|
$collection = $factory->createRepresentation( |
142
|
|
|
$pagerfanta, |
143
|
|
|
new Route( |
144
|
|
|
'/my_route', |
145
|
|
|
array(), |
146
|
|
|
true |
147
|
|
|
) |
148
|
|
|
); |
149
|
|
|
|
150
|
|
|
$this->assertSame( |
151
|
|
|
<<<XML |
152
|
|
|
<?xml version="1.0" encoding="UTF-8"?> |
153
|
|
|
<collection page="1" limit="10" pages="1" total="3"> |
154
|
|
|
<entry rel="items"> |
155
|
|
|
<entry><![CDATA[bim]]></entry> |
156
|
|
|
<entry><![CDATA[bam]]></entry> |
157
|
|
|
<entry><![CDATA[boom]]></entry> |
158
|
|
|
</entry> |
159
|
|
|
<link rel="self" href="http://example.com/my_route?page=1&limit=10"/> |
160
|
|
|
<link rel="first" href="http://example.com/my_route?page=1&limit=10"/> |
161
|
|
|
<link rel="last" href="http://example.com/my_route?page=1&limit=10"/> |
162
|
|
|
</collection> |
163
|
|
|
|
164
|
|
|
XML |
165
|
|
|
, |
166
|
|
|
$this->hateoas->serialize($collection, 'xml') |
167
|
|
|
); |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|