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