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 |
||
| 28 | class DatabaseActionTest extends TestCase |
||
| 29 | { |
||
| 30 | /** |
||
| 31 | * @var DatabaseAction |
||
| 32 | */ |
||
| 33 | protected $action; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var \PHPUnit_Framework_MockObject_MockObject |
||
| 37 | */ |
||
| 38 | protected $factory; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var \PHPUnit_Framework_MockObject_MockObject |
||
| 42 | */ |
||
| 43 | protected $repository; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var \PHPUnit_Framework_MockObject_MockObject |
||
| 47 | */ |
||
| 48 | protected $ezRepository; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var \PHPUnit_Framework_MockObject_MockObject |
||
| 52 | */ |
||
| 53 | protected $contentType; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @var \PHPUnit_Framework_MockObject_MockObject |
||
| 57 | */ |
||
| 58 | protected $contentService; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @var array |
||
| 62 | */ |
||
| 63 | protected $fields; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @var InformationCollectionStruct |
||
| 67 | */ |
||
| 68 | protected $struct; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @var LegacyData |
||
| 72 | */ |
||
| 73 | protected $legacyData; |
||
| 74 | |||
| 75 | public function setUp() |
||
| 76 | { |
||
| 77 | $this->factory = $this->getMockBuilder(FieldDataFactory::class) |
||
| 78 | ->disableOriginalConstructor() |
||
| 79 | ->setMethods(array('getLegacyValue')) |
||
| 80 | ->getMock(); |
||
| 81 | |||
| 82 | $this->repository = $this->getMockBuilder(RepositoryAggregate::class) |
||
| 83 | ->disableOriginalConstructor() |
||
| 84 | ->setMethods(array('createChild', 'createMain')) |
||
| 85 | ->getMock(); |
||
| 86 | |||
| 87 | $this->ezRepository = $this->getMockBuilder(Repository::class) |
||
| 88 | ->disableOriginalConstructor() |
||
| 89 | ->setMethods(array('getContentService', 'getCurrentUser')) |
||
| 90 | ->getMock(); |
||
| 91 | |||
| 92 | $this->contentType = new ContentType(array( |
||
| 93 | 'fieldDefinitions' => array( |
||
| 94 | new FieldDefinition(array( |
||
| 95 | 'identifier' => 'some_field', |
||
| 96 | 'id' => 321, |
||
| 97 | )), |
||
| 98 | new FieldDefinition(array( |
||
| 99 | 'identifier' => 'some_field_1', |
||
| 100 | 'id' => 654, |
||
| 101 | )), |
||
| 102 | new FieldDefinition(array( |
||
| 103 | 'identifier' => 'some_field_2', |
||
| 104 | 'id' => 987, |
||
| 105 | )), |
||
| 106 | ), |
||
| 107 | )); |
||
| 108 | |||
| 109 | $this->contentService = $this->getMockBuilder(ContentService::class) |
||
| 110 | ->disableOriginalConstructor() |
||
| 111 | ->setMethods(array('loadContent')) |
||
| 112 | ->getMock(); |
||
| 113 | |||
| 114 | $this->fields = array( |
||
| 115 | new Field(array( |
||
| 116 | 'id' => 123, |
||
| 117 | 'fieldDefIdentifier' => 'some_field', |
||
| 118 | 'value' => new TextLineValue('some value'), |
||
| 119 | 'languageCode' => 'eng_GB', |
||
| 120 | )), |
||
| 121 | new Field(array( |
||
| 122 | 'id' => 456, |
||
| 123 | 'fieldDefIdentifier' => 'some_field_1', |
||
| 124 | 'value' => new TextLineValue('some value 1'), |
||
| 125 | 'languageCode' => 'eng_GB', |
||
| 126 | )), |
||
| 127 | new Field(array( |
||
| 128 | 'id' => 789, |
||
| 129 | 'fieldDefIdentifier' => 'some_field_2', |
||
| 130 | 'value' => new TextLineValue('some value 2'), |
||
| 131 | 'languageCode' => 'eng_GB', |
||
| 132 | )), |
||
| 133 | ); |
||
| 134 | |||
| 135 | $this->struct = new InformationCollectionStruct(); |
||
| 136 | foreach ($this->fields as $field) { |
||
| 137 | $this->struct->setCollectedFieldValue($field->fieldDefIdentifier, $field->value); |
||
| 138 | } |
||
| 139 | |||
| 140 | $this->legacyData = new LegacyData( |
||
| 141 | array( |
||
| 142 | 'contentClassAttributeId' => 123, |
||
| 143 | 'dataInt' => 0, |
||
| 144 | 'dataFloat' => 0.0, |
||
| 145 | 'dataText' => 'some value', |
||
| 146 | ) |
||
| 147 | ); |
||
| 148 | |||
| 149 | $this->action = new DatabaseAction($this->factory, $this->repository, $this->ezRepository); |
||
| 150 | parent::setUp(); |
||
| 151 | } |
||
| 152 | |||
| 153 | public function testAct() |
||
| 154 | { |
||
| 155 | $location = new Location(array( |
||
| 156 | 'contentInfo' => new ContentInfo(array( |
||
| 157 | 'id' => 123, |
||
| 158 | )), |
||
| 159 | )); |
||
| 160 | |||
| 161 | $content = new Content(array( |
||
| 162 | 'internalFields' => $this->fields, |
||
| 163 | 'versionInfo' => new VersionInfo(array( |
||
| 164 | 'contentInfo' => new ContentInfo(array( |
||
| 165 | 'mainLanguageCode' => 'eng_GB', |
||
| 166 | )), |
||
| 167 | )), |
||
| 168 | )); |
||
| 169 | |||
| 170 | $dataWrapper = new DataWrapper($this->struct, $this->contentType, $location); |
||
| 171 | $event = new InformationCollected($dataWrapper); |
||
| 172 | |||
| 173 | $user = new User(array( |
||
| 174 | 'content' => new Content(array( |
||
| 175 | 'versionInfo' => new VersionInfo(array( |
||
| 176 | 'contentInfo' => new ContentInfo(array( |
||
| 177 | 'id' => 123, |
||
| 178 | )), |
||
| 179 | )), |
||
| 180 | )), |
||
| 181 | 'login' => 'login', |
||
| 182 | )); |
||
| 183 | |||
| 184 | $ezInfoCollection = new EzInfoCollection(); |
||
| 185 | $ezInfoCollectionAttribute = new EzInfoCollectionAttribute(); |
||
| 186 | |||
| 187 | $this->ezRepository->expects($this->once()) |
||
| 188 | ->method('getContentService') |
||
| 189 | ->willReturn($this->contentService); |
||
| 190 | |||
| 191 | $this->contentService->expects($this->once()) |
||
| 192 | ->method('loadContent') |
||
| 193 | ->with(123) |
||
| 194 | ->willReturn($content); |
||
| 195 | |||
| 196 | $this->ezRepository->expects($this->once()) |
||
| 197 | ->method('getCurrentUser') |
||
| 198 | ->willReturn($user); |
||
| 199 | |||
| 200 | $this->repository->expects($this->once()) |
||
| 201 | ->method('createMain') |
||
| 202 | ->willReturn($ezInfoCollection); |
||
| 203 | |||
| 204 | $this->factory->expects($this->exactly(3)) |
||
| 205 | ->method('getLegacyValue') |
||
| 206 | ->withAnyParameters() |
||
| 207 | ->willReturn($this->legacyData); |
||
| 208 | |||
| 209 | $this->repository->expects($this->exactly(3)) |
||
| 210 | ->method('createChild') |
||
| 211 | ->willReturn($ezInfoCollectionAttribute); |
||
| 212 | |||
| 213 | $this->action->act($event); |
||
| 214 | } |
||
| 215 | |||
| 216 | /** |
||
| 217 | * @expectedException \Netgen\Bundle\InformationCollectionBundle\Exception\ActionFailedException |
||
| 218 | */ |
||
| 219 | public function testActWithExceptionOnInformationCollectionRepository() |
||
| 220 | { |
||
| 221 | $location = new Location(array( |
||
| 222 | 'contentInfo' => new ContentInfo(array( |
||
| 223 | 'id' => 123, |
||
| 224 | )), |
||
| 225 | )); |
||
| 226 | |||
| 227 | $content = new Content(array( |
||
| 228 | 'internalFields' => $this->fields, |
||
| 229 | 'versionInfo' => new VersionInfo(array( |
||
| 230 | 'contentInfo' => new ContentInfo(array( |
||
| 231 | 'mainLanguageCode' => 'eng_GB', |
||
| 232 | )), |
||
| 233 | )), |
||
| 234 | )); |
||
| 235 | |||
| 236 | $dataWrapper = new DataWrapper($this->struct, $this->contentType, $location); |
||
| 237 | $event = new InformationCollected($dataWrapper); |
||
| 238 | |||
| 239 | $user = new User(array( |
||
| 240 | 'content' => new Content(array( |
||
| 241 | 'versionInfo' => new VersionInfo(array( |
||
| 242 | 'contentInfo' => new ContentInfo(array( |
||
| 243 | 'id' => 123, |
||
| 244 | )), |
||
| 245 | )), |
||
| 246 | )), |
||
| 247 | 'login' => 'login', |
||
| 248 | )); |
||
| 249 | |||
| 250 | $ezInfoCollection = new EzInfoCollection(); |
||
| 251 | |||
| 252 | $this->ezRepository->expects($this->once()) |
||
| 253 | ->method('getContentService') |
||
| 254 | ->willReturn($this->contentService); |
||
| 255 | |||
| 256 | $this->contentService->expects($this->once()) |
||
| 257 | ->method('loadContent') |
||
| 258 | ->with(123) |
||
| 259 | ->willReturn($content); |
||
| 260 | |||
| 261 | $this->ezRepository->expects($this->once()) |
||
| 262 | ->method('getCurrentUser') |
||
| 263 | ->willReturn($user); |
||
| 264 | |||
| 265 | $this->repository->expects($this->once()) |
||
| 266 | ->method('createMain') |
||
| 267 | ->willThrowException(new DBALException()); |
||
| 268 | |||
| 269 | $this->factory->expects($this->never()) |
||
| 270 | ->method('getLegacyValue'); |
||
| 271 | |||
| 272 | $this->repository->expects($this->never()) |
||
| 273 | ->method('createChild'); |
||
| 274 | |||
| 275 | $this->action->act($event); |
||
| 276 | } |
||
| 277 | |||
| 278 | /** |
||
| 279 | * @expectedException \Netgen\Bundle\InformationCollectionBundle\Exception\ActionFailedException |
||
| 280 | */ |
||
| 281 | public function testActWithExceptionOnInformationCollectionAttributeRepository() |
||
| 282 | { |
||
| 283 | $location = new Location(array( |
||
| 284 | 'contentInfo' => new ContentInfo(array( |
||
| 285 | 'id' => 123, |
||
| 286 | )), |
||
| 287 | )); |
||
| 288 | |||
| 289 | $content = new Content(array( |
||
| 290 | 'internalFields' => $this->fields, |
||
| 291 | 'versionInfo' => new VersionInfo(array( |
||
| 292 | 'contentInfo' => new ContentInfo(array( |
||
| 293 | 'mainLanguageCode' => 'eng_GB', |
||
| 294 | )), |
||
| 295 | )), |
||
| 296 | )); |
||
| 297 | |||
| 298 | $dataWrapper = new DataWrapper($this->struct, $this->contentType, $location); |
||
| 299 | $event = new InformationCollected($dataWrapper); |
||
| 300 | |||
| 301 | $user = new User(array( |
||
| 302 | 'content' => new Content(array( |
||
| 303 | 'versionInfo' => new VersionInfo(array( |
||
| 304 | 'contentInfo' => new ContentInfo(array( |
||
| 305 | 'id' => 123, |
||
| 306 | )), |
||
| 307 | )), |
||
| 308 | )), |
||
| 309 | 'login' => 'login', |
||
| 310 | )); |
||
| 311 | |||
| 312 | $ezInfoCollection = new EzInfoCollection(); |
||
| 313 | $ezInfoCollectionAttribute = new EzInfoCollectionAttribute(); |
||
| 314 | |||
| 315 | $this->ezRepository->expects($this->once()) |
||
| 316 | ->method('getContentService') |
||
| 317 | ->willReturn($this->contentService); |
||
| 318 | |||
| 319 | $this->contentService->expects($this->once()) |
||
| 320 | ->method('loadContent') |
||
| 321 | ->with(123) |
||
| 322 | ->willReturn($content); |
||
| 323 | |||
| 324 | $this->ezRepository->expects($this->once()) |
||
| 325 | ->method('getCurrentUser') |
||
| 326 | ->willReturn($user); |
||
| 327 | |||
| 328 | $this->repository->expects($this->once()) |
||
| 329 | ->method('createMain') |
||
| 330 | ->willReturn($ezInfoCollection); |
||
| 331 | |||
| 332 | $this->factory->expects($this->exactly(1)) |
||
| 333 | ->method('getLegacyValue') |
||
| 334 | ->withAnyParameters() |
||
| 335 | ->willReturn($this->legacyData); |
||
| 336 | |||
| 337 | $this->repository->expects($this->once()) |
||
| 338 | ->method('createChild') |
||
| 339 | ->willThrowException(new DBALException()); |
||
| 340 | |||
| 341 | $this->action->act($event); |
||
| 342 | } |
||
| 343 | } |
||
| 344 |