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 |
||
| 12 | class HookTest extends \PHPUnit_Framework_TestCase |
||
| 13 | { |
||
| 14 | /** |
||
| 15 | * Tests Hook::__construct |
||
| 16 | */ |
||
| 17 | public function testDisabledByDefault() |
||
| 18 | { |
||
| 19 | $hook = new Hook(); |
||
| 20 | $config = $hook->getJsonData(); |
||
| 21 | |||
| 22 | $this->assertEquals(false, $hook->isEnabled()); |
||
| 23 | $this->assertEquals(false, $config['enabled']); |
||
| 24 | } |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Tests Hook::setEnabled |
||
| 28 | */ |
||
| 29 | public function testSetEnabled() |
||
| 30 | { |
||
| 31 | $hook = new Hook(); |
||
| 32 | $hook->setEnabled(true); |
||
| 33 | $config = $hook->getJsonData(); |
||
| 34 | |||
| 35 | $this->assertEquals(true, $hook->isEnabled()); |
||
| 36 | $this->assertEquals(true, $config['enabled']); |
||
| 37 | } |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Tests Hook::__construct |
||
| 41 | */ |
||
| 42 | public function testEmptyActions() |
||
| 43 | { |
||
| 44 | $hook = new Hook(); |
||
| 45 | $config = $hook->getJsonData(); |
||
| 46 | |||
| 47 | $this->assertEquals(0, count($hook->getActions())); |
||
| 48 | $this->assertEquals(0, count($config['actions'])); |
||
| 49 | } |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Tests Hook::addAction |
||
| 53 | */ |
||
| 54 | public function testAddAction() |
||
| 55 | { |
||
| 56 | $hook = new Hook(); |
||
| 57 | $hook->addAction(new Action('php', '\\Foo\\Bar')); |
||
| 58 | $config = $hook->getJsonData(); |
||
| 59 | |||
| 60 | $this->assertEquals(1, count($hook->getActions())); |
||
| 61 | $this->assertEquals(1, count($config['actions'])); |
||
| 62 | } |
||
| 63 | } |
||
| 64 |