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 |
||
21 | public function testAtomParser() |
||
22 | { |
||
23 | // Use a dummy (but valid) URI for the requests |
||
24 | $url = 'http://localhost/'; |
||
25 | |||
26 | // Create a mock and queue two responses |
||
27 | $mock = new MockHandler([ |
||
28 | new Response(200, [], file_get_contents(__DIR__ . '/sample1.atom')), |
||
29 | new Response(200, [], file_get_contents(__DIR__ . '/sample2.atom')), |
||
30 | ]); |
||
31 | $handler = HandlerStack::create($mock); |
||
32 | |||
33 | $service = new FeedReaderService($url, 3600, [ 'handler' => $handler ]); |
||
34 | $service->clearCache(); |
||
35 | |||
36 | // First read |
||
37 | $items = $service->getItems(); |
||
38 | |||
39 | $this->assertEquals(1, $items->count()); |
||
40 | |||
41 | $item = $items[0]; |
||
42 | $this->assertEquals('urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a', $item->Id); |
||
43 | $this->assertEquals('http://example.org/2003/12/13/atom03', $item->Link); |
||
44 | $this->assertEquals('2003-12-13 18:30:02', (string) $item->Date); |
||
45 | $this->assertEquals('Atom-Powered Robots Run Amok', $item->Title); |
||
46 | $this->assertEquals('Some text.', $item->Summary); |
||
47 | $this->assertTrue(strlen($item->Content) > 20); |
||
48 | |||
49 | // Second read: must still return the first one (cached) |
||
50 | $items = $service->getItems(); |
||
51 | $this->assertEquals(1, $items->count()); |
||
52 | |||
53 | // Third read: must read the sample2.atom mocked response |
||
54 | $service->clearCache(); |
||
55 | $items = $service->getItems(); |
||
56 | |||
57 | $this->assertEquals(3, $items->count()); |
||
58 | |||
59 | $item = $items[0]; |
||
60 | $this->assertEquals('http://example.org/1', $item->Id); |
||
61 | $this->assertEquals('', $item->Link); |
||
62 | $this->assertEquals('2017-12-19 11:22:33', (string) $item->Date); |
||
63 | $this->assertEquals('Entry 1', $item->Title); |
||
64 | $this->assertEquals('', $item->Summary); |
||
65 | $this->assertEquals('', $item->Content); |
||
66 | |||
67 | $item = $items[1]; |
||
68 | $this->assertEquals('http://example.org/2', $item->Id); |
||
69 | $this->assertEquals('http://example.org/link/2', $item->Link); |
||
70 | $this->assertEquals('2017-12-19 22:33:44', (string) $item->Date); |
||
71 | $this->assertEquals('Entry 2', $item->Title); |
||
72 | $this->assertEquals('Summary 2', $item->Summary); |
||
73 | $this->assertTrue(strlen($item->Content) > 5); |
||
74 | |||
75 | $item = $items[2]; |
||
76 | $this->assertEquals('http://example.org/3', $item->Id); |
||
77 | $this->assertEquals('http://example.org/link/3', $item->Link); |
||
78 | $this->assertEquals('2017-12-19 03:44:55', (string) $item->Date); |
||
79 | $this->assertEquals('Entry 3', $item->Title); |
||
80 | $this->assertEquals('Summary 3', $item->Summary); |
||
81 | $this->assertEquals('Summary 3', $item->Content); |
||
82 | |||
83 | // Forth read: must still return sample2.atom (cached) |
||
84 | $items = $service->getItems(); |
||
85 | $this->assertEquals(3, $items->count()); |
||
86 | } |
||
203 |