Conditions | 1 |
Paths | 1 |
Total Lines | 139 |
Code Lines | 73 |
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 |
||
116 | public function testSerializingCollectionResource() |
||
117 | { |
||
118 | $manager = new Manager(); |
||
119 | $manager->parseIncludes('author'); |
||
120 | $manager->setSerializer(new ArraySerializer()); |
||
121 | |||
122 | $resource = new Collection($this->bookCollectionInput, new GenericBookTransformer(), 'books'); |
||
123 | |||
124 | // Try without metadata |
||
125 | $scope = new Scope($manager, $resource); |
||
126 | |||
127 | $expected = [ |
||
128 | 'books' => [ |
||
129 | [ |
||
130 | 'title' => 'Foo', |
||
131 | 'year' => 1991, |
||
132 | 'author' => [ |
||
133 | 'name' => 'Dave', |
||
134 | ], |
||
135 | ], |
||
136 | [ |
||
137 | 'title' => 'Bar', |
||
138 | 'year' => 1997, |
||
139 | 'author' => [ |
||
140 | 'name' => 'Bob', |
||
141 | ], |
||
142 | ], |
||
143 | ], |
||
144 | ]; |
||
145 | |||
146 | $this->assertSame($expected, $scope->toArray()); |
||
147 | |||
148 | // JSON array of JSON objects |
||
149 | $expectedJson = '{"books":[{"title":"Foo","year":1991,"author":{"name":"Dave"}},{"title":"Bar","year":1997,"author":{"name":"Bob"}}]}'; |
||
150 | $this->assertSame($expectedJson, $scope->toJson()); |
||
151 | |||
152 | //Test single field |
||
153 | $manager->parseFieldsets(['books' => 'title']); |
||
154 | $expected = [ |
||
155 | 'books' => [ |
||
156 | ['title' => 'Foo'], |
||
157 | ['title' => 'Bar'] |
||
158 | ] |
||
159 | ]; |
||
160 | $this->assertSame($expected, $scope->toArray()); |
||
161 | |||
162 | //Test multiple field |
||
163 | $manager->parseFieldsets(['books' => 'title,year']); |
||
164 | $expected = [ |
||
165 | 'books' => [ |
||
166 | [ |
||
167 | 'title' => 'Foo', |
||
168 | 'year' => 1991 |
||
169 | ], |
||
170 | [ |
||
171 | 'title' => 'Bar', |
||
172 | 'year' => 1997 |
||
173 | ] |
||
174 | ] |
||
175 | ]; |
||
176 | $this->assertSame($expected, $scope->toArray()); |
||
177 | |||
178 | //Test with relationship field |
||
179 | $manager->parseFieldsets(['books' => 'title,author', 'author' => 'name']); |
||
180 | $expected = [ |
||
181 | 'books' => [ |
||
182 | [ |
||
183 | 'title' => 'Foo', |
||
184 | 'author' => [ |
||
185 | 'name' => 'Dave' |
||
186 | ] |
||
187 | ], |
||
188 | [ |
||
189 | 'title' => 'Bar', |
||
190 | 'author' => [ |
||
191 | 'name' => 'Bob' |
||
192 | ] |
||
193 | ] |
||
194 | ] |
||
195 | ]; |
||
196 | $this->assertSame($expected, $scope->toArray()); |
||
197 | |||
198 | //Clear all sparse fieldsets |
||
199 | $manager->parseFieldsets([]); |
||
200 | |||
201 | // Same again with metadata |
||
202 | $resource->setMetaValue('foo', 'bar'); |
||
203 | |||
204 | $scope = new Scope($manager, $resource); |
||
205 | |||
206 | $expected = [ |
||
207 | 'books' => [ |
||
208 | [ |
||
209 | 'title' => 'Foo', |
||
210 | 'year' => 1991, |
||
211 | 'author' => [ |
||
212 | 'name' => 'Dave', |
||
213 | ], |
||
214 | ], |
||
215 | [ |
||
216 | 'title' => 'Bar', |
||
217 | 'year' => 1997, |
||
218 | 'author' => [ |
||
219 | 'name' => 'Bob', |
||
220 | ], |
||
221 | ], |
||
222 | ], |
||
223 | 'meta' => [ |
||
224 | 'foo' => 'bar', |
||
225 | ], |
||
226 | ]; |
||
227 | |||
228 | $this->assertSame($expected, $scope->toArray()); |
||
229 | |||
230 | $expectedJson = '{"books":[{"title":"Foo","year":1991,"author":{"name":"Dave"}},{"title":"Bar","year":1997,"author":{"name":"Bob"}}],"meta":{"foo":"bar"}}'; |
||
231 | $this->assertSame($expectedJson, $scope->toJson()); |
||
232 | |||
233 | $manager->parseFieldsets(['books' => 'title,author', 'author' => 'name']); |
||
234 | $expected = [ |
||
235 | 'books' => [ |
||
236 | [ |
||
237 | 'title' => 'Foo', |
||
238 | 'author' => [ |
||
239 | 'name' => 'Dave' |
||
240 | ] |
||
241 | ], |
||
242 | [ |
||
243 | 'title' => 'Bar', |
||
244 | 'author' => [ |
||
245 | 'name' => 'Bob' |
||
246 | ] |
||
247 | ] |
||
248 | ], |
||
249 | 'meta' => [ |
||
250 | 'foo' => 'bar', |
||
251 | ] |
||
252 | ]; |
||
253 | $this->assertSame($expected, $scope->toArray()); |
||
254 | } |
||
255 | |||
337 |