Conditions | 1 |
Paths | 1 |
Total Lines | 174 |
Code Lines | 91 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 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 |
||
124 | public function testSerializingCollectionResource() |
||
125 | { |
||
126 | $manager = new Manager(); |
||
127 | $manager->parseIncludes('author'); |
||
128 | $manager->setSerializer(new DataArraySerializer()); |
||
129 | |||
130 | $booksData = [ |
||
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 | // Try without metadata |
||
148 | $resource = new Collection($booksData, new GenericBookTransformer(), 'books'); |
||
149 | |||
150 | $scope = new Scope($manager, $resource); |
||
151 | |||
152 | $expected = [ |
||
153 | 'data' => [ |
||
154 | [ |
||
155 | 'title' => 'Foo', |
||
156 | 'year' => 1991, |
||
157 | 'author' => [ |
||
158 | 'data' => [ |
||
159 | 'name' => 'Dave', |
||
160 | ], |
||
161 | ], |
||
162 | ], |
||
163 | [ |
||
164 | 'title' => 'Bar', |
||
165 | 'year' => 1997, |
||
166 | 'author' => [ |
||
167 | 'data' => [ |
||
168 | 'name' => 'Bob', |
||
169 | ], |
||
170 | ], |
||
171 | ], |
||
172 | ], |
||
173 | ]; |
||
174 | |||
175 | $this->assertSame($expected, $scope->toArray()); |
||
176 | |||
177 | $expectedJson = '{"data":[{"title":"Foo","year":1991,"author":{"data":{"name":"Dave"}}},{"title":"Bar","year":1997,"author":{"data":{"name":"Bob"}}}]}'; |
||
178 | $this->assertSame($expectedJson, $scope->toJson()); |
||
179 | |||
180 | //Test single field |
||
181 | $manager->parseFieldsets(['books' => 'title']); |
||
182 | $expected = [ |
||
183 | 'data' => [ |
||
184 | ['title' => 'Foo'], |
||
185 | ['title' => 'Bar'] |
||
186 | ], |
||
187 | ]; |
||
188 | $this->assertSame($expected, $scope->toArray()); |
||
189 | |||
190 | //Test multiple field |
||
191 | $manager->parseFieldsets(['books' => 'title,year']); |
||
192 | $expected = [ |
||
193 | 'data' => [ |
||
194 | [ |
||
195 | 'title' => 'Foo', |
||
196 | 'year' => 1991 |
||
197 | ], |
||
198 | [ |
||
199 | 'title' => 'Bar', |
||
200 | 'year' => 1997 |
||
201 | ] |
||
202 | ], |
||
203 | ]; |
||
204 | $this->assertSame($expected, $scope->toArray()); |
||
205 | |||
206 | //Test with relationship field |
||
207 | $manager->parseFieldsets(['books' => 'title,author', 'author' => 'name']); |
||
208 | $expected = [ |
||
209 | 'data' => [ |
||
210 | [ |
||
211 | 'title' => 'Foo', |
||
212 | 'author' => [ |
||
213 | 'data' => [ |
||
214 | 'name' => 'Dave' |
||
215 | ] |
||
216 | ] |
||
217 | ], |
||
218 | [ |
||
219 | 'title' => 'Bar', |
||
220 | 'author' => [ |
||
221 | 'data' => [ |
||
222 | 'name' => 'Bob' |
||
223 | ] |
||
224 | ] |
||
225 | ] |
||
226 | ] |
||
227 | ]; |
||
228 | $this->assertSame($expected, $scope->toArray()); |
||
229 | |||
230 | //Clear all sparse fieldsets |
||
231 | $manager->parseFieldsets([]); |
||
232 | |||
233 | // Same again with meta |
||
234 | $resource = new Collection($booksData, new GenericBookTransformer(), 'books'); |
||
235 | $resource->setMetaValue('foo', 'bar'); |
||
236 | |||
237 | $scope = new Scope($manager, $resource); |
||
238 | |||
239 | |||
240 | $expected = [ |
||
241 | 'data' => [ |
||
242 | [ |
||
243 | 'title' => 'Foo', |
||
244 | 'year' => 1991, |
||
245 | 'author' => [ |
||
246 | 'data' => [ |
||
247 | 'name' => 'Dave', |
||
248 | ], |
||
249 | ], |
||
250 | ], |
||
251 | [ |
||
252 | 'title' => 'Bar', |
||
253 | 'year' => 1997, |
||
254 | 'author' => [ |
||
255 | 'data' => [ |
||
256 | 'name' => 'Bob', |
||
257 | ], |
||
258 | ], |
||
259 | ], |
||
260 | ], |
||
261 | 'meta' => [ |
||
262 | 'foo' => 'bar', |
||
263 | ], |
||
264 | ]; |
||
265 | |||
266 | $this->assertSame($expected, $scope->toArray()); |
||
267 | |||
268 | $expectedJson = '{"data":[{"title":"Foo","year":1991,"author":{"data":{"name":"Dave"}}},{"title":"Bar","year":1997,"author":{"data":{"name":"Bob"}}}],"meta":{"foo":"bar"}}'; |
||
269 | $this->assertSame($expectedJson, $scope->toJson()); |
||
270 | |||
271 | $manager->parseFieldsets(['books' => 'title,author', 'author' => 'name']); |
||
272 | $expected = [ |
||
273 | 'data' => [ |
||
274 | [ |
||
275 | 'title' => 'Foo', |
||
276 | 'author' => [ |
||
277 | 'data' => [ |
||
278 | 'name' => 'Dave' |
||
279 | ] |
||
280 | ] |
||
281 | ], |
||
282 | [ |
||
283 | 'title' => 'Bar', |
||
284 | 'author' => [ |
||
285 | 'data' => [ |
||
286 | 'name' => 'Bob' |
||
287 | ] |
||
288 | ] |
||
289 | ] |
||
290 | ], |
||
291 | 'meta' => [ |
||
292 | 'foo' => 'bar' |
||
293 | ] |
||
294 | ]; |
||
295 | |||
296 | $this->assertSame($expected, $scope->toArray()); |
||
297 | } |
||
298 | |||
361 |