Conditions | 1 |
Paths | 1 |
Total Lines | 65 |
Code Lines | 44 |
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 |
||
91 | public function testRSSParser() |
||
92 | { |
||
93 | // Use a dummy (but valid) URI for the requests |
||
94 | $url = 'http://localhost/'; |
||
95 | |||
96 | // Create a mock and queue two responses |
||
97 | $mock = new MockHandler([ |
||
98 | new Response(200, [], file_get_contents(__DIR__ . '/sample1.rss')), |
||
99 | new Response(200, [], file_get_contents(__DIR__ . '/sample2.rss')), |
||
100 | ]); |
||
101 | $handler = HandlerStack::create($mock); |
||
102 | |||
103 | $service = new FeedReaderService($url, 3600, [ 'handler' => $handler ]); |
||
104 | $service->clearCache(); |
||
105 | |||
106 | // First read |
||
107 | $items = $service->getItems(); |
||
108 | |||
109 | $this->assertEquals(1, $items->count()); |
||
110 | |||
111 | $item = $items[0]; |
||
112 | $this->assertEquals('7bd204c6-1655-4c27-aeee-53f933c5395f', $item->Id); |
||
113 | $this->assertEquals('http://www.example.com/blog/post/1', $item->Link); |
||
114 | $this->assertEquals('2009-09-06 16:20:00', (string) $item->Date); |
||
115 | $this->assertEquals('Example entry', $item->Title); |
||
116 | $this->assertEquals('Here is some text containing an interesting description.', $item->Summary); |
||
117 | $this->assertEquals('Here is some text containing an interesting description.', $item->Content); |
||
118 | |||
119 | // Second read: must still return the first one (cached) |
||
120 | $items = $service->getItems(); |
||
121 | $this->assertEquals(1, $items->count()); |
||
122 | |||
123 | // Third read: must read the sample2.rss mocked response |
||
124 | $service->clearCache(); |
||
125 | $items = $service->getItems(); |
||
126 | |||
127 | $this->assertEquals(3, $items->count()); |
||
128 | |||
129 | $item = $items[0]; |
||
130 | $this->assertEquals('http://example.org/1', $item->Id); |
||
131 | $this->assertEquals('', $item->Link); |
||
132 | $this->assertEquals('2017-12-19 11:22:33', (string) $item->Date); |
||
133 | $this->assertEquals('Entry 1', $item->Title); |
||
134 | $this->assertEquals('', $item->Summary); |
||
135 | $this->assertEquals('', $item->Content); |
||
136 | |||
137 | $item = $items[1]; |
||
138 | $this->assertEquals('http://example.org/2', $item->Id); |
||
139 | $this->assertEquals('http://example.org/link/2', $item->Link); |
||
140 | $this->assertEquals('2017-12-19 22:33:44', (string) $item->Date); |
||
141 | $this->assertEquals('Entry 2', $item->Title); |
||
142 | $this->assertEquals('Content 2', $item->Summary); |
||
143 | $this->assertEquals('Content 2', $item->Content); |
||
144 | |||
145 | $item = $items[2]; |
||
146 | $this->assertEquals('http://example.org/3', $item->Id); |
||
147 | $this->assertEquals('http://example.org/link/3', $item->Link); |
||
148 | $this->assertEquals('2017-12-19 03:44:55', (string) $item->Date); |
||
149 | $this->assertEquals('Entry 3', $item->Title); |
||
150 | $this->assertEquals('Summary 3', $item->Summary); |
||
151 | $this->assertEquals('Summary 3', $item->Content); |
||
152 | |||
153 | // Forth read: must still return sample2.rss (cached) |
||
154 | $items = $service->getItems(); |
||
155 | $this->assertEquals(3, $items->count()); |
||
156 | } |
||
203 |