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 |
||
| 18 | class RulebookTest extends \PHPUnit_Framework_TestCase |
||
| 19 | { |
||
| 20 | /** |
||
| 21 | * @var \sebastianfeldmann\CaptainHook\Git\DummyRepo |
||
| 22 | */ |
||
| 23 | private $repo; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Setup dummy repo. |
||
| 27 | */ |
||
| 28 | public function setUp() |
||
| 29 | { |
||
| 30 | $this->repo = new DummyRepo(); |
||
| 31 | $this->repo->setup(); |
||
| 32 | } |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Cleanup dummy repo. |
||
| 36 | */ |
||
| 37 | public function tearDown() |
||
| 38 | { |
||
| 39 | $this->repo->cleanup(); |
||
| 40 | } |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Tests Rulebook::execute |
||
| 44 | */ |
||
| 45 | public function testExecute() |
||
| 46 | { |
||
| 47 | $io = new NullIO(); |
||
| 48 | $config = new Config(HMU_PATH_FILES . '/captainhook.json'); |
||
| 49 | $action = new Config\Action('php', '\\sebastianfeldmann\\CaptainHook\\Hook\\Message\\Beams'); |
||
| 50 | $repo = new Repository($this->repo->getPath()); |
||
| 51 | $repo->setCommitMsg(new CommitMessage('Foo bar baz')); |
||
| 52 | |||
| 53 | $standard = new Beams(); |
||
| 54 | $standard->execute($config, $io, $repo, $action); |
||
| 55 | } |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Tests Rulebook::execute |
||
| 59 | * |
||
| 60 | * @expectedException \Exception |
||
| 61 | */ |
||
| 62 | public function testExecuteClassNotFound() |
||
| 63 | { |
||
| 64 | $io = new NullIO(); |
||
| 65 | $config = new Config(HMU_PATH_FILES . '/captainhook.json'); |
||
| 66 | $repo = new Repository($this->repo->getPath()); |
||
| 67 | $action = new Config\Action( |
||
| 68 | 'php', |
||
| 69 | '\\sebastianfeldmann\\CaptainHook\\Hook\\Message\\Rulebook', |
||
| 70 | ['\\sebastianfeldmann\\CaptainHook\\Foo'] |
||
| 71 | ); |
||
| 72 | |||
| 73 | $standard = new Rulebook(); |
||
| 74 | $standard->execute($config, $io, $repo, $action); |
||
| 75 | } |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Tests Rulebook::execute |
||
| 79 | * |
||
| 80 | * @expectedException \Exception |
||
| 81 | */ |
||
| 82 | public function testExecuteInvalidClass() |
||
| 83 | { |
||
| 84 | $io = new NullIO(); |
||
| 85 | $config = new Config(HMU_PATH_FILES . '/captainhook.json'); |
||
| 86 | $action = new Config\Action( |
||
| 87 | 'php', |
||
| 88 | '\\sebastianfeldmann\\CaptainHook\\Hook\\Message\\Rulebook', |
||
| 89 | ['\\sebastianfeldmann\\CaptainHook\\Hook\\Message\\Validator'] |
||
| 90 | ); |
||
| 91 | $repo = new Repository($this->repo->getPath()); |
||
| 92 | |||
| 93 | $standard = new Rulebook(); |
||
| 94 | $standard->execute($config, $io, $repo, $action); |
||
| 95 | } |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Tests Rulebook::execute |
||
| 99 | */ |
||
| 100 | public function testExecuteValidRule() |
||
| 101 | { |
||
| 102 | $io = new NullIO(); |
||
| 103 | $config = new Config(HMU_PATH_FILES . '/captainhook.json'); |
||
| 104 | $action = new Config\Action( |
||
| 105 | 'php', |
||
| 106 | '\\sebastianfeldmann\\CaptainHook\\Hook\\Message\\Rulebook', |
||
| 107 | ['\\sebastianfeldmann\\CaptainHook\\Hook\\Message\\Validator\\Rule\\CapitalizeSubject'] |
||
| 108 | ); |
||
| 109 | $repo = new Repository($this->repo->getPath()); |
||
| 110 | $repo->setCommitMsg(new CommitMessage('Foo bar baz')); |
||
| 111 | |||
| 112 | $standard = new Rulebook(); |
||
| 113 | $standard->execute($config, $io, $repo, $action); |
||
| 114 | } |
||
| 115 | } |
||
| 116 |