Completed
Push — 1.0.x ( 209591...7a1bb8 )
by Korvin
04:54
created

testSerializingCollectionResource()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 174

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 174
rs 8
c 0
b 0
f 0
cc 1
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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