1
|
|
|
<?php namespace League\Fractal\Test; |
2
|
|
|
|
3
|
|
|
use League\Fractal\Manager; |
4
|
|
|
use League\Fractal\ParamBag; |
5
|
|
|
use League\Fractal\Resource\Collection; |
6
|
|
|
use League\Fractal\Resource\Item; |
7
|
|
|
use Mockery; |
8
|
|
|
use PHPUnit\Framework\TestCase; |
9
|
|
|
|
10
|
|
|
class ManagerTest extends TestCase |
11
|
|
|
{ |
12
|
|
|
public function testParseIncludeSelfie() |
13
|
|
|
{ |
14
|
|
|
$manager = new Manager(); |
15
|
|
|
|
16
|
|
|
// Test that some includes provided returns self |
17
|
|
|
$this->assertInstanceOf(get_class($manager), $manager->parseIncludes(['foo'])); |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @expectedException \InvalidArgumentException |
22
|
|
|
* @expectedExceptionMessage The parseIncludes() method expects a string or an array. NULL given |
23
|
|
|
*/ |
24
|
|
|
public function testInvalidParseInclude() |
25
|
|
|
{ |
26
|
|
|
$manager = new Manager(); |
27
|
|
|
|
28
|
|
|
$manager->parseIncludes(null); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @expectedException \InvalidArgumentException |
33
|
|
|
* @expectedExceptionMessage The parseIncludes() method expects a string or an array. integer given |
34
|
|
|
*/ |
35
|
|
|
public function testIceTParseInclude() |
36
|
|
|
{ |
37
|
|
|
$manager = new Manager(); |
38
|
|
|
|
39
|
|
|
$manager->parseIncludes(99); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function testParseIncludes() |
43
|
|
|
{ |
44
|
|
|
$manager = new Manager(); |
45
|
|
|
|
46
|
|
|
// Does a CSV string work |
47
|
|
|
$manager->parseIncludes('foo,bar'); |
48
|
|
|
|
49
|
|
|
$this->assertSame(['foo', 'bar'], $manager->getRequestedIncludes()); |
50
|
|
|
|
51
|
|
|
// Does a big array of stuff work |
52
|
|
|
$manager->parseIncludes(['foo', 'bar', 'bar.baz']); |
53
|
|
|
$this->assertSame(['foo', 'bar', 'bar.baz'], $manager->getRequestedIncludes()); |
54
|
|
|
|
55
|
|
|
// Are repeated things stripped |
56
|
|
|
$manager->parseIncludes(['foo', 'foo', 'bar']); |
57
|
|
|
$this->assertSame(['foo', 'bar'], $manager->getRequestedIncludes()); |
58
|
|
|
|
59
|
|
|
$manager->parseIncludes(['foo.bar', 'foo:limit(10|1).bar']); |
60
|
|
|
$this->assertSame(['foo', 'foo.bar'], $manager->getRequestedIncludes()); |
61
|
|
|
$this->assertSame(['10', '1'], $manager->getIncludeParams('foo')->get('limit')); |
62
|
|
|
|
63
|
|
|
// Do requests for `baz.bart` also request `baz`? |
64
|
|
|
$manager->parseIncludes(['foo.bar']); |
65
|
|
|
$this->assertSame(['foo', 'foo.bar'], $manager->getRequestedIncludes()); |
66
|
|
|
|
67
|
|
|
|
68
|
|
|
// See if fancy syntax works |
69
|
|
|
$manager->parseIncludes('foo:limit(5|1):order(-something):anotherparam'); |
70
|
|
|
|
71
|
|
|
$params = $manager->getIncludeParams('foo'); |
72
|
|
|
|
73
|
|
|
$this->assertInstanceOf('League\Fractal\ParamBag', $params); |
74
|
|
|
|
75
|
|
|
|
76
|
|
|
$this->assertSame(['5', '1'], $params['limit']); |
77
|
|
|
$this->assertSame(['-something'], $params['order']); |
78
|
|
|
$this->assertSame([''], $params['anotherparam']); |
79
|
|
|
|
80
|
|
|
$this->assertNull($params['totallymadeup']); |
81
|
|
|
|
82
|
|
|
// Relation with params and sub relation |
83
|
|
|
$manager->parseIncludes('foo:limit(5|1):order(name).bar,baz'); |
84
|
|
|
|
85
|
|
|
$params = $manager->getIncludeParams('foo'); |
86
|
|
|
|
87
|
|
|
$this->assertInstanceOf('League\Fractal\ParamBag', $params); |
88
|
|
|
|
89
|
|
|
$this->assertSame(['5', '1'], $params['limit']); |
90
|
|
|
$this->assertSame(['name'], $params['order']); |
91
|
|
|
$this->assertSame(['foo', 'foo.bar', 'baz'], $manager->getRequestedIncludes()); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function testParseExcludeSelfie() |
95
|
|
|
{ |
96
|
|
|
$manager = new Manager(); |
97
|
|
|
|
98
|
|
|
// Test that some excludes provided returns self |
99
|
|
|
$this->assertInstanceOf(get_class($manager), $manager->parseExcludes(['foo'])); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @expectedException \InvalidArgumentException |
104
|
|
|
* @expectedExceptionMessage The parseExcludes() method expects a string or an array. NULL given |
105
|
|
|
*/ |
106
|
|
|
public function testInvalidParseExclude() |
107
|
|
|
{ |
108
|
|
|
$manager = new Manager(); |
109
|
|
|
|
110
|
|
|
$manager->parseExcludes(null); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @expectedException \InvalidArgumentException |
115
|
|
|
* @expectedExceptionMessage The parseExcludes() method expects a string or an array. integer given |
116
|
|
|
*/ |
117
|
|
|
public function testIceTParseExclude() |
118
|
|
|
{ |
119
|
|
|
$manager = new Manager(); |
120
|
|
|
|
121
|
|
|
$manager->parseExcludes(99); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
public function testParseExcludes() |
125
|
|
|
{ |
126
|
|
|
$manager = new Manager(); |
127
|
|
|
|
128
|
|
|
// Does a CSV string work |
129
|
|
|
$manager->parseExcludes('foo,bar'); |
130
|
|
|
|
131
|
|
|
$this->assertSame(['foo', 'bar'], $manager->getRequestedExcludes()); |
132
|
|
|
|
133
|
|
|
// Does a big array of stuff work |
134
|
|
|
$manager->parseExcludes(['foo', 'bar', 'bar.baz']); |
135
|
|
|
$this->assertSame(['foo', 'bar', 'bar.baz'], $manager->getRequestedExcludes()); |
136
|
|
|
|
137
|
|
|
// Are repeated things stripped |
138
|
|
|
$manager->parseExcludes(['foo', 'foo', 'bar']); |
139
|
|
|
$this->assertSame(['foo', 'bar'], $manager->getRequestedExcludes()); |
140
|
|
|
|
141
|
|
|
// Do requests for `baz.bart` also request `baz`? |
142
|
|
|
$manager->parseExcludes(['foo.bar']); |
143
|
|
|
$this->assertSame(['foo.bar'], $manager->getRequestedExcludes()); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
public function testRecursionLimiting() |
147
|
|
|
{ |
148
|
|
|
$manager = new Manager(); |
149
|
|
|
|
150
|
|
|
// Should limit to 10 by default |
151
|
|
|
$manager->parseIncludes('a.b.c.d.e.f.g.h.i.j.NEVER'); |
152
|
|
|
|
153
|
|
|
$this->assertSame( |
154
|
|
|
[ |
155
|
|
|
'a', |
156
|
|
|
'a.b', |
157
|
|
|
'a.b.c', |
158
|
|
|
'a.b.c.d', |
159
|
|
|
'a.b.c.d.e', |
160
|
|
|
'a.b.c.d.e.f', |
161
|
|
|
'a.b.c.d.e.f.g', |
162
|
|
|
'a.b.c.d.e.f.g.h', |
163
|
|
|
'a.b.c.d.e.f.g.h.i', |
164
|
|
|
'a.b.c.d.e.f.g.h.i.j', |
165
|
|
|
], |
166
|
|
|
$manager->getRequestedIncludes() |
167
|
|
|
); |
168
|
|
|
|
169
|
|
|
$manager->parseIncludes('a:limit(5|1).b.c.d.e.f.g.h.i.j.NEVER'); |
170
|
|
|
|
171
|
|
|
$this->assertSame( |
172
|
|
|
[ |
173
|
|
|
'a', |
174
|
|
|
'a.b', |
175
|
|
|
'a.b.c', |
176
|
|
|
'a.b.c.d', |
177
|
|
|
'a.b.c.d.e', |
178
|
|
|
'a.b.c.d.e.f', |
179
|
|
|
'a.b.c.d.e.f.g', |
180
|
|
|
'a.b.c.d.e.f.g.h', |
181
|
|
|
'a.b.c.d.e.f.g.h.i', |
182
|
|
|
'a.b.c.d.e.f.g.h.i.j', |
183
|
|
|
], |
184
|
|
|
$manager->getRequestedIncludes() |
185
|
|
|
); |
186
|
|
|
|
187
|
|
|
// Try setting to 3 and see what happens |
188
|
|
|
$manager->setRecursionLimit(3); |
189
|
|
|
$manager->parseIncludes('a.b.c.NEVER'); |
190
|
|
|
|
191
|
|
|
$this->assertSame( |
192
|
|
|
[ |
193
|
|
|
'a', |
194
|
|
|
'a.b', |
195
|
|
|
'a.b.c', |
196
|
|
|
], |
197
|
|
|
$manager->getRequestedIncludes() |
198
|
|
|
); |
199
|
|
|
|
200
|
|
|
$manager->parseIncludes('a:limit(5|1).b.c.NEVER'); |
201
|
|
|
|
202
|
|
|
$this->assertSame( |
203
|
|
|
[ |
204
|
|
|
'a', |
205
|
|
|
'a.b', |
206
|
|
|
'a.b.c', |
207
|
|
|
], |
208
|
|
|
$manager->getRequestedIncludes() |
209
|
|
|
); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
public function testCreateDataWithCallback() |
213
|
|
|
{ |
214
|
|
|
$manager = new Manager(); |
215
|
|
|
|
216
|
|
|
// Item |
217
|
|
|
$resource = new Item(['foo' => 'bar'], function (array $data) { |
218
|
|
|
return $data; |
219
|
|
|
}); |
220
|
|
|
|
221
|
|
|
$rootScope = $manager->createData($resource); |
222
|
|
|
|
223
|
|
|
$this->assertInstanceOf('League\Fractal\Scope', $rootScope); |
224
|
|
|
|
225
|
|
|
|
226
|
|
|
$this->assertSame(['data' => ['foo' => 'bar']], $rootScope->toArray()); |
227
|
|
|
$this->assertSame('{"data":{"foo":"bar"}}', $rootScope->toJson()); |
228
|
|
|
|
229
|
|
|
// Collection |
230
|
|
|
$resource = new Collection([['foo' => 'bar']], function (array $data) { |
231
|
|
|
return $data; |
232
|
|
|
}); |
233
|
|
|
|
234
|
|
|
$rootScope = $manager->createData($resource); |
235
|
|
|
|
236
|
|
|
$this->assertInstanceOf('League\Fractal\Scope', $rootScope); |
237
|
|
|
|
238
|
|
|
|
239
|
|
|
$this->assertSame(['data' => [['foo' => 'bar']]], $rootScope->toArray()); |
240
|
|
|
$this->assertSame('{"data":[{"foo":"bar"}]}', $rootScope->toJson()); |
241
|
|
|
|
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
public function testParseFieldsets() |
245
|
|
|
{ |
246
|
|
|
$manager = new Manager(); |
247
|
|
|
|
248
|
|
|
$fields = [ |
249
|
|
|
'articles' => 'title,body', |
250
|
|
|
'people' => 'name' |
251
|
|
|
]; |
252
|
|
|
|
253
|
|
|
$expectedFieldset = [ |
254
|
|
|
'articles' => ['title' , 'body'], |
255
|
|
|
'people' => ['name'] |
256
|
|
|
]; |
257
|
|
|
|
258
|
|
|
$manager->parseFieldsets($fields); |
259
|
|
|
$this->assertSame($expectedFieldset, $manager->getRequestedFieldsets()); |
260
|
|
|
|
261
|
|
|
$paramBag = new ParamBag($expectedFieldset['articles']); |
262
|
|
|
$this->assertEquals($paramBag, $manager->getFieldset('articles')); |
263
|
|
|
|
264
|
|
|
// Are repeated fields stripped |
265
|
|
|
$manager->parseFieldsets(['foo' => 'bar,baz,bar']); |
266
|
|
|
$this->assertSame(['foo' => ['bar', 'baz']], $manager->getRequestedFieldsets()); |
267
|
|
|
|
268
|
|
|
// Are empty fields stripped |
269
|
|
|
$manager->parseFieldsets(['foo' => 'bar,']); |
270
|
|
|
$this->assertSame(['foo' => ['bar']], $manager->getRequestedFieldsets()); |
271
|
|
|
|
272
|
|
|
// Verify you can send in arrays directly |
273
|
|
|
$manager->parseFieldsets(['foo' => ['bar', 'baz']]); |
274
|
|
|
$this->assertSame(['foo' => ['bar', 'baz']], $manager->getRequestedFieldsets()); |
275
|
|
|
|
276
|
|
|
$this->assertSame(null, $manager->getFieldset('inexistent')); |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
public function tearDown() |
280
|
|
|
{ |
281
|
|
|
Mockery::close(); |
282
|
|
|
} |
283
|
|
|
} |
284
|
|
|
|