DataArraySerializerTest   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 350
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 9
dl 0
loc 350
rs 10
c 0
b 0
f 0

4 Methods

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