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 |
||
22 | class SlugConverterTest extends TestCase |
||
23 | { |
||
24 | /** |
||
25 | * Test for the __construct() method. |
||
26 | * |
||
27 | * @covers \eZ\Publish\Core\Persistence\Legacy\Content\UrlAlias\SlugConverter::__construct |
||
28 | */ |
||
29 | public function testConstructor() |
||
45 | |||
46 | /** |
||
47 | * Test for the convert() method. |
||
48 | * |
||
49 | * @covers \eZ\Publish\Core\Persistence\Legacy\Content\UrlAlias\SlugConverter::convert |
||
50 | */ |
||
51 | View Code Duplication | public function testConvert() |
|
75 | |||
76 | /** |
||
77 | * Test for the convert() method. |
||
78 | * |
||
79 | * @covers \eZ\Publish\Core\Persistence\Legacy\Content\UrlAlias\SlugConverter::convert |
||
80 | */ |
||
81 | View Code Duplication | public function testConvertWithDefaultTextFallback() |
|
105 | |||
106 | /** |
||
107 | * Test for the convert() method. |
||
108 | * |
||
109 | * @covers \eZ\Publish\Core\Persistence\Legacy\Content\UrlAlias\SlugConverter::convert |
||
110 | */ |
||
111 | View Code Duplication | public function testConvertWithGivenTransformation() |
|
135 | |||
136 | public function providerForTestGetUniqueCounterValue() |
||
145 | |||
146 | /** |
||
147 | * Test for the getUniqueCounterValue() method. |
||
148 | * |
||
149 | * @dataProvider providerForTestGetUniqueCounterValue |
||
150 | * @covers \eZ\Publish\Core\Persistence\Legacy\Content\UrlAlias\SlugConverter::getUniqueCounterValue |
||
151 | */ |
||
152 | public function testGetUniqueCounterValue($text, $isRootLevel, $returnValue) |
||
161 | |||
162 | public function cleanupTextData() |
||
163 | { |
||
164 | return [ |
||
165 | [ |
||
166 | '.Ph\'nglui mglw\'nafh, Cthulhu R\'lyeh wgah\'nagl fhtagn!?...', |
||
167 | 'url_cleanup', |
||
168 | 'Ph-nglui-mglw-nafh-Cthulhu-R-lyeh-wgah-nagl-fhtagn!', |
||
169 | ], |
||
170 | [ |
||
171 | '.Ph\'nglui mglw\'nafh, Cthulhu R\'lyeh wgah\'nagl fhtagn!?...', |
||
172 | 'url_cleanup_iri', |
||
173 | 'Ph\'nglui-mglw\'nafh,-Cthulhu-R\'lyeh-wgah\'nagl-fhtagn!', |
||
174 | ], |
||
175 | [ |
||
176 | '.Ph\'nglui mglw\'nafh, Cthulhu R\'lyeh wgah\'nagl fhtagn!?...', |
||
177 | 'url_cleanup_compat', |
||
178 | 'ph_nglui_mglw_nafh_cthulhu_r_lyeh_wgah_nagl_fhtagn', |
||
179 | ], |
||
180 | ]; |
||
181 | } |
||
182 | |||
183 | /** |
||
184 | * Test for the cleanupText() method. |
||
185 | * |
||
186 | * @dataProvider cleanupTextData |
||
187 | * @covers \eZ\Publish\Core\Persistence\Legacy\Content\UrlAlias\SlugConverter::cleanupText |
||
188 | */ |
||
189 | public function testCleanupText($text, $method, $expected) |
||
190 | { |
||
191 | $testMethod = new \ReflectionMethod( |
||
192 | '\eZ\Publish\Core\Persistence\Legacy\Content\UrlAlias\SlugConverter', |
||
193 | 'cleanupText' |
||
194 | ); |
||
195 | $testMethod->setAccessible(true); |
||
196 | |||
197 | $actual = $testMethod->invoke($this->getMockedSlugConverter(), $text, $method); |
||
198 | |||
199 | $this->assertEquals( |
||
200 | $expected, |
||
201 | $actual |
||
202 | ); |
||
203 | } |
||
204 | |||
205 | public function convertData() |
||
206 | { |
||
207 | return [ |
||
208 | [ |
||
209 | '.Ph\'nglui mglw\'nafh, Cthulhu R\'lyeh wgah\'nagl fhtagn!?...', |
||
210 | '\'_1\'', |
||
211 | 'urlalias', |
||
212 | 'Ph-nglui-mglw-nafh-Cthulhu-R-lyeh-wgah-nagl-fhtagn!', |
||
213 | ], |
||
214 | [ |
||
215 | '.Ph\'nglui mglw\'nafh, Cthulhu R\'lyeh wgah\'nagl fhtagn!?...', |
||
216 | '\'_1\'', |
||
217 | 'urlalias_iri', |
||
218 | 'Ph\'nglui-mglw\'nafh,-Cthulhu-R\'lyeh-wgah\'nagl-fhtagn!', |
||
219 | ], |
||
220 | [ |
||
221 | '.Ph\'nglui mglw\'nafh, Cthulhu R\'lyeh wgah\'nagl fhtagn!?...', |
||
222 | '\'_1\'', |
||
223 | 'urlalias_compat', |
||
224 | 'ph_nglui_mglw_nafh_cthulhu_r_lyeh_wgah_nagl_fhtagn', |
||
225 | ], |
||
226 | ]; |
||
227 | } |
||
228 | |||
229 | /** |
||
230 | * Test for the convert() method. |
||
231 | * |
||
232 | * @dataProvider convertData |
||
233 | * @covers \eZ\Publish\Core\Persistence\Legacy\Content\UrlAlias\SlugConverter::convert |
||
234 | * @depends testCleanupText |
||
235 | */ |
||
236 | public function testConvertNoMocking($text, $defaultText, $transformation, $expected) |
||
237 | { |
||
238 | $transformationProcessor = new PreprocessedBased( |
||
239 | new PcreCompiler( |
||
240 | new Utf8Converter() |
||
241 | ), |
||
242 | [ |
||
243 | __DIR__ . '../../../../../Tests/TransformationProcessor/_fixtures/transformations/ascii.tr.result', |
||
244 | __DIR__ . '../../../../../Tests/TransformationProcessor/_fixtures/transformations/basic.tr.result', |
||
245 | __DIR__ . '../../../../../Tests/TransformationProcessor/_fixtures/transformations/latin.tr.result', |
||
246 | __DIR__ . '../../../../../Tests/TransformationProcessor/_fixtures/transformations/search.tr.result', |
||
247 | ] |
||
248 | ); |
||
249 | $slugConverter = new SlugConverter($transformationProcessor); |
||
250 | |||
251 | $this->assertEquals( |
||
252 | $expected, |
||
253 | $slugConverter->convert($text, $defaultText, $transformation) |
||
254 | ); |
||
255 | } |
||
256 | |||
257 | /** |
||
258 | * @var array |
||
259 | */ |
||
260 | protected $configuration = array( |
||
261 | 'transformation' => 'testTransformation1', |
||
262 | 'transformationGroups' => array( |
||
263 | 'testTransformation1' => array( |
||
264 | 'commands' => array( |
||
265 | 'test_command1', |
||
266 | ), |
||
267 | 'cleanupMethod' => 'test_cleanup1', |
||
268 | ), |
||
269 | 'testTransformation2' => array( |
||
270 | 'commands' => array( |
||
271 | 'test_command2', |
||
272 | ), |
||
273 | 'cleanupMethod' => 'test_cleanup2', |
||
274 | ), |
||
275 | ), |
||
276 | 'reservedNames' => array( |
||
277 | 'reserved', |
||
278 | ), |
||
279 | ); |
||
280 | |||
281 | /** |
||
282 | * @var \eZ\Publish\Core\Persistence\Legacy\Content\UrlAlias\SlugConverter |
||
283 | */ |
||
284 | protected $slugConverter; |
||
285 | |||
286 | /** |
||
287 | * @var \PHPUnit_Framework_MockObject_MockObject |
||
288 | */ |
||
289 | protected $slugConverterMock; |
||
290 | |||
291 | /** |
||
292 | * @var \PHPUnit_Framework_MockObject_MockObject |
||
293 | */ |
||
294 | protected $transformationProcessorMock; |
||
295 | |||
296 | /** |
||
297 | * @return \eZ\Publish\Core\Persistence\Legacy\Content\UrlAlias\SlugConverter |
||
298 | */ |
||
299 | protected function getMockedSlugConverter() |
||
310 | |||
311 | /** |
||
312 | * @param array $methods |
||
313 | * |
||
314 | * @return \eZ\Publish\Core\Persistence\Legacy\Content\UrlAlias\SlugConverter|\PHPUnit_Framework_MockObject_MockObject |
||
315 | */ |
||
316 | protected function getSlugConverterMock(array $methods = array()) |
||
331 | |||
332 | /** |
||
333 | * @return \PHPUnit_Framework_MockObject_MockObject |
||
334 | */ |
||
335 | protected function getTransformationProcessorMock() |
||
351 | |||
352 | /** |
||
353 | * Returns the test suite with all tests declared in this class. |
||
354 | * |
||
355 | * @return \PHPUnit_Framework_TestSuite |
||
356 | */ |
||
357 | public static function suite() |
||
361 | } |
||
362 |