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 |
||
| 16 | class RedisTest extends \PHPUnit_Framework_TestCase |
||
| 17 | { |
||
| 18 | /** |
||
| 19 | * @var Cache\Redis |
||
| 20 | * @since 1.0 |
||
| 21 | */ |
||
| 22 | private $instance; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Tests for the correct Psr\Cache return values. |
||
| 26 | * |
||
| 27 | * @return void |
||
| 28 | * |
||
| 29 | * @coversNothing |
||
| 30 | * @since 1.0 |
||
| 31 | */ |
||
| 32 | public function testPsrCache() |
||
| 33 | { |
||
| 34 | $this->assertInternalType('boolean', $this->instance->clear(), 'Checking clear.'); |
||
| 35 | $this->assertInstanceOf('\Psr\Cache\CacheItemInterface', $this->instance->getItem('foo'), 'Checking getItem.'); |
||
| 36 | $this->assertInternalType('array', $this->instance->getItems(array('foo')), 'Checking getItems.'); |
||
| 37 | $this->assertInternalType('boolean', $this->instance->deleteItem('foo'), 'Checking deleteItem.'); |
||
| 38 | $this->assertInternalType('array', $this->instance->deleteItems(array('foo')), 'Checking deleteItems.'); |
||
| 39 | |||
| 40 | // Create a stub for the CacheItemInterface class. |
||
| 41 | $stub = $this->getMockBuilder('\\Psr\\Cache\\CacheItemInterface') |
||
| 42 | ->getMock(); |
||
| 43 | |||
| 44 | $stub->method('get') |
||
| 45 | ->willReturn('bar'); |
||
| 46 | |||
| 47 | $stub->method('getKey') |
||
| 48 | ->willReturn('foo'); |
||
| 49 | |||
| 50 | $this->assertInternalType('boolean', $this->instance->save($stub), 'Checking save.'); |
||
| 51 | } |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Tests the Joomla\Cache\Redis::get and Joomla\Cache\Redis::set methods. |
||
| 55 | * |
||
| 56 | * @return void |
||
| 57 | * |
||
| 58 | * @covers Joomla\Cache\Redis::getItem |
||
| 59 | * @covers Joomla\Cache\Redis::save |
||
| 60 | * @covers Joomla\Cache\Redis::connect |
||
| 61 | * @since 1.0 |
||
| 62 | */ |
||
| 63 | public function testGetAndSave() |
||
| 64 | { |
||
| 65 | // Create a stub for the CacheItemInterface class. |
||
| 66 | $stub = $this->getMockBuilder('\\Psr\\Cache\\CacheItemInterface') |
||
| 67 | ->getMock(); |
||
| 68 | |||
| 69 | $stub->method('get') |
||
| 70 | ->willReturn('bar'); |
||
| 71 | |||
| 72 | $stub->method('getKey') |
||
| 73 | ->willReturn('foo'); |
||
| 74 | |||
| 75 | $this->assertTrue( |
||
| 76 | $this->instance->save($stub), |
||
| 77 | 'Should store the data properly' |
||
| 78 | ); |
||
| 79 | |||
| 80 | $this->assertEquals( |
||
| 81 | 'bar', |
||
| 82 | $this->instance->getItem('foo')->get(), |
||
| 83 | 'Checking get' |
||
| 84 | ); |
||
| 85 | } |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Tests the Joomla\Cache\Redis::get and Joomla\Cache\Redis::set methods with timeout |
||
| 89 | * |
||
| 90 | * @return void |
||
| 91 | * |
||
| 92 | * @covers Joomla\Cache\Redis::getItem |
||
| 93 | * @covers Joomla\Cache\Redis::save |
||
| 94 | * @covers Joomla\Cache\Redis::connect |
||
| 95 | * @since 1.0 |
||
| 96 | */ |
||
| 97 | public function testGetAndSaveWithTimeout() |
||
| 98 | { |
||
| 99 | // Create a stub for the CacheItemInterface class. |
||
| 100 | $stub = $this->getMockBuilder('\\Joomla\\Cache\\Item\\AbstractItem') |
||
| 101 | ->getMock(); |
||
| 102 | |||
| 103 | $stub->method('get') |
||
| 104 | ->willReturn('bar'); |
||
| 105 | |||
| 106 | $stub->method('getKey') |
||
| 107 | ->willReturn('foo'); |
||
| 108 | |||
| 109 | $expireDate = new \DateTime; |
||
| 110 | $expireDate->setTimestamp(time() - 1); |
||
| 111 | $stub->method('getExpiration') |
||
| 112 | ->willReturn($expireDate); |
||
| 113 | |||
| 114 | $this->assertTrue( |
||
| 115 | $this->instance->save($stub), |
||
| 116 | 'Should store the data properly' |
||
| 117 | ); |
||
| 118 | |||
| 119 | sleep(2); |
||
| 120 | |||
| 121 | $this->assertFalse( |
||
| 122 | $this->instance->getItem('foo')->isHit(), |
||
| 123 | 'Checks expired get.' |
||
| 124 | ); |
||
| 125 | } |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Tests the Joomla\Cache\Redis::clear method. |
||
| 129 | * |
||
| 130 | * @return void |
||
| 131 | * |
||
| 132 | * @covers Joomla\Cache\Redis::clear |
||
| 133 | * @covers Joomla\Cache\Redis::connect |
||
| 134 | * @since 1.0 |
||
| 135 | */ |
||
| 136 | public function testClear() |
||
| 137 | { |
||
| 138 | // Create a stub for the CacheItemInterface class. |
||
| 139 | $stub = $this->getMockBuilder('\\Psr\\Cache\\CacheItemInterface') |
||
| 140 | ->getMock(); |
||
| 141 | |||
| 142 | $stub->method('get') |
||
| 143 | ->willReturn('bar'); |
||
| 144 | |||
| 145 | $stub->method('getKey') |
||
| 146 | ->willReturn('foo'); |
||
| 147 | |||
| 148 | // Create a stub for the CacheItemInterface class. |
||
| 149 | $stub2 = $this->getMockBuilder('\\Psr\\Cache\\CacheItemInterface') |
||
| 150 | ->getMock(); |
||
| 151 | |||
| 152 | $stub2->method('get') |
||
| 153 | ->willReturn('car'); |
||
| 154 | |||
| 155 | $stub2->method('getKey') |
||
| 156 | ->willReturn('boo'); |
||
| 157 | |||
| 158 | $this->instance->save($stub); |
||
| 159 | $this->instance->save($stub2); |
||
| 160 | |||
| 161 | $this->instance->clear(); |
||
| 162 | |||
| 163 | $this->assertFalse( |
||
| 164 | $this->instance->getItem('foo')->isHit(), |
||
| 165 | 'Item should have been removed' |
||
| 166 | ); |
||
| 167 | |||
| 168 | $this->assertFalse( |
||
| 169 | $this->instance->getItem('goo')->isHit(), |
||
| 170 | 'Item should have been removed' |
||
| 171 | ); |
||
| 172 | } |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Tests the Joomla\Cache\Redis::hasItem method. |
||
| 176 | * |
||
| 177 | * @return void |
||
| 178 | * |
||
| 179 | * @covers Joomla\Cache\Redis::connect |
||
| 180 | * @covers Joomla\Cache\Redis::hasItem |
||
| 181 | * @since 1.0 |
||
| 182 | */ |
||
| 183 | public function testHasItem() |
||
| 184 | { |
||
| 185 | $this->assertFalse( |
||
| 186 | $this->instance->hasItem('foo'), |
||
| 187 | 'Item should not exist' |
||
| 188 | ); |
||
| 189 | |||
| 190 | // Create a stub for the CacheItemInterface class. |
||
| 191 | $stub = $this->getMockBuilder('\\Psr\\Cache\\CacheItemInterface') |
||
| 192 | ->getMock(); |
||
| 193 | |||
| 194 | $stub->method('get') |
||
| 195 | ->willReturn('bar'); |
||
| 196 | |||
| 197 | $stub->method('getKey') |
||
| 198 | ->willReturn('foo'); |
||
| 199 | |||
| 200 | $this->instance->save($stub); |
||
| 201 | |||
| 202 | $this->assertTrue( |
||
| 203 | $this->instance->hasItem('foo'), |
||
| 204 | 'Item should exist' |
||
| 205 | ); |
||
| 206 | } |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Tests the Joomla\Cache\Redis::remove method. |
||
| 210 | * |
||
| 211 | * @return void |
||
| 212 | * |
||
| 213 | * @covers Joomla\Cache\Redis::connect |
||
| 214 | * @covers Joomla\Cache\Redis::deleteItem |
||
| 215 | * @since 1.0 |
||
| 216 | */ |
||
| 217 | |||
| 218 | public function testRemove() |
||
| 219 | { |
||
| 220 | // Create a stub for the CacheItemInterface class. |
||
| 221 | $stub = $this->getMockBuilder('\\Psr\\Cache\\CacheItemInterface') |
||
| 222 | ->getMock(); |
||
| 223 | |||
| 224 | $stub->method('get') |
||
| 225 | ->willReturn('bar'); |
||
| 226 | |||
| 227 | $stub->method('getKey') |
||
| 228 | ->willReturn('foo'); |
||
| 229 | |||
| 230 | $this->instance->save($stub); |
||
| 231 | $this->assertTrue( |
||
| 232 | $this->instance->getItem('foo')->isHit(), |
||
| 233 | 'Item should exist' |
||
| 234 | ); |
||
| 235 | |||
| 236 | $this->instance->deleteItem('foo'); |
||
| 237 | |||
| 238 | $this->assertFalse( |
||
| 239 | $this->instance->getItem('foo')->isHit(), |
||
| 240 | 'Item should have been removed' |
||
| 241 | ); |
||
| 242 | } |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Tests the Joomla\Cache\Redis::getItems method. |
||
| 246 | * |
||
| 247 | * @return void |
||
| 248 | * |
||
| 249 | * @covers Joomla\Cache\Redis::getItems |
||
| 250 | * @since 1.0 |
||
| 251 | */ |
||
| 252 | public function testGetItems() |
||
| 253 | { |
||
| 254 | // Create a stub for the CacheItemInterface class. |
||
| 255 | $stub = $this->getMockBuilder('\\Psr\\Cache\\CacheItemInterface') |
||
| 256 | ->getMock(); |
||
| 257 | |||
| 258 | $stub->method('get') |
||
| 259 | ->willReturn('bar'); |
||
| 260 | |||
| 261 | $stub->method('getKey') |
||
| 262 | ->willReturn('foo'); |
||
| 263 | |||
| 264 | // Create a stub for the CacheItemInterface class. |
||
| 265 | $stub2 = $this->getMockBuilder('\\Psr\\Cache\\CacheItemInterface') |
||
| 266 | ->getMock(); |
||
| 267 | |||
| 268 | $stub2->method('get') |
||
| 269 | ->willReturn('bar'); |
||
| 270 | |||
| 271 | $stub2->method('getKey') |
||
| 272 | ->willReturn('boo'); |
||
| 273 | |||
| 274 | $this->instance->save($stub); |
||
| 275 | $this->instance->save($stub2); |
||
| 276 | |||
| 277 | $fooResult = $this->instance->getItems(array('foo', 'boo')); |
||
| 278 | |||
| 279 | $this->assertArrayHasKey('foo', $fooResult, 'Missing array key'); |
||
| 280 | $this->assertArrayHasKey('boo', $fooResult, 'Missing array key'); |
||
| 281 | |||
| 282 | $this->assertInstanceOf( |
||
| 283 | 'Joomla\Cache\Item\Item', |
||
| 284 | $fooResult['foo'], |
||
| 285 | 'Expected instance of Joomla\Cache\Item\Item' |
||
| 286 | ); |
||
| 287 | $this->assertInstanceOf( |
||
| 288 | 'Joomla\Cache\Item\Item', |
||
| 289 | $fooResult['boo'], |
||
| 290 | 'Expected instance of Joomla\Cache\Item\Item' |
||
| 291 | ); |
||
| 292 | |||
| 293 | $this->assertTrue( |
||
| 294 | $fooResult['foo']->isHit(), |
||
| 295 | 'Item should be returned from cache' |
||
| 296 | ); |
||
| 297 | $this->assertTrue( |
||
| 298 | $fooResult['boo']->isHit(), |
||
| 299 | 'Item should be returned from cache' |
||
| 300 | ); |
||
| 301 | } |
||
| 302 | |||
| 303 | /** |
||
| 304 | * Tests the Joomla\Cache\Redis::deleteItems method. |
||
| 305 | * |
||
| 306 | * @return void |
||
| 307 | * |
||
| 308 | * @covers Joomla\Cache\Redis::deleteItems |
||
| 309 | * @since 1.0 |
||
| 310 | */ |
||
| 311 | public function testDeleteItems() |
||
| 312 | { |
||
| 313 | // Create a stub for the CacheItemInterface class. |
||
| 314 | $stub = $this->getMockBuilder('\\Psr\\Cache\\CacheItemInterface') |
||
| 315 | ->getMock(); |
||
| 316 | |||
| 317 | $stub->method('get') |
||
| 318 | ->willReturn('bar'); |
||
| 319 | |||
| 320 | $stub->method('getKey') |
||
| 321 | ->willReturn('foo'); |
||
| 322 | |||
| 323 | // Create a stub for the CacheItemInterface class. |
||
| 324 | $stub2 = $this->getMockBuilder('\\Psr\\Cache\\CacheItemInterface') |
||
| 325 | ->getMock(); |
||
| 326 | |||
| 327 | $stub2->method('get') |
||
| 328 | ->willReturn('bar'); |
||
| 329 | |||
| 330 | $stub2->method('getKey') |
||
| 331 | ->willReturn('boo'); |
||
| 332 | |||
| 333 | $this->instance->save($stub); |
||
| 334 | $this->instance->save($stub2); |
||
| 335 | |||
| 336 | $this->instance->deleteItems(array('foo', 'boo')); |
||
| 337 | |||
| 338 | $this->assertFalse( |
||
| 339 | $this->instance->getItem('foo')->isHit(), |
||
| 340 | 'Item should have been removed' |
||
| 341 | ); |
||
| 342 | $this->assertFalse( |
||
| 343 | $this->instance->getItem('boo')->isHit(), |
||
| 344 | 'Item should have been removed' |
||
| 345 | ); |
||
| 346 | } |
||
| 347 | |||
| 348 | /** |
||
| 349 | * Setup the tests. |
||
| 350 | * |
||
| 351 | * @return void |
||
| 352 | * |
||
| 353 | * @covers Joomla\Cache\Redis::__construct |
||
| 354 | * @since 1.0 |
||
| 355 | */ |
||
| 356 | protected function setUp() |
||
| 357 | { |
||
| 358 | parent::setUp(); |
||
| 359 | |||
| 360 | try |
||
| 361 | { |
||
| 362 | $this->instance = new Cache\Redis; |
||
| 363 | } |
||
| 364 | catch (\Exception $e) |
||
| 365 | { |
||
| 366 | $this->markTestSkipped($e->getMessage()); |
||
| 367 | } |
||
| 368 | } |
||
| 369 | |||
| 370 | /** |
||
| 371 | * Flush all data before each test |
||
| 372 | * |
||
| 373 | * @return void |
||
| 374 | * |
||
| 375 | * @since 1.0 |
||
| 376 | */ |
||
| 377 | protected function assertPreConditions() |
||
| 378 | { |
||
| 379 | if ($this->instance) |
||
| 380 | { |
||
| 381 | $this->instance->clear(); |
||
| 382 | } |
||
| 383 | } |
||
| 384 | |||
| 385 | /** |
||
| 386 | * Teardown the test. |
||
| 387 | * |
||
| 388 | * @return void |
||
| 389 | * |
||
| 390 | * @since 1.0 |
||
| 391 | */ |
||
| 392 | protected function tearDown() |
||
| 393 | { |
||
| 394 | if ($this->instance) |
||
| 395 | { |
||
| 396 | $this->instance->clear(); |
||
| 397 | } |
||
| 398 | } |
||
| 399 | } |
||
| 400 |