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 |
||
| 7 | class McryptRijndael128Test extends \PHPUnit_Framework_TestCase { |
||
| 8 | |||
| 9 | public static function provideMix() { |
||
| 10 | $data = array( |
||
| 11 | array(array(), ''), |
||
| 12 | array(array('', ''), ''), |
||
| 13 | array(array('a'), '61'), |
||
| 14 | array(array('a', 'b'), '6a'), |
||
| 15 | array(array('aa', 'ba'), '688d'), |
||
| 16 | array(array('ab', 'bb'), 'f8bc'), |
||
| 17 | array(array('aa', 'bb'), 'a0f3'), |
||
| 18 | array(array('aa', 'bb', 'cc'), '87c3'), |
||
| 19 | array(array('aabbcc', 'bbccdd', 'ccddee'), '7cf2273e46c7'), |
||
| 20 | ); |
||
| 21 | return $data; |
||
| 22 | } |
||
| 23 | |||
| 24 | protected function setUp() { |
||
| 25 | if (!extension_loaded('mcrypt')) { |
||
| 26 | $this->markTestSkipped('mcrypt extension is not available'); |
||
| 27 | } |
||
| 28 | } |
||
| 29 | |||
| 30 | public function testConstructWithoutArgument() { |
||
| 31 | $hash = new McryptRijndael128; |
||
| 32 | $this->assertTrue($hash instanceof \RandomLib\Mixer); |
||
| 33 | } |
||
| 34 | |||
| 35 | public function testGetStrength() { |
||
| 36 | $strength = new Strength(Strength::HIGH); |
||
| 37 | $actual = McryptRijndael128::getStrength(); |
||
| 38 | $this->assertEquals($actual, $strength); |
||
| 39 | } |
||
| 40 | |||
| 41 | public function testTest() { |
||
| 42 | $actual = McryptRijndael128::test(); |
||
| 43 | $this->assertTrue($actual); |
||
| 44 | } |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @dataProvider provideMix |
||
| 48 | */ |
||
| 49 | public function testMix($parts, $result) { |
||
| 50 | $mixer = new McryptRijndael128; |
||
| 51 | $actual = $mixer->mix($parts); |
||
| 52 | $this->assertEquals($result, bin2hex($actual)); |
||
| 53 | } |
||
| 54 | } |
||
| 55 |