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 HashTest extends \PHPUnit_Framework_TestCase { |
||
| 8 | |||
| 9 | public static function provideMix() { |
||
| 10 | $data = array( |
||
| 11 | array(array(), ''), |
||
| 12 | array(array('1', '1'), '0d'), |
||
| 13 | array(array('a'), '61'), |
||
| 14 | // This expects 'b' because of how the mock hmac function works |
||
| 15 | array(array('a', 'b'), '9a'), |
||
| 16 | array(array('aa', 'ba'), '6e84'), |
||
| 17 | array(array('ab', 'bb'), 'b0cb'), |
||
| 18 | array(array('aa', 'bb'), 'ae8d'), |
||
| 19 | array(array('aa', 'bb', 'cc'), 'a14c'), |
||
| 20 | array(array('aabbcc', 'bbccdd', 'ccddee'), 'a8aff3939934'), |
||
| 21 | ); |
||
| 22 | return $data; |
||
| 23 | } |
||
| 24 | |||
| 25 | public function testConstructWithoutArgument() { |
||
| 26 | $hash = new Hash; |
||
| 27 | $this->assertTrue($hash instanceof \RandomLib\Mixer); |
||
| 28 | } |
||
| 29 | |||
| 30 | public function testGetStrength() { |
||
| 31 | $strength = new Strength(Strength::MEDIUM); |
||
| 32 | $actual = Hash::getStrength(); |
||
| 33 | $this->assertEquals($actual, $strength); |
||
| 34 | } |
||
| 35 | |||
| 36 | public function testTest() { |
||
| 37 | $actual = Hash::test(); |
||
| 38 | $this->assertTrue($actual); |
||
| 39 | } |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @dataProvider provideMix |
||
| 43 | */ |
||
| 44 | public function testMix($parts, $result) { |
||
| 45 | $mixer = new Hash('md5'); |
||
| 46 | $actual = $mixer->mix($parts); |
||
| 47 | $this->assertEquals($result, bin2hex($actual)); |
||
| 48 | } |
||
| 49 | |||
| 50 | |||
| 51 | } |
||
| 52 |