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

ArraySerializerTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 326
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 5
c 2
b 0
f 1
lcom 1
cbo 9
dl 0
loc 326
rs 10

5 Methods

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