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 |
||
| 45 | class IndexServiceTest extends IntegrationTest |
||
| 46 | { |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var Queue |
||
| 50 | */ |
||
| 51 | protected $indexQueue; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @return void |
||
| 55 | */ |
||
| 56 | public function setUp() |
||
| 57 | { |
||
| 58 | parent::setUp(); |
||
| 59 | $this->indexQueue = GeneralUtility::makeInstance(Queue::class); |
||
| 60 | |||
| 61 | /** @var $beUser \TYPO3\CMS\Core\Authentication\BackendUserAuthentication */ |
||
| 62 | $beUser = GeneralUtility::makeInstance('TYPO3\CMS\Core\Authentication\BackendUserAuthentication'); |
||
| 63 | $GLOBALS['BE_USER'] = $beUser; |
||
| 64 | |||
| 65 | /** @var $languageService \TYPO3\CMS\Lang\LanguageService */ |
||
| 66 | $languageService = GeneralUtility::makeInstance('TYPO3\CMS\Lang\LanguageService'); |
||
| 67 | $languageService->csConvObj = GeneralUtility::makeInstance('TYPO3\CMS\Core\Charset\CharsetConverter'); |
||
| 68 | $GLOBALS['LANG'] = $languageService; |
||
| 69 | } |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @param string $table |
||
| 73 | * @param integer $uid |
||
| 74 | * @return \Apache_Solr_Response |
||
| 75 | */ |
||
| 76 | protected function addToIndexQueue($table, $uid) |
||
| 77 | { |
||
| 78 | // write an index queue item |
||
| 79 | $this->indexQueue->updateItem($table, $uid, null, time()); |
||
| 80 | } |
||
| 81 | |||
| 82 | |||
| 83 | public function canResolveAbsRefPrefixDataProvider() |
||
| 84 | { |
||
| 85 | return [ |
||
| 86 | 'absRefPrefixIsAuto' => [ |
||
| 87 | 'absRefPrefix' => 'auto', |
||
| 88 | 'expectedUrl' => '/index.php?id=1&tx_ttnews%5Btt_news%5D=111&L=0&cHash=0c0f0e52711b08554a6658230061fb1a', |
||
| 89 | ], |
||
| 90 | 'absRefPrefixIsSlash' => [ |
||
| 91 | 'absRefPrefix' => 'slash', |
||
| 92 | 'expectedUrl' => '/index.php?id=1&tx_ttnews%5Btt_news%5D=111&L=0&cHash=0c0f0e52711b08554a6658230061fb1a', |
||
| 93 | ], |
||
| 94 | 'absRefPrefixIsFoo' => [ |
||
| 95 | 'absRefPrefix' => 'foo', |
||
| 96 | 'expectedUrl' => '/foo/index.php?id=1&tx_ttnews%5Btt_news%5D=111&L=0&cHash=0c0f0e52711b08554a6658230061fb1a', |
||
| 97 | ], |
||
| 98 | 'absRefPrefixIsNone' => [ |
||
| 99 | 'absRefPrefix' => 'none', |
||
| 100 | 'expectedUrl' => 'index.php?id=1&tx_ttnews%5Btt_news%5D=111&L=0&cHash=0c0f0e52711b08554a6658230061fb1a', |
||
| 101 | ] |
||
| 102 | ]; |
||
| 103 | } |
||
| 104 | /** |
||
| 105 | * |
||
| 106 | * @dataProvider canResolveAbsRefPrefixDataProvider |
||
| 107 | * @param string $absRefPrefix |
||
| 108 | * @param string $expectedUrl |
||
| 109 | * @test |
||
| 110 | */ |
||
| 111 | public function canResolveAbsRefPrefix($absRefPrefix, $expectedUrl) |
||
| 112 | { |
||
| 113 | $this->cleanUpSolrServerAndAssertEmpty(); |
||
| 114 | |||
| 115 | // create fake extension database table and TCA |
||
| 116 | $this->importDumpFromFixture('fake_extension2_table.sql'); |
||
| 117 | $GLOBALS['TCA']['tx_fakeextension_domain_model_bar'] = include($this->getFixturePath('fake_extension2_bar_tca.php')); |
||
| 118 | $GLOBALS['TCA']['tx_fakeextension_domain_model_directrelated'] = include($this->getFixturePath('fake_extension2_directrelated_tca.php')); |
||
| 119 | |||
| 120 | $this->importDataSetFromFixture('can_index_custom_record_absRefPrefix_'.$absRefPrefix.'.xml'); |
||
| 121 | |||
| 122 | $this->addToIndexQueue('tx_fakeextension_domain_model_bar', 111); |
||
| 123 | |||
| 124 | /** @var $cliEnvironment CliEnvironment */ |
||
| 125 | $cliEnvironment = GeneralUtility::makeInstance(CliEnvironment::class); |
||
| 126 | $cliEnvironment->backup(); |
||
| 127 | $cliEnvironment->initialize(PATH_site); |
||
| 128 | |||
| 129 | /** @var $indexService IndexService */ |
||
| 130 | $site = Site::getFirstAvailableSite(); |
||
| 131 | $indexService = GeneralUtility::makeInstance(IndexService::class, $site); |
||
| 132 | |||
| 133 | // run the indexer |
||
| 134 | $indexService->indexItems(1); |
||
| 135 | |||
| 136 | $cliEnvironment->restore(); |
||
| 137 | |||
| 138 | // do we have the record in the index with the value from the mm relation? |
||
| 139 | sleep(2); |
||
| 140 | $solrContent = file_get_contents('http://localhost:8080/solr/core_en/select?q=*:*'); |
||
| 141 | $this->assertContains('"numFound":1', $solrContent, 'Could not index document into solr'); |
||
| 142 | $this->assertContains('"url":"'.$expectedUrl.'"', $solrContent, 'Generated unexpected url with absRefPrefix = auto'); |
||
| 143 | $this->assertNotContains('auto', $solrContent, 'absRefPrefix=auto was not resolved'); |
||
| 144 | $this->cleanUpSolrServerAndAssertEmpty(); |
||
| 145 | } |
||
| 146 | } |
||
| 147 |