| Conditions | 1 |
| Paths | 1 |
| Total Lines | 110 |
| 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 |
||
| 16 | public function testSerializingItemResource() |
||
| 17 | { |
||
| 18 | $manager = new Manager(); |
||
| 19 | $manager->parseIncludes('author'); |
||
| 20 | $manager->setSerializer(new DataArraySerializer()); |
||
| 21 | |||
| 22 | $bookData = [ |
||
| 23 | 'title' => 'Foo', |
||
| 24 | 'year' => '1991', |
||
| 25 | '_author' => [ |
||
| 26 | 'name' => 'Dave', |
||
| 27 | ], |
||
| 28 | ]; |
||
| 29 | |||
| 30 | // Try without metadata |
||
| 31 | $resource = new Item($bookData, new GenericBookTransformer(), 'book'); |
||
| 32 | $scope = new Scope($manager, $resource); |
||
| 33 | |||
| 34 | $expected = [ |
||
| 35 | 'data' => [ |
||
| 36 | 'title' => 'Foo', |
||
| 37 | 'year' => 1991, |
||
| 38 | 'author' => [ |
||
| 39 | 'data' => [ |
||
| 40 | 'name' => 'Dave', |
||
| 41 | ], |
||
| 42 | ], |
||
| 43 | ], |
||
| 44 | ]; |
||
| 45 | |||
| 46 | $this->assertSame($expected, $scope->toArray()); |
||
| 47 | |||
| 48 | //Test single field |
||
| 49 | $manager->parseFieldsets(['book' => 'title']); |
||
| 50 | $expected = [ |
||
| 51 | 'data' => [ |
||
| 52 | 'title' => 'Foo', |
||
| 53 | ] |
||
| 54 | ]; |
||
| 55 | $this->assertSame($expected, $scope->toArray()); |
||
| 56 | |||
| 57 | //Test multiple field |
||
| 58 | $manager->parseFieldsets(['book' => 'title,year']); |
||
| 59 | $expected = [ |
||
| 60 | 'data' => [ |
||
| 61 | 'title' => 'Foo', |
||
| 62 | 'year' => 1991 |
||
| 63 | ] |
||
| 64 | ]; |
||
| 65 | $this->assertSame($expected, $scope->toArray()); |
||
| 66 | |||
| 67 | //Test with relationship field |
||
| 68 | $manager->parseFieldsets(['book' => 'title,author', 'author' => 'name']); |
||
| 69 | $expected = [ |
||
| 70 | 'data' => [ |
||
| 71 | 'title' => 'Foo', |
||
| 72 | 'author' => [ |
||
| 73 | 'data' => [ |
||
| 74 | 'name' => 'Dave' |
||
| 75 | ] |
||
| 76 | ] |
||
| 77 | ] |
||
| 78 | ]; |
||
| 79 | $this->assertSame($expected, $scope->toArray()); |
||
| 80 | |||
| 81 | //Clear all sparse fieldsets |
||
| 82 | $manager->parseFieldsets([]); |
||
| 83 | |||
| 84 | // Same again with metadata |
||
| 85 | $resource = new Item($bookData, new GenericBookTransformer(), 'book'); |
||
| 86 | $resource->setMetaValue('foo', 'bar'); |
||
| 87 | |||
| 88 | $scope = new Scope($manager, $resource); |
||
| 89 | |||
| 90 | $expected = [ |
||
| 91 | 'data' => [ |
||
| 92 | 'title' => 'Foo', |
||
| 93 | 'year' => 1991, |
||
| 94 | 'author' => [ |
||
| 95 | 'data' => [ |
||
| 96 | 'name' => 'Dave', |
||
| 97 | |||
| 98 | ], |
||
| 99 | ], |
||
| 100 | ], |
||
| 101 | 'meta' => [ |
||
| 102 | 'foo' => 'bar', |
||
| 103 | ], |
||
| 104 | ]; |
||
| 105 | |||
| 106 | $this->assertSame($expected, $scope->toArray()); |
||
| 107 | |||
| 108 | //Test with relationship field |
||
| 109 | $manager->parseFieldsets(['book' => 'title,author', 'author' => 'name']); |
||
| 110 | $expected = [ |
||
| 111 | 'data' => [ |
||
| 112 | 'title' => 'Foo', |
||
| 113 | 'author' => [ |
||
| 114 | 'data' => [ |
||
| 115 | 'name' => 'Dave' |
||
| 116 | |||
| 117 | ] |
||
| 118 | ] |
||
| 119 | ], |
||
| 120 | 'meta' => [ |
||
| 121 | 'foo' => 'bar' |
||
| 122 | ], |
||
| 123 | ]; |
||
| 124 | $this->assertSame($expected, $scope->toArray()); |
||
| 125 | } |
||
| 126 | |||
| 359 |