Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
17 | class NoneTest extends \PHPUnit_Framework_TestCase |
||
18 | { |
||
19 | /** |
||
20 | * @var Cache\None |
||
21 | * @since 1.0 |
||
22 | */ |
||
23 | private $instance; |
||
24 | |||
25 | /** |
||
26 | * Tests for the correct Psr\Cache return values. |
||
27 | * |
||
28 | * @return void |
||
29 | * |
||
30 | * @coversNothing |
||
31 | * @since 1.0 |
||
32 | */ |
||
33 | public function testPsrCache() |
||
34 | { |
||
35 | $this->assertInternalType('boolean', $this->instance->clear(), 'Checking clear.'); |
||
36 | $this->assertInstanceOf('\Psr\Cache\CacheItemInterface', $this->instance->getItem('foo'), 'Checking get.'); |
||
37 | $this->assertInternalType('array', $this->instance->getItems(array('foo')), 'Checking getMultiple.'); |
||
38 | $this->assertInternalType('boolean', $this->instance->deleteItem('foo'), 'Checking remove.'); |
||
39 | $this->assertInternalType('array', $this->instance->deleteItems(array('foo')), 'Checking removeMultiple.'); |
||
40 | |||
41 | // Create a stub for the CacheItemInterface class. |
||
42 | $stub = $this->getMockBuilder('\\Psr\\Cache\\CacheItemInterface') |
||
43 | ->getMock(); |
||
44 | |||
45 | $stub->method('get') |
||
46 | ->willReturn('bar'); |
||
47 | |||
48 | $stub->method('getKey') |
||
49 | ->willReturn('foo'); |
||
50 | |||
51 | $this->assertInternalType('boolean', $this->instance->save($stub), 'Checking set.'); |
||
52 | } |
||
53 | |||
54 | /** |
||
55 | * Tests the Joomla\Cache\None::clear method. |
||
56 | * |
||
57 | * @return void |
||
58 | * |
||
59 | * @covers Joomla\Cache\None::clear |
||
60 | * @since 1.0 |
||
61 | */ |
||
62 | public function testClear() |
||
63 | { |
||
64 | $this->instance->clear(); |
||
65 | } |
||
66 | |||
67 | /** |
||
68 | * Tests the Joomla\Cache\None::getItem method. |
||
69 | * |
||
70 | * @return void |
||
71 | * |
||
72 | * @covers Joomla\Cache\None::getItem |
||
73 | * @since 1.0 |
||
74 | */ |
||
75 | public function testGetItem() |
||
76 | { |
||
77 | // Create a stub for the CacheItemInterface class. |
||
78 | $stub = $this->getMockBuilder('\\Psr\\Cache\\CacheItemInterface') |
||
79 | ->getMock(); |
||
80 | |||
81 | $stub->method('get') |
||
82 | ->willReturn('bar'); |
||
83 | |||
84 | $stub->method('getKey') |
||
85 | ->willReturn('foo'); |
||
86 | |||
87 | $this->instance->save($stub); |
||
88 | $item = $this->instance->getItem('foo'); |
||
89 | $this->assertNull($item->get()); |
||
90 | $this->assertFalse($item->isHit()); |
||
91 | } |
||
92 | |||
93 | /** |
||
94 | * Tests the Joomla\Cache\None::deleteItem method. |
||
95 | * |
||
96 | * @return void |
||
97 | * |
||
98 | * @covers Joomla\Cache\None::deleteItem |
||
99 | * @since 1.0 |
||
100 | */ |
||
101 | public function testDeleteItem() |
||
102 | { |
||
103 | $this->instance->deleteItem('foo'); |
||
104 | } |
||
105 | |||
106 | /** |
||
107 | * Tests the Joomla\Cache\None::save method. |
||
108 | * |
||
109 | * @return void |
||
110 | * |
||
111 | * @covers Joomla\Cache\None::save |
||
112 | * @since 1.0 |
||
113 | */ |
||
114 | public function testSet() |
||
115 | { |
||
116 | // Create a stub for the CacheItemInterface class. |
||
117 | $stub = $this->getMockBuilder('\\Psr\\Cache\\CacheItemInterface') |
||
118 | ->getMock(); |
||
119 | |||
120 | $stub->method('get') |
||
121 | ->willReturn('bar'); |
||
122 | |||
123 | $stub->method('getKey') |
||
124 | ->willReturn('foo'); |
||
125 | |||
126 | $this->instance->save($stub); |
||
127 | $item = $this->instance->getItem('foo'); |
||
128 | $this->assertNull($item->get()); |
||
129 | $this->assertFalse($item->isHit()); |
||
130 | } |
||
131 | |||
132 | /** |
||
133 | * Tests the Joomla\Cache\None::hasItem method. |
||
134 | * |
||
135 | * @return void |
||
136 | * |
||
137 | * @covers Joomla\Cache\None::hasItem |
||
138 | * @since 1.0 |
||
139 | */ |
||
140 | public function testHasItem() |
||
141 | { |
||
142 | $this->assertFalse($this->instance->hasItem('foo')); |
||
143 | |||
144 | // Create a stub for the CacheItemInterface class. |
||
145 | $stub = $this->getMockBuilder('\\Psr\\Cache\\CacheItemInterface') |
||
146 | ->getMock(); |
||
147 | |||
148 | $stub->method('get') |
||
149 | ->willReturn('bar'); |
||
150 | |||
151 | $stub->method('getKey') |
||
152 | ->willReturn('foo'); |
||
153 | |||
154 | $this->instance->save($stub); |
||
155 | $this->assertFalse($this->instance->hasItem('foo')); |
||
156 | } |
||
157 | |||
158 | /** |
||
159 | * Setup the tests. |
||
160 | * |
||
161 | * @return void |
||
162 | * |
||
163 | * @since 1.0 |
||
164 | */ |
||
165 | protected function setUp() |
||
166 | { |
||
167 | parent::setUp(); |
||
168 | |||
169 | try |
||
170 | { |
||
171 | $this->instance = new Cache\None; |
||
172 | } |
||
173 | catch (\Exception $e) |
||
174 | { |
||
175 | $this->markTestSkipped(); |
||
176 | } |
||
177 | } |
||
178 | } |
||
179 |