1 | <?php namespace League\Fractal\Test\Serializer; |
||
11 | use PHPUnit\Framework\TestCase; |
||
12 | |||
13 | class ArraySerializerTest extends TestCase |
||
14 | { |
||
15 | private $bookItemInput = [ |
||
16 | 'title' => 'Foo', |
||
17 | 'year' => '1991', |
||
18 | '_author' => [ |
||
19 | 'name' => 'Dave', |
||
20 | ], |
||
21 | ]; |
||
22 | |||
23 | private $bookCollectionInput = [ |
||
24 | [ |
||
25 | 'title' => 'Foo', |
||
26 | 'year' => '1991', |
||
27 | '_author' => [ |
||
28 | 'name' => 'Dave', |
||
29 | ], |
||
30 | ], |
||
31 | [ |
||
32 | 'title' => 'Bar', |
||
33 | 'year' => '1997', |
||
34 | '_author' => [ |
||
35 | 'name' => 'Bob', |
||
36 | ], |
||
37 | ], |
||
38 | ]; |
||
39 | |||
40 | public function testSerializingItemResource() |
||
117 | |||
118 | public function testSerializingCollectionResource() |
||
119 | { |
||
120 | $manager = new Manager(); |
||
121 | $manager->parseIncludes('author'); |
||
122 | $manager->setSerializer(new ArraySerializer()); |
||
123 | |||
124 | $resource = new Collection($this->bookCollectionInput, new GenericBookTransformer(), 'books'); |
||
125 | |||
126 | // Try without metadata |
||
127 | $scope = new Scope($manager, $resource); |
||
128 | |||
129 | $expected = [ |
||
130 | 'books' => [ |
||
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 | |||
148 | $this->assertSame($expected, $scope->toArray()); |
||
149 | |||
150 | // JSON array of JSON objects |
||
151 | $expectedJson = '{"books":[{"title":"Foo","year":1991,"author":{"name":"Dave"}},{"title":"Bar","year":1997,"author":{"name":"Bob"}}]}'; |
||
152 | $this->assertSame($expectedJson, $scope->toJson()); |
||
153 | |||
154 | //Test single field |
||
155 | $manager->parseFieldsets(['books' => 'title']); |
||
156 | $expected = [ |
||
157 | 'books' => [ |
||
158 | ['title' => 'Foo'], |
||
159 | ['title' => 'Bar'] |
||
160 | ] |
||
161 | ]; |
||
162 | $this->assertSame($expected, $scope->toArray()); |
||
163 | |||
164 | //Test multiple field |
||
165 | $manager->parseFieldsets(['books' => 'title,year']); |
||
166 | $expected = [ |
||
167 | 'books' => [ |
||
168 | [ |
||
169 | 'title' => 'Foo', |
||
170 | 'year' => 1991 |
||
171 | ], |
||
172 | [ |
||
173 | 'title' => 'Bar', |
||
174 | 'year' => 1997 |
||
175 | ] |
||
176 | ] |
||
177 | ]; |
||
178 | $this->assertSame($expected, $scope->toArray()); |
||
179 | |||
180 | //Test with relationship field |
||
181 | $manager->parseFieldsets(['books' => 'title,author', 'author' => 'name']); |
||
182 | $expected = [ |
||
183 | 'books' => [ |
||
184 | [ |
||
185 | 'title' => 'Foo', |
||
186 | 'author' => [ |
||
187 | 'name' => 'Dave' |
||
188 | ] |
||
189 | ], |
||
190 | [ |
||
191 | 'title' => 'Bar', |
||
192 | 'author' => [ |
||
193 | 'name' => 'Bob' |
||
194 | ] |
||
195 | ] |
||
196 | ] |
||
197 | ]; |
||
198 | $this->assertSame($expected, $scope->toArray()); |
||
199 | |||
200 | //Clear all sparse fieldsets |
||
201 | $manager->parseFieldsets([]); |
||
202 | |||
203 | // Same again with metadata |
||
204 | $resource->setMetaValue('foo', 'bar'); |
||
205 | |||
206 | $scope = new Scope($manager, $resource); |
||
207 | |||
208 | $expected = [ |
||
209 | 'books' => [ |
||
339 |