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