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 ApcuTest extends CacheTest |
||
| 17 | { |
||
| 18 | /** |
||
| 19 | * Tests for the correct Psr\Cache return values. |
||
| 20 | * |
||
| 21 | * @return void |
||
| 22 | * |
||
| 23 | * @coversNothing |
||
| 24 | * @since 1.0 |
||
| 25 | */ |
||
| 26 | public function testPsrCache() |
||
| 27 | { |
||
| 28 | $this->assertInternalType('boolean', $this->instance->clear(), 'Checking clear.'); |
||
| 29 | $this->assertInstanceOf('\Psr\Cache\CacheItemInterface', $this->instance->getItem('foo'), 'Checking getItem.'); |
||
| 30 | $this->assertInternalType('array', $this->instance->getItems(array('foo')), 'Checking getItems.'); |
||
| 31 | $this->assertInternalType('boolean', $this->instance->deleteItem('foo'), 'Checking deleteItem.'); |
||
| 32 | $this->assertInternalType('array', $this->instance->deleteItems(array('foo')), 'Checking deleteItems.'); |
||
| 33 | |||
| 34 | // Create a stub for the CacheItemInterface class. |
||
| 35 | $stub = $this->getMockBuilder('\\Psr\\Cache\\CacheItemInterface') |
||
| 36 | ->getMock(); |
||
| 37 | |||
| 38 | $stub->method('get') |
||
| 39 | ->willReturn('bar'); |
||
| 40 | |||
| 41 | $stub->method('getKey') |
||
| 42 | ->willReturn('foo'); |
||
| 43 | |||
| 44 | $this->assertInternalType('boolean', $this->instance->save($stub), 'Checking save.'); |
||
| 45 | } |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Tests the Joomla\Cache\Apcu::clear method. |
||
| 49 | * |
||
| 50 | * @return void |
||
| 51 | * |
||
| 52 | * @covers Joomla\Cache\Apcu::clear |
||
| 53 | * @since 1.0 |
||
| 54 | */ |
||
| 55 | public function testClear() |
||
| 56 | { |
||
| 57 | $this->assertTrue($this->instance->clear()); |
||
| 58 | } |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Tests the Joomla\Cache\Apcu::hasItem method. |
||
| 62 | * |
||
| 63 | * @return void |
||
| 64 | * |
||
| 65 | * @covers Joomla\Cache\Apcu::hasItem |
||
| 66 | * @since 1.0 |
||
| 67 | */ |
||
| 68 | public function testHasItem() |
||
| 69 | { |
||
| 70 | $this->assertTrue($this->instance->hasItem('foo')); |
||
| 71 | } |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Tests the Joomla\Cache\Apcu::get method. |
||
| 75 | * |
||
| 76 | * @return void |
||
| 77 | * |
||
| 78 | * @covers Joomla\Cache\Apcu::get |
||
| 79 | * @since 1.0 |
||
| 80 | */ |
||
| 81 | public function testGet() |
||
| 82 | { |
||
| 83 | $this->assertInstanceOf( |
||
| 84 | '\Psr\Cache\CacheItemInterface', |
||
| 85 | $this->instance->getItem('foo') |
||
| 86 | ); |
||
| 87 | } |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Tests the Joomla\Cache\Apcu::remove method. |
||
| 91 | * |
||
| 92 | * @return void |
||
| 93 | * |
||
| 94 | * @covers Joomla\Cache\Apcu::remove |
||
| 95 | * @since 1.0 |
||
| 96 | */ |
||
| 97 | public function testRemove() |
||
| 98 | { |
||
| 99 | $this->assertTrue($this->instance->deleteItem('foo')); |
||
| 100 | } |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Tests the Joomla\Cache\Apcu::save method. |
||
| 104 | * |
||
| 105 | * @return void |
||
| 106 | * |
||
| 107 | * @covers Joomla\Cache\Apcu::save |
||
| 108 | * @since 1.0 |
||
| 109 | */ |
||
| 110 | public function testSave() |
||
| 111 | { |
||
| 112 | // Create a stub for the CacheItemInterface class. |
||
| 113 | $stub = $this->getMockBuilder('\\Psr\\Cache\\CacheItemInterface') |
||
| 114 | ->getMock(); |
||
| 115 | |||
| 116 | $stub->method('get') |
||
| 117 | ->willReturn('car'); |
||
| 118 | |||
| 119 | $stub->method('getKey') |
||
| 120 | ->willReturn('boo'); |
||
| 121 | |||
| 122 | $this->assertTrue($this->instance->save($stub)); |
||
| 123 | } |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Setup the tests. |
||
| 127 | * |
||
| 128 | * @return void |
||
| 129 | * |
||
| 130 | * @since 1.0 |
||
| 131 | */ |
||
| 132 | protected function setUp() |
||
| 133 | { |
||
| 134 | $this->cacheClass = 'Joomla\\Cache\\Apcu'; |
||
| 135 | |||
| 136 | try |
||
| 137 | { |
||
| 138 | parent::setUp(); |
||
| 139 | |||
| 140 | // Create a stub for the CacheItemInterface class. |
||
| 141 | $stub = $this->getMockBuilder('\\Psr\\Cache\\CacheItemInterface') |
||
| 142 | ->getMock(); |
||
| 143 | |||
| 144 | $stub->method('get') |
||
| 145 | ->willReturn('bar'); |
||
| 146 | |||
| 147 | $stub->method('getKey') |
||
| 148 | ->willReturn('foo'); |
||
| 149 | |||
| 150 | $this->instance->save($stub); |
||
| 151 | } |
||
| 152 | catch (\Exception $e) |
||
| 153 | { |
||
| 154 | $this->markTestSkipped(); |
||
| 155 | } |
||
| 156 | } |
||
| 157 | } |
||
| 158 |