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 |
||
| 7 | class PageServiceTest extends \PHPUnit_Framework_TestCase |
||
| 8 | { |
||
| 9 | public function testGetPaginationShouldReturnPaginationInstance() |
||
| 10 | { |
||
| 11 | $pageFilter = $this->getMockBuilder(\Page\Filter\PageFilter::class) |
||
| 12 | ->disableOriginalConstructor() |
||
| 13 | ->getMock(); |
||
| 14 | $pageMapper = $this->getMockBuilder(\Page\Mapper\PageMapper::class) |
||
| 15 | ->disableOriginalConstructor() |
||
| 16 | ->getMock(); |
||
| 17 | $paginator = $this->getMockBuilder(\Zend\Paginator\Paginator::class) |
||
| 18 | ->disableOriginalConstructor() |
||
| 19 | ->getMock(); |
||
| 20 | $upload = $this->getMockBuilder(\UploadHelper\Upload::class) |
||
| 21 | ->disableOriginalConstructor() |
||
| 22 | ->getMock(); |
||
| 23 | $pageService = new \Page\Service\PageService($pageFilter, $pageMapper, $paginator, $upload); |
||
| 24 | |||
| 25 | static::assertInstanceOf(\Zend\Paginator\Paginator::class, $pageService->getPagination()); |
||
| 26 | } |
||
| 27 | |||
| 28 | public function testGetPageShouldReturnNull() |
||
| 29 | { |
||
| 30 | $pageFilter = $this->getMockBuilder(\Page\Filter\PageFilter::class) |
||
| 31 | ->disableOriginalConstructor() |
||
| 32 | ->getMock(); |
||
| 33 | $pageMapper = $this->getMockBuilder(\Page\Mapper\PageMapper::class) |
||
| 34 | ->setMethods(['select', 'current']) |
||
| 35 | ->disableOriginalConstructor() |
||
| 36 | ->getMock(); |
||
| 37 | $pageMapper->expects(static::once()) |
||
| 38 | ->method('select') |
||
| 39 | ->willReturnSelf(); |
||
| 40 | $pageMapper->expects(static::once()) |
||
| 41 | ->method('current') |
||
| 42 | ->willReturn(null); |
||
| 43 | $paginator = $this->getMockBuilder(\Zend\Paginator\Paginator::class) |
||
| 44 | ->disableOriginalConstructor() |
||
| 45 | ->getMock(); |
||
| 46 | $upload = $this->getMockBuilder(\UploadHelper\Upload::class) |
||
| 47 | ->disableOriginalConstructor() |
||
| 48 | ->getMock(); |
||
| 49 | $pageService = new \Page\Service\PageService($pageFilter, $pageMapper, $paginator, $upload); |
||
| 50 | |||
| 51 | static::assertInternalType('null', $pageService->getPage(1)); |
||
| 52 | } |
||
| 53 | |||
| 54 | public function testGetPageBySlugShouldReturnNull() |
||
| 55 | { |
||
| 56 | $pageFilter = $this->getMockBuilder(\Page\Filter\PageFilter::class) |
||
| 57 | ->disableOriginalConstructor() |
||
| 58 | ->getMock(); |
||
| 59 | $pageMapper = $this->getMockBuilder(\Page\Mapper\PageMapper::class) |
||
| 60 | ->setMethods(['getActivePage', 'current']) |
||
| 61 | ->disableOriginalConstructor() |
||
| 62 | ->getMock(); |
||
| 63 | $pageMapper->expects(static::once()) |
||
| 64 | ->method('getActivePage') |
||
| 65 | ->willReturnSelf(); |
||
| 66 | $pageMapper->expects(static::once()) |
||
| 67 | ->method('current') |
||
| 68 | ->willReturn(null); |
||
| 69 | $paginator = $this->getMockBuilder(\Zend\Paginator\Paginator::class) |
||
| 70 | ->disableOriginalConstructor() |
||
| 71 | ->getMock(); |
||
| 72 | $upload = $this->getMockBuilder(\UploadHelper\Upload::class) |
||
| 73 | ->disableOriginalConstructor() |
||
| 74 | ->getMock(); |
||
| 75 | $pageService = new \Page\Service\PageService($pageFilter, $pageMapper, $paginator, $upload); |
||
| 76 | |||
| 77 | static::assertInternalType('null', $pageService->getPageBySlug('test')); |
||
| 78 | } |
||
| 79 | |||
| 80 | public function testGetHomepageShouldReturnNull() |
||
| 81 | { |
||
| 82 | $pageFilter = $this->getMockBuilder(\Page\Filter\PageFilter::class) |
||
| 83 | ->disableOriginalConstructor() |
||
| 84 | ->getMock(); |
||
| 85 | $pageMapper = $this->getMockBuilder(\Page\Mapper\PageMapper::class) |
||
| 86 | ->setMethods(['select', 'current']) |
||
| 87 | ->disableOriginalConstructor() |
||
| 88 | ->getMock(); |
||
| 89 | $pageMapper->expects(static::once()) |
||
| 90 | ->method('select') |
||
| 91 | ->willReturnSelf(); |
||
| 92 | $pageMapper->expects(static::once()) |
||
| 93 | ->method('current') |
||
| 94 | ->willReturn(null); |
||
| 95 | $paginator = $this->getMockBuilder(\Zend\Paginator\Paginator::class) |
||
| 96 | ->disableOriginalConstructor() |
||
| 97 | ->getMock(); |
||
| 98 | $upload = $this->getMockBuilder(\UploadHelper\Upload::class) |
||
| 99 | ->disableOriginalConstructor() |
||
| 100 | ->getMock(); |
||
| 101 | $pageService = new \Page\Service\PageService($pageFilter, $pageMapper, $paginator, $upload); |
||
| 102 | |||
| 103 | static::assertInternalType('null', $pageService->getHomepage()); |
||
| 104 | } |
||
| 105 | |||
| 106 | public function testCreatePageShouldReturnTrue() |
||
| 107 | { |
||
| 108 | $pageData = [ |
||
| 109 | 'is_homepage' => 1, |
||
| 110 | ]; |
||
| 111 | $pageFilter = $this->getMockBuilder(\Page\Filter\PageFilter::class) |
||
| 112 | ->setMethods(['getInputFilter', 'setData', 'isValid', 'getValues']) |
||
| 113 | ->disableOriginalConstructor() |
||
| 114 | ->getMock(); |
||
| 115 | $pageFilter->expects(static::once()) |
||
| 116 | ->method('getInputFilter') |
||
| 117 | ->willReturnSelf(); |
||
| 118 | $pageFilter->expects(static::once()) |
||
| 119 | ->method('isValid') |
||
| 120 | ->willReturn(true); |
||
| 121 | $pageFilter->expects(static::once()) |
||
| 122 | ->method('getValues') |
||
| 123 | ->willReturn($pageData); |
||
| 124 | $pageFilter->expects(static::once()) |
||
| 125 | ->method('setData') |
||
| 126 | ->willReturnSelf(); |
||
| 127 | $pageMapper = $this->getMockBuilder(\Page\Mapper\PageMapper::class) |
||
| 128 | ->setMethods(['update', 'insert']) |
||
| 129 | ->disableOriginalConstructor() |
||
| 130 | ->getMock(); |
||
| 131 | $pageMapper->expects(static::once()) |
||
| 132 | ->method('insert') |
||
| 133 | ->willReturn(true); |
||
| 134 | $paginator = $this->getMockBuilder(\Zend\Paginator\Paginator::class) |
||
| 135 | ->disableOriginalConstructor() |
||
| 136 | ->getMock(); |
||
| 137 | $upload = $this->getMockBuilder(\UploadHelper\Upload::class) |
||
| 138 | ->disableOriginalConstructor() |
||
| 139 | ->getMock(); |
||
| 140 | $pageService = new \Page\Service\PageService($pageFilter, $pageMapper, $paginator, $upload); |
||
| 141 | |||
| 142 | static::assertSame(true, $pageService->createPage($pageData)); |
||
| 143 | } |
||
| 144 | |||
| 145 | /** |
||
| 146 | * @expectedExceptionMessage test error |
||
| 147 | * @expectedException \Std\FilterException |
||
| 148 | */ |
||
| 149 | public function testCreatePageShouldThrowFilterException() |
||
| 150 | { |
||
| 151 | $pageData = [ |
||
| 152 | 'is_homepage' => 1, |
||
| 153 | ]; |
||
| 154 | |||
| 155 | $pageFilter = $this->getMockBuilder(\Page\Filter\PageFilter::class) |
||
| 156 | ->setMethods(['getInputFilter', 'setData', 'isValid', 'getMessages']) |
||
| 157 | ->disableOriginalConstructor() |
||
| 158 | ->getMock(); |
||
| 159 | $pageFilter->expects(static::once()) |
||
| 160 | ->method('getInputFilter') |
||
| 161 | ->willReturnSelf(); |
||
| 162 | $pageFilter->expects(static::once()) |
||
| 163 | ->method('isValid') |
||
| 164 | ->willReturn(false); |
||
| 165 | $pageFilter->expects(static::once()) |
||
| 166 | ->method('getMessages') |
||
| 167 | ->willReturn(['test error']); |
||
| 168 | $pageFilter->expects(static::once()) |
||
| 169 | ->method('setData') |
||
| 170 | ->willReturnSelf(); |
||
| 171 | $pageMapper = $this->getMockBuilder(\Page\Mapper\PageMapper::class) |
||
| 172 | ->setMethods(['update', 'insert']) |
||
| 173 | ->disableOriginalConstructor() |
||
| 174 | ->getMock(); |
||
| 175 | $paginator = $this->getMockBuilder(\Zend\Paginator\Paginator::class) |
||
| 176 | ->disableOriginalConstructor() |
||
| 177 | ->getMock(); |
||
| 178 | $upload = $this->getMockBuilder(\UploadHelper\Upload::class) |
||
| 179 | ->disableOriginalConstructor() |
||
| 180 | ->getMock(); |
||
| 181 | $pageService = new \Page\Service\PageService($pageFilter, $pageMapper, $paginator, $upload); |
||
| 182 | |||
| 183 | static::assertSame(true, $pageService->createPage($pageData)); |
||
| 184 | } |
||
| 185 | |||
| 186 | /** |
||
| 187 | * @expectedExceptionMessage Page object not found. Page ID:1 |
||
| 188 | * @expectedException \Exception |
||
| 189 | */ |
||
| 190 | public function testUpdatePageShouldThrowException() |
||
| 191 | { |
||
| 192 | $pageData = [ |
||
| 193 | 'is_homepage' => 1, |
||
| 194 | ]; |
||
| 195 | $pageFilter = $this->getMockBuilder(\Page\Filter\PageFilter::class) |
||
| 196 | ->setMethods(['select']) |
||
| 197 | ->disableOriginalConstructor() |
||
| 198 | ->getMock(); |
||
| 199 | $pageMapper = $this->getMockBuilder(\Page\Mapper\PageMapper::class) |
||
| 200 | ->setMethods(['select', 'current']) |
||
| 201 | ->disableOriginalConstructor() |
||
| 202 | ->getMock(); |
||
| 203 | $pageMapper->expects(static::once()) |
||
| 204 | ->method('select') |
||
| 205 | ->willReturnSelf(); |
||
| 206 | $pageMapper->expects(static::once()) |
||
| 207 | ->method('current') |
||
| 208 | ->willReturn(null); |
||
| 209 | $paginator = $this->getMockBuilder(\Zend\Paginator\Paginator::class) |
||
| 210 | ->disableOriginalConstructor() |
||
| 211 | ->getMock(); |
||
| 212 | $upload = $this->getMockBuilder(\UploadHelper\Upload::class) |
||
| 213 | ->disableOriginalConstructor() |
||
| 214 | ->getMock(); |
||
| 215 | $pageService = new \Page\Service\PageService($pageFilter, $pageMapper, $paginator, $upload); |
||
| 216 | |||
| 217 | $pageService->updatePage($pageData, 1); |
||
| 218 | } |
||
| 219 | |||
| 220 | /** |
||
| 221 | * @expectedExceptionMessage test error |
||
| 222 | * @expectedException \Std\FilterException |
||
| 223 | */ |
||
| 224 | public function testUpdatePageShouldThrowFilterException() |
||
| 225 | { |
||
| 226 | $pageData = [ |
||
| 227 | 'is_homepage' => 1, |
||
| 228 | ]; |
||
| 229 | $pageFilter = $this->getMockBuilder(\Page\Filter\PageFilter::class) |
||
| 230 | ->setMethods(['getInputFilter', 'setData', 'isValid', 'getMessages']) |
||
| 231 | ->disableOriginalConstructor() |
||
| 232 | ->getMock(); |
||
| 233 | $pageFilter->expects(static::once()) |
||
| 234 | ->method('getInputFilter') |
||
| 235 | ->willReturnSelf(); |
||
| 236 | $pageFilter->expects(static::once()) |
||
| 237 | ->method('setData') |
||
| 238 | ->willReturnSelf(); |
||
| 239 | $pageFilter->expects(static::once()) |
||
| 240 | ->method('isValid') |
||
| 241 | ->willReturn(false); |
||
| 242 | $pageFilter->expects(static::once()) |
||
| 243 | ->method('getMessages') |
||
| 244 | ->willReturn(['test error']); |
||
| 245 | $pageMapper = $this->getMockBuilder(\Page\Mapper\PageMapper::class) |
||
| 246 | ->setMethods(['select', 'current']) |
||
| 247 | ->disableOriginalConstructor() |
||
| 248 | ->getMock(); |
||
| 249 | $pageMapper->expects(static::once()) |
||
| 250 | ->method('select') |
||
| 251 | ->willReturnSelf(); |
||
| 252 | $pageMapper->expects(static::once()) |
||
| 253 | ->method('current') |
||
| 254 | ->willReturn(new \Page\Entity\Page()); |
||
| 255 | $paginator = $this->getMockBuilder(\Zend\Paginator\Paginator::class) |
||
| 256 | ->disableOriginalConstructor() |
||
| 257 | ->getMock(); |
||
| 258 | $upload = $this->getMockBuilder(\UploadHelper\Upload::class) |
||
| 259 | ->disableOriginalConstructor() |
||
| 260 | ->getMock(); |
||
| 261 | $pageService = new \Page\Service\PageService($pageFilter, $pageMapper, $paginator, $upload); |
||
| 262 | |||
| 263 | $pageService->updatePage($pageData, 1); |
||
| 264 | } |
||
| 265 | |||
| 266 | public function testUpdatePageShouldReturnTrue() |
||
| 267 | { |
||
| 268 | $pageData = [ |
||
| 269 | 'is_homepage' => 1, |
||
| 270 | ]; |
||
| 271 | $pageFilter = $this->getMockBuilder(\Page\Filter\PageFilter::class) |
||
| 272 | ->setMethods(['getInputFilter', 'setData', 'isValid', 'getValues', 'select']) |
||
| 273 | ->disableOriginalConstructor() |
||
| 274 | ->getMock(); |
||
| 275 | $pageFilter->expects(static::once()) |
||
| 276 | ->method('getInputFilter') |
||
| 277 | ->willReturnSelf(); |
||
| 278 | $pageFilter->expects(static::once()) |
||
| 279 | ->method('isValid') |
||
| 280 | ->willReturn(true); |
||
| 281 | $pageFilter->expects(static::once()) |
||
| 282 | ->method('getValues') |
||
| 283 | ->willReturn($pageData); |
||
| 284 | $pageFilter->expects(static::once()) |
||
| 285 | ->method('setData') |
||
| 286 | ->willReturnSelf(); |
||
| 287 | $pageMapper = $this->getMockBuilder(\Page\Mapper\PageMapper::class) |
||
| 288 | ->setMethods(['update', 'select', 'current']) |
||
| 289 | ->disableOriginalConstructor() |
||
| 290 | ->getMock(); |
||
| 291 | $pageMapper->expects(static::exactly(2)) |
||
| 292 | ->method('update') |
||
| 293 | ->willReturn(true); |
||
| 294 | $pageMapper->expects(static::once()) |
||
| 295 | ->method('select') |
||
| 296 | ->willReturnSelf(); |
||
| 297 | $pageMapper->expects(static::once()) |
||
| 298 | ->method('current') |
||
| 299 | ->willReturn(new \Page\Entity\Page()); |
||
| 300 | $paginator = $this->getMockBuilder(\Zend\Paginator\Paginator::class) |
||
| 301 | ->disableOriginalConstructor() |
||
| 302 | ->getMock(); |
||
| 303 | $upload = $this->getMockBuilder(\UploadHelper\Upload::class) |
||
| 304 | ->disableOriginalConstructor() |
||
| 305 | ->getMock(); |
||
| 306 | $pageService = new \Page\Service\PageService($pageFilter, $pageMapper, $paginator, $upload); |
||
| 307 | |||
| 308 | static::assertSame(true, $pageService->updatePage($pageData, 1)); |
||
| 309 | } |
||
| 310 | |||
| 311 | public function testUpdatePageShouldReturnTrueAndDeleteImageViaUploader() |
||
| 312 | { |
||
| 313 | $pageData = [ |
||
| 314 | 'is_homepage' => 1, |
||
| 315 | 'main_img' => 'test_path', |
||
| 316 | ]; |
||
| 317 | $pageFilter = $this->getMockBuilder(\Page\Filter\PageFilter::class) |
||
| 318 | ->setMethods(['getInputFilter', 'setData', 'isValid', 'getValues', 'select']) |
||
| 319 | ->disableOriginalConstructor() |
||
| 320 | ->getMock(); |
||
| 321 | $pageFilter->expects(static::once()) |
||
| 322 | ->method('getInputFilter') |
||
| 323 | ->willReturnSelf(); |
||
| 324 | $pageFilter->expects(static::once()) |
||
| 325 | ->method('isValid') |
||
| 326 | ->willReturn(true); |
||
| 327 | $pageFilter->expects(static::once()) |
||
| 328 | ->method('getValues') |
||
| 329 | ->willReturn($pageData); |
||
| 330 | $pageFilter->expects(static::once()) |
||
| 331 | ->method('setData') |
||
| 332 | ->willReturnSelf(); |
||
| 333 | $pageMapper = $this->getMockBuilder(\Page\Mapper\PageMapper::class) |
||
| 334 | ->setMethods(['update', 'select', 'current']) |
||
| 335 | ->disableOriginalConstructor() |
||
| 336 | ->getMock(); |
||
| 337 | $pageMapper->expects(static::exactly(2)) |
||
| 338 | ->method('update') |
||
| 339 | ->willReturn(true); |
||
| 340 | $pageMapper->expects(static::once()) |
||
| 341 | ->method('select') |
||
| 342 | ->willReturnSelf(); |
||
| 343 | $pageMapper->expects(static::once()) |
||
| 344 | ->method('current') |
||
| 345 | ->willReturn(new \Page\Entity\Page()); |
||
| 346 | $paginator = $this->getMockBuilder(\Zend\Paginator\Paginator::class) |
||
| 347 | ->disableOriginalConstructor() |
||
| 348 | ->getMock(); |
||
| 349 | $upload = $this->getMockBuilder(\UploadHelper\Upload::class) |
||
| 350 | ->disableOriginalConstructor() |
||
| 351 | ->getMock(); |
||
| 352 | $pageService = new \Page\Service\PageService($pageFilter, $pageMapper, $paginator, $upload); |
||
| 353 | |||
| 354 | static::assertSame(true, $pageService->updatePage($pageData, 1)); |
||
| 355 | } |
||
| 356 | |||
| 357 | public function testDeletePageShouldReturnTrue() |
||
| 358 | { |
||
| 359 | $pageFilter = $this->getMockBuilder(\Page\Filter\PageFilter::class) |
||
| 360 | ->disableOriginalConstructor() |
||
| 361 | ->getMock(); |
||
| 362 | $pageMapper = $this->getMockBuilder(\Page\Mapper\PageMapper::class) |
||
| 363 | ->setMethods(['select', 'current', 'delete']) |
||
| 364 | ->disableOriginalConstructor() |
||
| 365 | ->getMock(); |
||
| 366 | $pageMapper->expects(static::once()) |
||
| 367 | ->method('select') |
||
| 368 | ->willReturnSelf(); |
||
| 369 | $pageMapper->expects(static::once()) |
||
| 370 | ->method('current') |
||
| 371 | ->willReturn(new \Page\Entity\Page()); |
||
| 372 | $pageMapper->expects(static::once()) |
||
| 373 | ->method('delete') |
||
| 374 | ->willReturn(true); |
||
| 375 | $paginator = $this->getMockBuilder(\Zend\Paginator\Paginator::class) |
||
| 376 | ->disableOriginalConstructor() |
||
| 377 | ->getMock(); |
||
| 378 | $upload = $this->getMockBuilder(\UploadHelper\Upload::class) |
||
| 379 | ->disableOriginalConstructor() |
||
| 380 | ->getMock(); |
||
| 381 | $pageService = new \Page\Service\PageService($pageFilter, $pageMapper, $paginator, $upload); |
||
| 382 | |||
| 383 | static::assertSame(true, $pageService->delete(1)); |
||
| 384 | } |
||
| 385 | |||
| 386 | /** |
||
| 387 | * @expectedExceptionMessage Page not found |
||
| 388 | * @expectedException \Exception |
||
| 389 | */ |
||
| 390 | public function testDeletePageShouldThrowException() |
||
| 391 | { |
||
| 392 | $pageFilter = $this->getMockBuilder(\Page\Filter\PageFilter::class) |
||
| 393 | ->disableOriginalConstructor() |
||
| 394 | ->getMock(); |
||
| 395 | $pageMapper = $this->getMockBuilder(\Page\Mapper\PageMapper::class) |
||
| 396 | ->setMethods(['select', 'current']) |
||
| 397 | ->disableOriginalConstructor() |
||
| 398 | ->getMock(); |
||
| 399 | $pageMapper->expects(static::once()) |
||
| 400 | ->method('select') |
||
| 401 | ->willReturnSelf(); |
||
| 402 | $pageMapper->expects(static::once()) |
||
| 403 | ->method('current') |
||
| 404 | ->willReturn(null); |
||
| 405 | $paginator = $this->getMockBuilder(\Zend\Paginator\Paginator::class) |
||
| 406 | ->disableOriginalConstructor() |
||
| 407 | ->getMock(); |
||
| 408 | $upload = $this->getMockBuilder(\UploadHelper\Upload::class) |
||
| 409 | ->disableOriginalConstructor() |
||
| 410 | ->getMock(); |
||
| 411 | $pageService = new \Page\Service\PageService($pageFilter, $pageMapper, $paginator, $upload); |
||
| 412 | |||
| 413 | $pageService->delete(1); |
||
| 414 | } |
||
| 415 | } |
||
| 416 |