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