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 RuntimeTest extends \PHPUnit_Framework_TestCase |
||
18 | { |
||
19 | /** |
||
20 | * @var Cache\Runtime |
||
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\Runtime::clear method. |
||
56 | * |
||
57 | * @return void |
||
58 | * |
||
59 | * @covers Joomla\Cache\Runtime::clear |
||
60 | * @since 1.0 |
||
61 | */ |
||
62 | public function testClear() |
||
63 | { |
||
64 | // Create a stub for the CacheItemInterface class. |
||
65 | $stub = $this->getMockBuilder('\\Psr\\Cache\\CacheItemInterface') |
||
66 | ->getMock(); |
||
67 | |||
68 | $stub->method('get') |
||
69 | ->willReturn('bar'); |
||
70 | |||
71 | $stub->method('getKey') |
||
72 | ->willReturn('foo'); |
||
73 | |||
74 | // Create a stub for the CacheItemInterface class. |
||
75 | $stub2 = $this->getMockBuilder('\\Psr\\Cache\\CacheItemInterface') |
||
76 | ->getMock(); |
||
77 | |||
78 | $stub2->method('get') |
||
79 | ->willReturn('car'); |
||
80 | |||
81 | $stub2->method('getKey') |
||
82 | ->willReturn('goo'); |
||
83 | |||
84 | $this->instance->save($stub); |
||
85 | $this->instance->save($stub2); |
||
86 | |||
87 | $this->assertEquals( |
||
88 | 'bar', |
||
89 | $this->instance->getItem('foo')->get(), |
||
90 | 'Checks first item was set.' |
||
91 | ); |
||
92 | |||
93 | $this->assertEquals( |
||
94 | 'car', |
||
95 | $this->instance->getItem('goo')->get(), |
||
96 | 'Checks second item was set.' |
||
97 | ); |
||
98 | |||
99 | $this->instance->clear(); |
||
100 | |||
101 | $this->assertNull( |
||
102 | $this->instance->getItem('foo')->get(), |
||
103 | 'Checks first item was cleared.' |
||
104 | ); |
||
105 | |||
106 | $this->assertNull( |
||
107 | $this->instance->getItem('goo')->get(), |
||
108 | 'Checks second item was cleared.' |
||
109 | ); |
||
110 | } |
||
111 | |||
112 | /** |
||
113 | * Tests the Joomla\Cache\Runtime::hasItem method. |
||
114 | * |
||
115 | * @return void |
||
116 | * |
||
117 | * @covers Joomla\Cache\Runtime::hasItem |
||
118 | * @since 1.0 |
||
119 | */ |
||
120 | public function testHasItem() |
||
121 | { |
||
122 | $this->assertFalse($this->instance->hasItem('foo')); |
||
123 | |||
124 | // Create a stub for the CacheItemInterface class. |
||
125 | $stub = $this->getMockBuilder('\\Psr\\Cache\\CacheItemInterface') |
||
126 | ->getMock(); |
||
127 | |||
128 | $stub->method('get') |
||
129 | ->willReturn('bar'); |
||
130 | |||
131 | $stub->method('getKey') |
||
132 | ->willReturn('foo'); |
||
133 | |||
134 | $this->instance->save($stub); |
||
135 | $this->assertTrue($this->instance->hasItem('foo')); |
||
136 | } |
||
137 | |||
138 | /** |
||
139 | * Tests the Joomla\Cache\Runtime::getItem method. |
||
140 | * |
||
141 | * @return void |
||
142 | * |
||
143 | * @covers Joomla\Cache\Runtime::getItem |
||
144 | * @since 1.0 |
||
145 | */ |
||
146 | public function testGetItem() |
||
147 | { |
||
148 | $item = $this->instance->getItem('foo'); |
||
149 | $this->assertNull($item->get()); |
||
150 | $this->assertFalse($item->isHit()); |
||
151 | |||
152 | // Create a stub for the CacheItemInterface class. |
||
153 | $stub = $this->getMockBuilder('\\Psr\\Cache\\CacheItemInterface') |
||
154 | ->getMock(); |
||
155 | |||
156 | $stub->method('get') |
||
157 | ->willReturn('bar'); |
||
158 | |||
159 | $stub->method('getKey') |
||
160 | ->willReturn('foo'); |
||
161 | |||
162 | $this->instance->save($stub); |
||
163 | $this->assertEquals('bar', $this->instance->getItem('foo')->get()); |
||
164 | } |
||
165 | |||
166 | /** |
||
167 | * Tests the Joomla\Cache\Runtime::deleteItem method. |
||
168 | * |
||
169 | * @return void |
||
170 | * |
||
171 | * @covers Joomla\Cache\Runtime::deleteItem |
||
172 | * @since 1.0 |
||
173 | */ |
||
174 | public function testDeleteItem() |
||
175 | { |
||
176 | // Create a stub for the CacheItemInterface class. |
||
177 | $stub = $this->getMockBuilder('\\Psr\\Cache\\CacheItemInterface') |
||
178 | ->getMock(); |
||
179 | |||
180 | $stub->method('get') |
||
181 | ->willReturn('bar'); |
||
182 | |||
183 | $stub->method('getKey') |
||
184 | ->willReturn('foo'); |
||
185 | |||
186 | $this->instance->save($stub); |
||
187 | $this->assertEquals('bar', $this->instance->getItem('foo')->get()); |
||
188 | |||
189 | $this->instance->deleteItem('foo'); |
||
190 | $this->assertNull($this->instance->getItem('foo')->get()); |
||
191 | } |
||
192 | |||
193 | /** |
||
194 | * Tests the Joomla\Cache\Runtime::save method. |
||
195 | * |
||
196 | * @return void |
||
197 | * |
||
198 | * @covers Joomla\Cache\Runtime::save |
||
199 | * @since 1.0 |
||
200 | */ |
||
201 | public function testSave() |
||
202 | { |
||
203 | // Create a stub for the CacheItemInterface class. |
||
204 | $stub = $this->getMockBuilder('\\Psr\\Cache\\CacheItemInterface') |
||
205 | ->getMock(); |
||
206 | |||
207 | $stub->method('get') |
||
208 | ->willReturn('bar'); |
||
209 | |||
210 | $stub->method('getKey') |
||
211 | ->willReturn('foo'); |
||
212 | |||
213 | $this->instance->save($stub); |
||
214 | $this->assertEquals('bar', $this->instance->getItem('foo')->get()); |
||
215 | } |
||
216 | |||
217 | /** |
||
218 | * Setup the tests. |
||
219 | * |
||
220 | * @return void |
||
221 | * |
||
222 | * @since 1.0 |
||
223 | */ |
||
224 | protected function setUp() |
||
225 | { |
||
226 | parent::setUp(); |
||
227 | |||
228 | try |
||
229 | { |
||
230 | $this->instance = new Cache\Runtime; |
||
231 | |||
232 | // Clear the internal store. |
||
233 | TestHelper::setValue($this->instance, 'store', array()); |
||
234 | } |
||
235 | catch (\Exception $e) |
||
236 | { |
||
237 | $this->markTestSkipped(); |
||
238 | } |
||
239 | } |
||
240 | } |
||
241 |