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