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 |
||
| 17 | class InstallTest extends \PHPUnit_Framework_TestCase |
||
| 18 | { |
||
| 19 | /** |
||
| 20 | * Tests Install::run |
||
| 21 | * |
||
| 22 | * @expectedException \Exception |
||
| 23 | */ |
||
| 24 | public function testExecuteNoConfig() |
||
| 25 | { |
||
| 26 | $input = new ArrayInput( |
||
| 27 | [ |
||
| 28 | 'hook' => 'pre-commit', |
||
| 29 | '--configuration' => 'foo', |
||
| 30 | '--git-directory' => 'bar' |
||
| 31 | ] |
||
| 32 | ); |
||
| 33 | $output = new DummyOutput(); |
||
| 34 | $install = new Install(); |
||
| 35 | $install->setIO(new NullIO()); |
||
| 36 | $install->run($input, $output); |
||
| 37 | } |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Tests Install::run |
||
| 41 | * |
||
| 42 | * @expectedException \Exception |
||
| 43 | */ |
||
| 44 | public function testExecuteInvalidRepository() |
||
| 45 | { |
||
| 46 | $input = new ArrayInput( |
||
| 47 | [ |
||
| 48 | 'hook' => 'pre-commit', |
||
| 49 | '--configuration' => HMU_PATH_FILES . '/config/valid.json', |
||
| 50 | '--git-directory' => 'bar/.git' |
||
| 51 | ] |
||
| 52 | ); |
||
| 53 | |||
| 54 | $output = new DummyOutput(); |
||
| 55 | $install = new Install(); |
||
| 56 | $install->setIO(new NullIO()); |
||
| 57 | $install->run($input, $output); |
||
| 58 | } |
||
| 59 | |||
| 60 | |||
| 61 | /** |
||
| 62 | * Tests Install::run |
||
| 63 | */ |
||
| 64 | public function testExecutePreCommit() |
||
| 65 | { |
||
| 66 | $repo = new DummyRepo(); |
||
| 67 | $repo->setup(); |
||
| 68 | |||
| 69 | $install = new Install(); |
||
| 70 | $output = new DummyOutput(); |
||
| 71 | $input = new ArrayInput( |
||
| 72 | [ |
||
| 73 | 'hook' => 'pre-commit', |
||
| 74 | '--configuration' => HMU_PATH_FILES . '/config/valid.json', |
||
| 75 | '--git-directory' => $repo->getGitDir() |
||
| 76 | ] |
||
| 77 | ); |
||
| 78 | |||
| 79 | $install->setIO(new NullIO()); |
||
| 80 | $install->run($input, $output); |
||
| 81 | |||
| 82 | // make sure the file is installed |
||
| 83 | $this->assertTrue( |
||
| 84 | file_exists( |
||
| 85 | $repo->getGitDir() . DIRECTORY_SEPARATOR . 'hooks' . DIRECTORY_SEPARATOR . 'pre-commit' |
||
| 86 | ) |
||
| 87 | ); |
||
| 88 | |||
| 89 | $repo->cleanup(); |
||
| 90 | } |
||
| 91 | } |
||
| 92 |