Completed
Pull Request — master (#277)
by
unknown
24:56
created

ArraySerializerTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 198
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

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

5 Methods

Rating   Name   Duplication   Size   Complexity  
B testSerializingItemResource() 0 39 1
A testSerializingCollectionResource() 0 68 1
B testSerializingNullResource() 0 32 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
        // Same again with meta
60
        $resource->setMetaValue('foo', 'bar');
61
62
        $scope = new Scope($manager, $resource);
63
64
        $expected = [
65
            'title' => 'Foo',
66
            'year' => 1991,
67
            'author' => [
68
                'name' => 'Dave',
69
            ],
70
            'meta' => [
71
                'foo' => 'bar',
72
            ],
73
        ];
74
75
        $this->assertSame($expected, $scope->toArray());
76
    }
77
78
    public function testSerializingCollectionResource()
79
    {
80
        $manager = new Manager();
81
        $manager->parseIncludes('author');
82
        $manager->setSerializer(new ArraySerializer());
83
84
        $resource = new Collection($this->bookCollectionInput, new GenericBookTransformer(), 'books');
85
86
        // Try without metadata
87
        $scope = new Scope($manager, $resource);
88
89
        $expected = [
90
            'books' => [
91
                [
92
                    'title' => 'Foo',
93
                    'year' => 1991,
94
                    'author' => [
95
                        'name' => 'Dave',
96
                    ],
97
                ],
98
                [
99
                    'title' => 'Bar',
100
                    'year' => 1997,
101
                    'author' => [
102
                        'name' => 'Bob',
103
                    ],
104
                ],
105
            ],
106
        ];
107
108
        $this->assertSame($expected, $scope->toArray());
109
110
        // JSON array of JSON objects
111
        $expectedJson = '{"books":[{"title":"Foo","year":1991,"author":{"name":"Dave"}},{"title":"Bar","year":1997,"author":{"name":"Bob"}}]}';
112
        $this->assertSame($expectedJson, $scope->toJson());
113
114
        // Same again with metadata
115
        $resource->setMetaValue('foo', 'bar');
116
117
        $scope = new Scope($manager, $resource);
118
119
        $expected = [
120
            'books' => [
121
                [
122
                    'title' => 'Foo',
123
                    'year' => 1991,
124
                    'author' => [
125
                        'name' => 'Dave',
126
                    ],
127
                ],
128
                [
129
                    'title' => 'Bar',
130
                    'year' => 1997,
131
                    'author' => [
132
                        'name' => 'Bob',
133
                    ],
134
                ],
135
            ],
136
            'meta' => [
137
                'foo' => 'bar',
138
            ],
139
        ];
140
141
        $this->assertSame($expected, $scope->toArray());
142
143
        $expectedJson = '{"books":[{"title":"Foo","year":1991,"author":{"name":"Dave"}},{"title":"Bar","year":1997,"author":{"name":"Bob"}}],"meta":{"foo":"bar"}}';
144
        $this->assertSame($expectedJson, $scope->toJson());
145
    }
146
147
    public function testSerializingNullResource()
148
    {
149
        $manager = new Manager();
150
        $manager->parseIncludes('author');
151
        $manager->setSerializer(new ArraySerializer());
152
153
        $resource = new NullResource($this->bookCollectionInput, new GenericBookTransformer(), 'books');
154
155
        // Try without metadata
156
        $scope = new Scope($manager, $resource);
157
158
        $expected = [];
159
        $this->assertSame($expected, $scope->toArray());
160
161
        // JSON array of JSON objects
162
        $expectedJson = '[]';
163
        $this->assertSame($expectedJson, $scope->toJson());
164
165
        // Same again with metadata
166
        $resource->setMetaValue('foo', 'bar');
167
        $scope = new Scope($manager, $resource);
168
169
        $expected = [
170
            'meta' => [
171
                'foo' => 'bar',
172
            ],
173
        ];
174
        $this->assertSame($expected, $scope->toArray());
175
176
        $expectedJson = '{"meta":{"foo":"bar"}}';
177
        $this->assertSame($expectedJson, $scope->toJson());
178
    }
179
180
    public function testSerializingCollectionResourceWithoutName()
181
    {
182
        $manager = new Manager();
183
        $manager->parseIncludes('author');
184
        $manager->setSerializer(new ArraySerializer());
185
186
        $resource = new Collection($this->bookCollectionInput, new GenericBookTransformer());
187
188
        // Try without metadata
189
        $scope = new Scope($manager, $resource);
190
191
        // JSON array of JSON objects
192
        $expectedJson = '{"data":[{"title":"Foo","year":1991,"author":{"name":"Dave"}},{"title":"Bar","year":1997,"author":{"name":"Bob"}}]}';
193
        $this->assertSame($expectedJson, $scope->toJson());
194
195
        // Same again with metadata
196
        $resource->setMetaValue('foo', 'bar');
197
198
        $scope = new Scope($manager, $resource);
199
200
        $expectedJson = '{"data":[{"title":"Foo","year":1991,"author":{"name":"Dave"}},{"title":"Bar","year":1997,"author":{"name":"Bob"}}],"meta":{"foo":"bar"}}';
201
        $this->assertSame($expectedJson, $scope->toJson());
202
    }
203
204
    public function tearDown()
205
    {
206
        Mockery::close();
207
    }
208
}
209