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 |
||
| 35 | class LastSearchesServiceTest extends UnitTest |
||
| 36 | { |
||
| 37 | /** |
||
| 38 | * @var LastSearchesService |
||
| 39 | */ |
||
| 40 | protected $lastSearchesService; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var TypoScriptFrontendController |
||
| 44 | */ |
||
| 45 | protected $tsfeMock; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var DatabaseConnection |
||
| 49 | */ |
||
| 50 | protected $databaseMock; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var TypoScriptConfiguration |
||
| 54 | */ |
||
| 55 | protected $configurationMock; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @return void |
||
| 59 | */ |
||
| 60 | public function setUp() |
||
| 61 | { |
||
| 62 | $this->tsfeMock = $this->getDumbMock('TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController'); |
||
| 63 | $this->databaseMock = $this->getDumbMock('TYPO3\CMS\Core\Database\DatabaseConnection'); |
||
| 64 | $this->configurationMock = $this->getDumbMock('\ApacheSolrForTypo3\Solr\System\Configuration\TypoScriptConfiguration'); |
||
| 65 | |||
| 66 | $this->lastSearchesService = $this->getMockBuilder(LastSearchesService::class) |
||
| 67 | ->setMethods(['getLastSearchesFromFrontendSession']) |
||
| 68 | ->setConstructorArgs([ $this->configurationMock, |
||
| 69 | $this->tsfeMock, |
||
| 70 | $this->databaseMock])->getMock(); |
||
| 71 | } |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @test |
||
| 75 | */ |
||
| 76 | public function canGetLastSearchesFromSessionInUserMode() |
||
| 77 | { |
||
| 78 | $fakedLastSearchesInSession = array('first search', 'second search'); |
||
| 79 | |||
| 80 | $this->lastSearchesService->expects($this->once())->method('getLastSearchesFromFrontendSession')->will($this->returnValue( |
||
| 81 | $fakedLastSearchesInSession |
||
| 82 | )); |
||
| 83 | |||
| 84 | $this->assertDatabaseWillNeverBeQueried(); |
||
| 85 | $this->fakeLastSearchMode('user'); |
||
| 86 | $this->fakeLastSearchLimit(10); |
||
| 87 | |||
| 88 | $lastSearches = $this->lastSearchesService->getLastSearches(); |
||
| 89 | $this->assertSame($fakedLastSearchesInSession, array_reverse($lastSearches), 'Did not get last searches from session in user mode'); |
||
| 90 | } |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @test |
||
| 94 | */ |
||
| 95 | public function canGetLastSearchesFromDatabaseInGlobalMode() |
||
| 96 | { |
||
| 97 | $fakedLastSearchesInDatabase = array( |
||
| 98 | array('keywords' => 'test'), |
||
| 99 | array('keywords' => 'test 2') |
||
| 100 | ); |
||
| 101 | |||
| 102 | $this->fakeLastSearchMode('global'); |
||
| 103 | $this->fakeLastSearchLimit(10); |
||
| 104 | $this->assertSessionWillNeverBeQueried(); |
||
| 105 | |||
| 106 | $this->databaseMock->expects($this->once())->method('exec_SELECTgetRows')->will($this->returnValue($fakedLastSearchesInDatabase)); |
||
| 107 | |||
| 108 | $lastSearches = $this->lastSearchesService->getLastSearches(); |
||
| 109 | |||
| 110 | $this->assertSame(array('test', 'test 2'), $lastSearches, 'Did not get last searches from database'); |
||
| 111 | } |
||
| 112 | |||
| 113 | /** |
||
| 114 | * @param string $mode |
||
| 115 | */ |
||
| 116 | protected function fakeLastSearchMode($mode) |
||
| 117 | { |
||
| 118 | $this->configurationMock->expects($this->once())->method('getSearchLastSearchesMode')->will($this->returnValue($mode)); |
||
| 119 | } |
||
| 120 | |||
| 121 | /** |
||
| 122 | * @param integer $limit |
||
| 123 | */ |
||
| 124 | protected function fakeLastSearchLimit($limit) |
||
| 125 | { |
||
| 126 | $this->configurationMock->expects($this->once())->method('getSearchLastSearchesLimit')->will($this->returnValue($limit)); |
||
| 127 | } |
||
| 128 | |||
| 129 | /** |
||
| 130 | * @return void |
||
| 131 | */ |
||
| 132 | protected function assertDatabaseWillNeverBeQueried() |
||
| 133 | { |
||
| 134 | $this->databaseMock->expects($this->never())->method('exec_SELECTgetRows'); |
||
| 135 | } |
||
| 136 | |||
| 137 | /** |
||
| 138 | * @return void |
||
| 139 | */ |
||
| 140 | protected function assertSessionWillNeverBeQueried() |
||
| 141 | { |
||
| 142 | $this->lastSearchesService->expects($this->never())->method('getLastSearchesFromFrontendSession'); |
||
| 143 | } |
||
| 144 | } |
||
| 145 |