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 |
||
| 16 | abstract class AbstractServiceTest extends TestCase |
||
| 17 | { |
||
| 18 | /** |
||
| 19 | * Purely to attempt to make tests easier to read. |
||
| 20 | * |
||
| 21 | * As language parameter is ignored from providers and replced with values in tests, this is used to mark value of |
||
| 22 | * language argument instead of either askingproviders to use 0, or a valid language array which would then not be |
||
| 23 | * used. |
||
| 24 | */ |
||
| 25 | const LANG_ARG = 0; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var \object|\PHPUnit_Framework_MockObject_MockObject |
||
| 29 | */ |
||
| 30 | protected $innerApiServiceMock; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var object |
||
| 34 | */ |
||
| 35 | protected $service; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var \eZ\Publish\Core\Repository\Helper\LanguageResolver|\PHPUnit_Framework_MockObject_MockObject |
||
| 39 | */ |
||
| 40 | protected $languageHelperMock; |
||
| 41 | |||
| 42 | abstract public function getAPIServiceClassName(); |
||
| 43 | |||
| 44 | abstract public function getSiteAccessAwareServiceClassName(); |
||
| 45 | |||
| 46 | public function setUp() |
||
| 60 | |||
| 61 | protected function tearDown() |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @return array See signature on {@link testForPassTrough} for arguments and their type. |
||
| 71 | */ |
||
| 72 | abstract public function providerForPassTroughMethods(); |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Make sure these methods does nothing more then passing the arguments to inner service. |
||
| 76 | * |
||
| 77 | * Methods tested here are basically those without as languages argument. |
||
| 78 | * |
||
| 79 | * @dataProvider providerForPassTroughMethods |
||
| 80 | * |
||
| 81 | * @param string $method |
||
| 82 | * @param array $arguments |
||
| 83 | * @param bool $return |
||
| 84 | */ |
||
| 85 | final public function testForPassTrough($method, array $arguments, $return = true) |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @return array See signature on {@link testForLanguagesLookup} for arguments and their type. |
||
| 104 | * NOTE: languages / prioritizedLanguage, can be set to 0, it will be replaced by tests methods. |
||
| 105 | */ |
||
| 106 | abstract public function providerForLanguagesLookupMethods(); |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Method to be able to customize the logic for setting expected language argument during {@see testForLanguagesLookup()}. |
||
| 110 | * |
||
| 111 | * @param array $arguments |
||
| 112 | * @param int $languageArgumentIndex |
||
| 113 | * @param array $languages |
||
| 114 | * |
||
| 115 | * @return array |
||
| 116 | */ |
||
| 117 | protected function setLanguagesLookupExpectedArguments(array $arguments, $languageArgumentIndex, array $languages) |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Test that language aware methods does a language lookup when language is not set. |
||
| 126 | * |
||
| 127 | * @dataProvider providerForLanguagesLookupMethods |
||
| 128 | * |
||
| 129 | * @param string $method |
||
| 130 | * @param array $arguments |
||
| 131 | * @param bool $return |
||
| 132 | * @param int $languageArgumentIndex From 0 and up, so the array index on $arguments. |
||
| 133 | */ |
||
| 134 | View Code Duplication | final public function testForLanguagesLookup($method, array $arguments, $return, $languageArgumentIndex, callable $callback = null) |
|
| 163 | |||
| 164 | /** |
||
| 165 | * Method to be able to customize the logic for setting expected language argument during {@see testForLanguagesPassTrough()}. |
||
| 166 | * |
||
| 167 | * @param array $arguments |
||
| 168 | * @param int $languageArgumentIndex |
||
| 169 | * @param array $languages |
||
| 170 | * |
||
| 171 | * @return array |
||
| 172 | */ |
||
| 173 | protected function setLanguagesPassTroughArguments(array $arguments, $languageArgumentIndex, array $languages) |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Make sure these methods does nothing more then passing the arguments to inner service. |
||
| 182 | * |
||
| 183 | * @dataProvider providerForLanguagesLookupMethods |
||
| 184 | * |
||
| 185 | * @param string $method |
||
| 186 | * @param array $arguments |
||
| 187 | * @param bool $return |
||
| 188 | * @param int $languageArgumentIndex From 0 and up, so the array index on $arguments. |
||
| 189 | */ |
||
| 190 | View Code Duplication | final public function testForLanguagesPassTrough($method, array $arguments, $return, $languageArgumentIndex, callable $callback = null) |
|
| 217 | } |
||
| 218 |