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 |
||
| 42 | class RedisLockStrategyTest extends FunctionalTestCase |
||
| 43 | { |
||
| 44 | /** |
||
| 45 | * @var LockFactory |
||
| 46 | */ |
||
| 47 | private $lockFactory; |
||
| 48 | private $redisHost; |
||
| 49 | private $redisDatabase; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @test |
||
| 53 | * @expectedException \TYPO3\CMS\Core\Locking\Exception |
||
| 54 | * @expectedExceptionMessage no configuration for redis lock strategy found |
||
| 55 | */ |
||
| 56 | public function shouldThrowExceptionBecauseConfigIsMissing() |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @test |
||
| 63 | * @expectedException \TYPO3\CMS\Core\Locking\Exception |
||
| 64 | * @expectedExceptionMessage no configuration for redis lock strategy found |
||
| 65 | */ |
||
| 66 | public function shouldThrowExceptionBecauseConfigIsNotAnArray() |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @test |
||
| 74 | * @expectedException \TYPO3\CMS\Core\Locking\Exception |
||
| 75 | * @expectedExceptionMessage no host for redis lock strategy found |
||
| 76 | */ |
||
| 77 | public function shouldThrowExceptionBecauseConfigHasNoHost() |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @test |
||
| 86 | * @expectedException \TYPO3\CMS\Core\Locking\Exception |
||
| 87 | * @expectedExceptionMessage no database for redis lock strategy found |
||
| 88 | */ |
||
| 89 | public function shouldThrowExceptionBecauseConfigHasNoDatabase() |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @test |
||
| 100 | */ |
||
| 101 | public function shouldConnectAndAcquireAExistingLock() |
||
| 113 | |||
| 114 | |||
| 115 | /** |
||
| 116 | * @test |
||
| 117 | */ |
||
| 118 | View Code Duplication | public function shouldConnectAndAcquireALock() |
|
| 130 | |||
| 131 | /** |
||
| 132 | * @test |
||
| 133 | */ |
||
| 134 | public function shouldConnectAndCheckIfLockIsAcquired() |
||
| 147 | |||
| 148 | /** |
||
| 149 | * @test |
||
| 150 | */ |
||
| 151 | View Code Duplication | public function shouldConnectAndDestroyALock() |
|
| 165 | |||
| 166 | /** |
||
| 167 | * @test |
||
| 168 | */ |
||
| 169 | View Code Duplication | public function shouldConnectAndDestroyANotExistingLock() |
|
| 181 | |||
| 182 | protected function setUp() |
||
| 183 | { |
||
| 184 | $this->testExtensionsToLoad[] = 'typo3conf/ext/redis_lock_strategy'; |
||
| 185 | $this->redisHost = getenv('typo3RedisHost'); |
||
| 186 | $this->redisDatabase = getenv('typo3RedisDatabase'); |
||
| 187 | |||
| 188 | parent::setUp(); |
||
| 189 | |||
| 190 | $this->lockFactory = GeneralUtility::makeInstance(LockFactory::class); |
||
| 191 | } |
||
| 192 | |||
| 193 | protected function tearDown() |
||
| 194 | { |
||
| 195 | parent::tearDown(); |
||
| 196 | |||
| 197 | $this->getRedisClient()->flushDB(); |
||
| 198 | } |
||
| 199 | |||
| 200 | /** |
||
| 201 | * @return \Redis |
||
| 202 | */ |
||
| 203 | private function getRedisClient() |
||
| 204 | { |
||
| 205 | $redis = new \Redis(); |
||
| 206 | $redis->connect($this->redisHost); |
||
| 207 | $redis->select($this->redisDatabase); |
||
| 208 | |||
| 209 | return $redis; |
||
| 210 | } |
||
| 211 | |||
| 212 | /** |
||
| 213 | * @param string $id Locker id |
||
| 214 | * |
||
| 215 | * @return \TYPO3\CMS\Core\Locking\LockingStrategyInterface |
||
| 216 | */ |
||
| 217 | private function getLocker($id) |
||
| 227 | |||
| 228 | } |
||
| 229 |