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