Conditions | 1 |
Paths | 1 |
Total Lines | 168 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php namespace League\Fractal\Test\Resource; |
||
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 | |||
347 |