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 |
||
| 19 | class ContentLanguageHandlerTest extends HandlerTest |
||
| 20 | { |
||
| 21 | /** |
||
| 22 | * @covers \eZ\Publish\Core\Persistence\Cache\ContentLanguageHandler::create |
||
| 23 | */ |
||
| 24 | public function testCreate() |
||
| 25 | { |
||
| 26 | $this->loggerMock->expects($this->once())->method('logCall'); |
||
| 27 | $cacheItemMock = $this->createMock(ItemInterface::class); |
||
| 28 | $this->cacheMock |
||
| 29 | ->expects($this->once()) |
||
| 30 | ->method('getItem') |
||
| 31 | ->with('language', 2) |
||
| 32 | ->will($this->returnValue($cacheItemMock)); |
||
| 33 | |||
| 34 | $innerHandlerMock = $this->createMock(Handler::class); |
||
| 35 | $this->persistenceHandlerMock |
||
| 36 | ->expects($this->once()) |
||
| 37 | ->method('contentLanguageHandler') |
||
| 38 | ->will($this->returnValue($innerHandlerMock)); |
||
| 39 | |||
| 40 | $innerHandlerMock |
||
| 41 | ->expects($this->once()) |
||
| 42 | ->method('create') |
||
| 43 | ->with($this->isInstanceOf(SPILanguageCreateStruct::class)) |
||
| 44 | ->will( |
||
| 45 | $this->returnValue( |
||
| 46 | new SPILanguage( |
||
| 47 | array('id' => 2, 'name' => 'English (UK)', 'languageCode' => 'eng-GB') |
||
| 48 | ) |
||
| 49 | ) |
||
| 50 | ); |
||
| 51 | |||
| 52 | $cacheItemMock |
||
| 53 | ->expects($this->once()) |
||
| 54 | ->method('set') |
||
| 55 | ->with($this->isInstanceOf(SPILanguage::class)) |
||
| 56 | ->will($this->returnValue($cacheItemMock)); |
||
| 57 | |||
| 58 | $cacheItemMock |
||
| 59 | ->expects($this->once()) |
||
| 60 | ->method('save') |
||
| 61 | ->with(); |
||
| 62 | |||
| 63 | $cacheItemMock |
||
| 64 | ->expects($this->never()) |
||
| 65 | ->method('get'); |
||
| 66 | |||
| 67 | $handler = $this->persistenceCacheHandler->contentLanguageHandler(); |
||
| 68 | $handler->create(new SPILanguageCreateStruct()); |
||
| 69 | } |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @covers \eZ\Publish\Core\Persistence\Cache\ContentLanguageHandler::delete |
||
| 73 | */ |
||
| 74 | public function testDelete() |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @covers \eZ\Publish\Core\Persistence\Cache\ContentLanguageHandler::load |
||
| 103 | */ |
||
| 104 | public function testLoadCacheIsMiss() |
||
| 105 | { |
||
| 106 | $this->loggerMock->expects($this->once())->method('logCall'); |
||
| 107 | $cacheItemMock = $this->createMock(ItemInterface::class); |
||
| 108 | $this->cacheMock |
||
| 109 | ->expects($this->once()) |
||
| 110 | ->method('getItem') |
||
| 111 | ->with('language', 2) |
||
| 112 | ->will($this->returnValue($cacheItemMock)); |
||
| 113 | |||
| 114 | $cacheItemMock |
||
| 115 | ->expects($this->once()) |
||
| 116 | ->method('get') |
||
| 117 | ->will($this->returnValue(null)); |
||
| 118 | |||
| 119 | $cacheItemMock |
||
| 120 | ->expects($this->once()) |
||
| 121 | ->method('isMiss') |
||
| 122 | ->will($this->returnValue(true)); |
||
| 123 | |||
| 124 | $innerHandlerMock = $this->createMock(Handler::class); |
||
| 125 | $this->persistenceHandlerMock |
||
| 126 | ->expects($this->once()) |
||
| 127 | ->method('contentLanguageHandler') |
||
| 128 | ->will($this->returnValue($innerHandlerMock)); |
||
| 129 | |||
| 130 | $innerHandlerMock |
||
| 131 | ->expects($this->once()) |
||
| 132 | ->method('load') |
||
| 133 | ->with(2) |
||
| 134 | ->will( |
||
| 135 | $this->returnValue( |
||
| 136 | new SPILanguage( |
||
| 137 | array('id' => 2, 'name' => 'English (UK)', 'languageCode' => 'eng-GB') |
||
| 138 | ) |
||
| 139 | ) |
||
| 140 | ); |
||
| 141 | |||
| 142 | $cacheItemMock |
||
| 143 | ->expects($this->once()) |
||
| 144 | ->method('set') |
||
| 145 | ->with($this->isInstanceOf(SPILanguage::class)) |
||
| 146 | ->will($this->returnValue($cacheItemMock)); |
||
| 147 | |||
| 148 | $cacheItemMock |
||
| 149 | ->expects($this->once()) |
||
| 150 | ->method('save') |
||
| 151 | ->with(); |
||
| 152 | |||
| 153 | $handler = $this->persistenceCacheHandler->contentLanguageHandler(); |
||
| 154 | $handler->load(2); |
||
| 155 | } |
||
| 156 | |||
| 157 | /** |
||
| 158 | * @covers \eZ\Publish\Core\Persistence\Cache\ContentLanguageHandler::load |
||
| 159 | */ |
||
| 160 | View Code Duplication | public function testLoadHasCache() |
|
| 197 | |||
| 198 | /** |
||
| 199 | * @covers \eZ\Publish\Core\Persistence\Cache\ContentLanguageHandler::loadAll |
||
| 200 | */ |
||
| 201 | public function testLoadAll() |
||
| 222 | |||
| 223 | /** |
||
| 224 | * @covers \eZ\Publish\Core\Persistence\Cache\ContentLanguageHandler::loadByLanguageCode |
||
| 225 | */ |
||
| 226 | View Code Duplication | public function testLoadByLanguageCode() |
|
| 227 | { |
||
| 228 | $this->loggerMock->expects($this->once())->method('logCall'); |
||
| 229 | $this->cacheMock |
||
| 230 | ->expects($this->never()) |
||
| 231 | ->method($this->anything()); |
||
| 232 | |||
| 233 | $innerHandler = $this->createMock(Handler::class); |
||
| 234 | $this->persistenceHandlerMock |
||
| 235 | ->expects($this->once()) |
||
| 236 | ->method('contentLanguageHandler') |
||
| 237 | ->will($this->returnValue($innerHandler)); |
||
| 238 | |||
| 239 | $innerHandler |
||
| 240 | ->expects($this->once()) |
||
| 241 | ->method('loadByLanguageCode') |
||
| 242 | ->with('eng-GB') |
||
| 243 | ->will( |
||
| 244 | $this->returnValue( |
||
| 245 | new SPILanguage( |
||
| 246 | array('id' => 2, 'name' => 'English (UK)', 'languageCode' => 'eng-GB') |
||
| 247 | ) |
||
| 248 | ) |
||
| 249 | ); |
||
| 250 | |||
| 251 | $handler = $this->persistenceCacheHandler->contentLanguageHandler(); |
||
| 252 | $handler->loadByLanguageCode('eng-GB'); |
||
| 253 | } |
||
| 254 | |||
| 255 | /** |
||
| 256 | * @covers \eZ\Publish\Core\Persistence\Cache\ContentLanguageHandler::update |
||
| 257 | */ |
||
| 258 | public function testUpdate() |
||
| 288 | } |
||
| 289 |