1
|
|
|
<?php namespace League\Fractal\Test\Serializer; |
2
|
|
|
|
3
|
|
|
use League\Fractal\Manager; |
4
|
|
|
use League\Fractal\Resource\Collection; |
5
|
|
|
use League\Fractal\Resource\Item; |
6
|
|
|
use League\Fractal\Resource\NullResource; |
7
|
|
|
use League\Fractal\Scope; |
8
|
|
|
use League\Fractal\Serializer\ArraySerializer; |
9
|
|
|
use League\Fractal\Test\Stub\Transformer\GenericBookTransformer; |
10
|
|
|
use Mockery; |
11
|
|
|
use PHPUnit\Framework\TestCase; |
12
|
|
|
|
13
|
|
|
class ArraySerializerTest extends TestCase |
14
|
|
|
{ |
15
|
|
|
private $bookItemInput = [ |
16
|
|
|
'title' => 'Foo', |
17
|
|
|
'year' => '1991', |
18
|
|
|
'_author' => [ |
19
|
|
|
'name' => 'Dave', |
20
|
|
|
], |
21
|
|
|
]; |
22
|
|
|
|
23
|
|
|
private $bookCollectionInput = [ |
24
|
|
|
[ |
25
|
|
|
'title' => 'Foo', |
26
|
|
|
'year' => '1991', |
27
|
|
|
'_author' => [ |
28
|
|
|
'name' => 'Dave', |
29
|
|
|
], |
30
|
|
|
], |
31
|
|
|
[ |
32
|
|
|
'title' => 'Bar', |
33
|
|
|
'year' => '1997', |
34
|
|
|
'_author' => [ |
35
|
|
|
'name' => 'Bob', |
36
|
|
|
], |
37
|
|
|
], |
38
|
|
|
]; |
39
|
|
|
|
40
|
|
|
public function testSerializingItemResource() |
41
|
|
|
{ |
42
|
|
|
$manager = new Manager(); |
43
|
|
|
$manager->parseIncludes('author'); |
44
|
|
|
$manager->setSerializer(new ArraySerializer()); |
45
|
|
|
|
46
|
|
|
$resource = new Item($this->bookItemInput, new GenericBookTransformer(), 'book'); |
47
|
|
|
|
48
|
|
|
// Try without metadata |
49
|
|
|
$scope = new Scope($manager, $resource); |
50
|
|
|
|
51
|
|
|
$expected = [ |
52
|
|
|
'title' => 'Foo', |
53
|
|
|
'year' => 1991, |
54
|
|
|
'author' => [ |
55
|
|
|
'name' => 'Dave', |
56
|
|
|
], |
57
|
|
|
]; |
58
|
|
|
|
59
|
|
|
$this->assertSame($expected, $scope->toArray()); |
60
|
|
|
|
61
|
|
|
//Test single field |
62
|
|
|
$manager->parseFieldsets(['book' => 'title']); |
63
|
|
|
$expected = ['title' => 'Foo']; |
64
|
|
|
$this->assertSame($expected, $scope->toArray()); |
65
|
|
|
|
66
|
|
|
//Test multiple field |
67
|
|
|
$manager->parseFieldsets(['book' => 'title,year']); |
68
|
|
|
$expected = [ |
69
|
|
|
'title' => 'Foo', |
70
|
|
|
'year' => 1991 |
71
|
|
|
]; |
72
|
|
|
$this->assertSame($expected, $scope->toArray()); |
73
|
|
|
|
74
|
|
|
//Test with relationship field |
75
|
|
|
$manager->parseFieldsets(['book' => 'title,author', 'author' => 'name']); |
76
|
|
|
$expected = [ |
77
|
|
|
'title' => 'Foo', |
78
|
|
|
'author' => [ |
79
|
|
|
'name' => 'Dave' |
80
|
|
|
], |
81
|
|
|
]; |
82
|
|
|
$this->assertSame($expected, $scope->toArray()); |
83
|
|
|
|
84
|
|
|
//Clear all sparse fieldsets |
85
|
|
|
$manager->parseFieldsets([]); |
86
|
|
|
//Same again with meta |
87
|
|
|
$resource->setMetaValue('foo', 'bar'); |
88
|
|
|
|
89
|
|
|
$scope = new Scope($manager, $resource); |
90
|
|
|
|
91
|
|
|
$expected = [ |
92
|
|
|
'title' => 'Foo', |
93
|
|
|
'year' => 1991, |
94
|
|
|
'author' => [ |
95
|
|
|
'name' => 'Dave' |
96
|
|
|
], |
97
|
|
|
'meta' => [ |
98
|
|
|
'foo' => 'bar' |
99
|
|
|
] |
100
|
|
|
]; |
101
|
|
|
|
102
|
|
|
$this->assertSame($expected, $scope->toArray()); |
103
|
|
|
|
104
|
|
|
//Test with relationship field |
105
|
|
|
$manager->parseFieldsets(['book' => 'title,author', 'author' => 'name']); |
106
|
|
|
$expected = [ |
107
|
|
|
'title' => 'Foo', |
108
|
|
|
'author' => [ |
109
|
|
|
'name' => 'Dave', |
110
|
|
|
], |
111
|
|
|
'meta' => [ |
112
|
|
|
'foo' => 'bar', |
113
|
|
|
] |
114
|
|
|
]; |
115
|
|
|
$this->assertSame($expected, $scope->toArray()); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function testSerializingCollectionResource() |
119
|
|
|
{ |
120
|
|
|
$manager = new Manager(); |
121
|
|
|
$manager->parseIncludes('author'); |
122
|
|
|
$manager->setSerializer(new ArraySerializer()); |
123
|
|
|
|
124
|
|
|
$resource = new Collection($this->bookCollectionInput, new GenericBookTransformer(), 'books'); |
125
|
|
|
|
126
|
|
|
// Try without metadata |
127
|
|
|
$scope = new Scope($manager, $resource); |
128
|
|
|
|
129
|
|
|
$expected = [ |
130
|
|
|
'books' => [ |
131
|
|
|
[ |
132
|
|
|
'title' => 'Foo', |
133
|
|
|
'year' => 1991, |
134
|
|
|
'author' => [ |
135
|
|
|
'name' => 'Dave', |
136
|
|
|
], |
137
|
|
|
], |
138
|
|
|
[ |
139
|
|
|
'title' => 'Bar', |
140
|
|
|
'year' => 1997, |
141
|
|
|
'author' => [ |
142
|
|
|
'name' => 'Bob', |
143
|
|
|
], |
144
|
|
|
], |
145
|
|
|
], |
146
|
|
|
]; |
147
|
|
|
|
148
|
|
|
$this->assertSame($expected, $scope->toArray()); |
149
|
|
|
|
150
|
|
|
// JSON array of JSON objects |
151
|
|
|
$expectedJson = '{"books":[{"title":"Foo","year":1991,"author":{"name":"Dave"}},{"title":"Bar","year":1997,"author":{"name":"Bob"}}]}'; |
152
|
|
|
$this->assertSame($expectedJson, $scope->toJson()); |
153
|
|
|
|
154
|
|
|
//Test single field |
155
|
|
|
$manager->parseFieldsets(['books' => 'title']); |
156
|
|
|
$expected = [ |
157
|
|
|
'books' => [ |
158
|
|
|
['title' => 'Foo'], |
159
|
|
|
['title' => 'Bar'] |
160
|
|
|
] |
161
|
|
|
]; |
162
|
|
|
$this->assertSame($expected, $scope->toArray()); |
163
|
|
|
|
164
|
|
|
//Test multiple field |
165
|
|
|
$manager->parseFieldsets(['books' => 'title,year']); |
166
|
|
|
$expected = [ |
167
|
|
|
'books' => [ |
168
|
|
|
[ |
169
|
|
|
'title' => 'Foo', |
170
|
|
|
'year' => 1991 |
171
|
|
|
], |
172
|
|
|
[ |
173
|
|
|
'title' => 'Bar', |
174
|
|
|
'year' => 1997 |
175
|
|
|
] |
176
|
|
|
] |
177
|
|
|
]; |
178
|
|
|
$this->assertSame($expected, $scope->toArray()); |
179
|
|
|
|
180
|
|
|
//Test with relationship field |
181
|
|
|
$manager->parseFieldsets(['books' => 'title,author', 'author' => 'name']); |
182
|
|
|
$expected = [ |
183
|
|
|
'books' => [ |
184
|
|
|
[ |
185
|
|
|
'title' => 'Foo', |
186
|
|
|
'author' => [ |
187
|
|
|
'name' => 'Dave' |
188
|
|
|
] |
189
|
|
|
], |
190
|
|
|
[ |
191
|
|
|
'title' => 'Bar', |
192
|
|
|
'author' => [ |
193
|
|
|
'name' => 'Bob' |
194
|
|
|
] |
195
|
|
|
] |
196
|
|
|
] |
197
|
|
|
]; |
198
|
|
|
$this->assertSame($expected, $scope->toArray()); |
199
|
|
|
|
200
|
|
|
//Clear all sparse fieldsets |
201
|
|
|
$manager->parseFieldsets([]); |
202
|
|
|
|
203
|
|
|
// Same again with metadata |
204
|
|
|
$resource->setMetaValue('foo', 'bar'); |
205
|
|
|
|
206
|
|
|
$scope = new Scope($manager, $resource); |
207
|
|
|
|
208
|
|
|
$expected = [ |
209
|
|
|
'books' => [ |
210
|
|
|
[ |
211
|
|
|
'title' => 'Foo', |
212
|
|
|
'year' => 1991, |
213
|
|
|
'author' => [ |
214
|
|
|
'name' => 'Dave', |
215
|
|
|
], |
216
|
|
|
], |
217
|
|
|
[ |
218
|
|
|
'title' => 'Bar', |
219
|
|
|
'year' => 1997, |
220
|
|
|
'author' => [ |
221
|
|
|
'name' => 'Bob', |
222
|
|
|
], |
223
|
|
|
], |
224
|
|
|
], |
225
|
|
|
'meta' => [ |
226
|
|
|
'foo' => 'bar', |
227
|
|
|
], |
228
|
|
|
]; |
229
|
|
|
|
230
|
|
|
$this->assertSame($expected, $scope->toArray()); |
231
|
|
|
|
232
|
|
|
$expectedJson = '{"books":[{"title":"Foo","year":1991,"author":{"name":"Dave"}},{"title":"Bar","year":1997,"author":{"name":"Bob"}}],"meta":{"foo":"bar"}}'; |
233
|
|
|
$this->assertSame($expectedJson, $scope->toJson()); |
234
|
|
|
|
235
|
|
|
$manager->parseFieldsets(['books' => 'title,author', 'author' => 'name']); |
236
|
|
|
$expected = [ |
237
|
|
|
'books' => [ |
238
|
|
|
[ |
239
|
|
|
'title' => 'Foo', |
240
|
|
|
'author' => [ |
241
|
|
|
'name' => 'Dave' |
242
|
|
|
] |
243
|
|
|
], |
244
|
|
|
[ |
245
|
|
|
'title' => 'Bar', |
246
|
|
|
'author' => [ |
247
|
|
|
'name' => 'Bob' |
248
|
|
|
] |
249
|
|
|
] |
250
|
|
|
], |
251
|
|
|
'meta' => [ |
252
|
|
|
'foo' => 'bar', |
253
|
|
|
] |
254
|
|
|
]; |
255
|
|
|
$this->assertSame($expected, $scope->toArray()); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
public function testSerializingNullResource() |
259
|
|
|
{ |
260
|
|
|
$manager = new Manager(); |
261
|
|
|
$manager->parseIncludes('author'); |
262
|
|
|
$manager->setSerializer(new ArraySerializer()); |
263
|
|
|
|
264
|
|
|
$resource = new NullResource($this->bookCollectionInput, new GenericBookTransformer(), 'books'); |
265
|
|
|
|
266
|
|
|
// Try without metadata |
267
|
|
|
$scope = new Scope($manager, $resource); |
268
|
|
|
|
269
|
|
|
$expected = []; |
270
|
|
|
$this->assertSame($expected, $scope->toArray()); |
271
|
|
|
|
272
|
|
|
// JSON array of JSON objects |
273
|
|
|
$expectedJson = '[]'; |
274
|
|
|
$this->assertSame($expectedJson, $scope->toJson()); |
275
|
|
|
|
276
|
|
|
//Test single field |
277
|
|
|
$manager->parseFieldsets(['books' => 'title']); |
278
|
|
|
$this->assertSame($expected, $scope->toArray()); |
279
|
|
|
|
280
|
|
|
//Test multiple fields |
281
|
|
|
$manager->parseFieldsets(['books' => 'title,year']); |
282
|
|
|
$this->assertSame($expected, $scope->toArray()); |
283
|
|
|
|
284
|
|
|
//Test with relationship |
285
|
|
|
$manager->parseFieldsets(['books' => 'title,author', 'author' => 'name']); |
286
|
|
|
$this->assertSame($expected, $scope->toArray()); |
287
|
|
|
|
288
|
|
|
//Clear all sparse fieldsets |
289
|
|
|
$manager->parseFieldsets([]); |
290
|
|
|
|
291
|
|
|
// Same again with metadata |
292
|
|
|
$resource->setMetaValue('foo', 'bar'); |
293
|
|
|
$scope = new Scope($manager, $resource); |
294
|
|
|
|
295
|
|
|
$expected = [ |
296
|
|
|
'meta' => [ |
297
|
|
|
'foo' => 'bar', |
298
|
|
|
], |
299
|
|
|
]; |
300
|
|
|
$this->assertSame($expected, $scope->toArray()); |
301
|
|
|
|
302
|
|
|
$expectedJson = '{"meta":{"foo":"bar"}}'; |
303
|
|
|
$this->assertSame($expectedJson, $scope->toJson()); |
304
|
|
|
|
305
|
|
|
//Test with relationship |
306
|
|
|
$manager->parseFieldsets(['books' => 'title,author', 'author' => 'name']); |
307
|
|
|
$this->assertSame($expected, $scope->toArray()); |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
public function testSerializingCollectionResourceWithoutName() |
311
|
|
|
{ |
312
|
|
|
$manager = new Manager(); |
313
|
|
|
$manager->parseIncludes('author'); |
314
|
|
|
$manager->setSerializer(new ArraySerializer()); |
315
|
|
|
|
316
|
|
|
$resource = new Collection($this->bookCollectionInput, new GenericBookTransformer()); |
317
|
|
|
|
318
|
|
|
// Try without metadata |
319
|
|
|
$scope = new Scope($manager, $resource); |
320
|
|
|
|
321
|
|
|
// JSON array of JSON objects |
322
|
|
|
$expectedJson = '{"data":[{"title":"Foo","year":1991,"author":{"name":"Dave"}},{"title":"Bar","year":1997,"author":{"name":"Bob"}}]}'; |
323
|
|
|
$this->assertSame($expectedJson, $scope->toJson()); |
324
|
|
|
|
325
|
|
|
// Same again with metadata |
326
|
|
|
$resource->setMetaValue('foo', 'bar'); |
327
|
|
|
|
328
|
|
|
$scope = new Scope($manager, $resource); |
329
|
|
|
|
330
|
|
|
$expectedJson = '{"data":[{"title":"Foo","year":1991,"author":{"name":"Dave"}},{"title":"Bar","year":1997,"author":{"name":"Bob"}}],"meta":{"foo":"bar"}}'; |
331
|
|
|
$this->assertSame($expectedJson, $scope->toJson()); |
332
|
|
|
} |
333
|
|
|
|
334
|
|
|
public function tearDown() |
335
|
|
|
{ |
336
|
|
|
Mockery::close(); |
337
|
|
|
} |
338
|
|
|
} |
339
|
|
|
|