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() |
||
| 30 | { |
||
| 31 | $slugConverter = $this->getMockedSlugConverter(); |
||
| 32 | |||
| 33 | $this->assertAttributeSame( |
||
| 34 | $this->getTransformationProcessorMock(), |
||
| 35 | 'transformationProcessor', |
||
| 36 | $slugConverter |
||
| 37 | ); |
||
| 38 | |||
| 39 | $this->assertAttributeInternalType( |
||
| 40 | 'array', |
||
| 41 | 'configuration', |
||
| 42 | $slugConverter |
||
| 43 | ); |
||
| 44 | } |
||
| 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() |
|
| 52 | { |
||
| 53 | $slugConverter = $this->getSlugConverterMock(array('cleanupText')); |
||
| 54 | $transformationProcessor = $this->getTransformationProcessorMock(); |
||
| 55 | |||
| 56 | $text = 'test text č '; |
||
| 57 | $transformedText = 'test text c '; |
||
| 58 | $slug = 'test_text_c'; |
||
| 59 | |||
| 60 | $transformationProcessor->expects($this->atLeastOnce()) |
||
| 61 | ->method('transform') |
||
| 62 | ->with($text, array('test_command1')) |
||
| 63 | ->will($this->returnValue($transformedText)); |
||
| 64 | |||
| 65 | $slugConverter->expects($this->once()) |
||
| 66 | ->method('cleanupText') |
||
| 67 | ->with($this->equalTo($transformedText), $this->equalTo('test_cleanup1')) |
||
| 68 | ->will($this->returnValue($slug)); |
||
| 69 | |||
| 70 | $this->assertEquals( |
||
| 71 | $slug, |
||
| 72 | $slugConverter->convert($text) |
||
| 73 | ); |
||
| 74 | } |
||
| 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() |
|
| 82 | { |
||
| 83 | $slugConverter = $this->getSlugConverterMock(array('cleanupText')); |
||
| 84 | $transformationProcessor = $this->getTransformationProcessorMock(); |
||
| 85 | |||
| 86 | $defaultText = 'test text č '; |
||
| 87 | $transformedText = 'test text c '; |
||
| 88 | $slug = 'test_text_c'; |
||
| 89 | |||
| 90 | $transformationProcessor->expects($this->atLeastOnce()) |
||
| 91 | ->method('transform') |
||
| 92 | ->with($defaultText, array('test_command1')) |
||
| 93 | ->will($this->returnValue($transformedText)); |
||
| 94 | |||
| 95 | $slugConverter->expects($this->once()) |
||
| 96 | ->method('cleanupText') |
||
| 97 | ->with($this->equalTo($transformedText), $this->equalTo('test_cleanup1')) |
||
| 98 | ->will($this->returnValue($slug)); |
||
| 99 | |||
| 100 | $this->assertEquals( |
||
| 101 | $slug, |
||
| 102 | $slugConverter->convert('', $defaultText) |
||
| 103 | ); |
||
| 104 | } |
||
| 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() |
|
| 112 | { |
||
| 113 | $slugConverter = $this->getSlugConverterMock(array('cleanupText')); |
||
| 114 | $transformationProcessor = $this->getTransformationProcessorMock(); |
||
| 115 | |||
| 116 | $text = 'test text č '; |
||
| 117 | $transformedText = 'test text c '; |
||
| 118 | $slug = 'test_text_c'; |
||
| 119 | |||
| 120 | $transformationProcessor->expects($this->atLeastOnce()) |
||
| 121 | ->method('transform') |
||
| 122 | ->with($text, array('test_command2')) |
||
| 123 | ->will($this->returnValue($transformedText)); |
||
| 124 | |||
| 125 | $slugConverter->expects($this->once()) |
||
| 126 | ->method('cleanupText') |
||
| 127 | ->with($this->equalTo($transformedText), $this->equalTo('test_cleanup2')) |
||
| 128 | ->will($this->returnValue($slug)); |
||
| 129 | |||
| 130 | $this->assertEquals( |
||
| 131 | $slug, |
||
| 132 | $slugConverter->convert($text, '_1', 'testTransformation2') |
||
| 133 | ); |
||
| 134 | } |
||
| 135 | |||
| 136 | public function providerForTestGetUniqueCounterValue() |
||
| 137 | { |
||
| 138 | return array( |
||
| 139 | array('reserved', true, 2), |
||
| 140 | array('reserved', false, 1), |
||
| 141 | array('not-reserved', true, 1), |
||
| 142 | array('not-reserved', false, 1), |
||
| 143 | ); |
||
| 144 | } |
||
| 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) |
||
| 153 | { |
||
| 154 | $slugConverter = $this->getMockedSlugConverter(); |
||
| 155 | |||
| 156 | $this->assertEquals( |
||
| 157 | $returnValue, |
||
| 158 | $slugConverter->getUniqueCounterValue($text, $isRootLevel) |
||
| 159 | ); |
||
| 160 | } |
||
| 161 | |||
| 162 | public function cleanupTextData() |
||
| 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) |
||
| 204 | |||
| 205 | public function convertData() |
||
| 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) |
||
| 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 |