Completed
Pull Request — master (#273)
by Gonçalo
04:11
created

testSerializingCollectionResource()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 174
Code Lines 91

Duplication

Lines 0
Ratio 0 %

Importance

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