Completed
Pull Request — master (#489)
by Milroy
02:55
created

testSerializingNullResource()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 51

Duplication

Lines 0
Ratio 0 %

Importance

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