Conditions | 1 |
Paths | 1 |
Total Lines | 139 |
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\Serializer; |
||
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' => [ |
||
210 | [ |
||
211 | 'title' => 'Foo', |
||
212 | 'year' => 1991, |
||
213 | 'author' => [ |
||
214 | 'name' => 'Dave', |
||
215 | ], |
||
216 | ], |
||
217 | [ |
||
218 | 'title' => 'Bar', |
||
219 | 'year' => 1997, |
||
220 | 'author' => [ |
||
221 | 'name' => 'Bob', |
||
222 | ], |
||
223 | ], |
||
224 | ], |
||
225 | 'meta' => [ |
||
226 | 'foo' => 'bar', |
||
227 | ], |
||
228 | ]; |
||
229 | |||
230 | $this->assertSame($expected, $scope->toArray()); |
||
231 | |||
232 | $expectedJson = '{"books":[{"title":"Foo","year":1991,"author":{"name":"Dave"}},{"title":"Bar","year":1997,"author":{"name":"Bob"}}],"meta":{"foo":"bar"}}'; |
||
233 | $this->assertSame($expectedJson, $scope->toJson()); |
||
234 | |||
235 | $manager->parseFieldsets(['books' => 'title,author', 'author' => 'name']); |
||
236 | $expected = [ |
||
237 | 'books' => [ |
||
238 | [ |
||
239 | 'title' => 'Foo', |
||
240 | 'author' => [ |
||
241 | 'name' => 'Dave' |
||
242 | ] |
||
243 | ], |
||
244 | [ |
||
245 | 'title' => 'Bar', |
||
246 | 'author' => [ |
||
247 | 'name' => 'Bob' |
||
248 | ] |
||
249 | ] |
||
250 | ], |
||
251 | 'meta' => [ |
||
252 | 'foo' => 'bar', |
||
253 | ] |
||
254 | ]; |
||
255 | $this->assertSame($expected, $scope->toArray()); |
||
256 | } |
||
257 | |||
339 |