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 |
||
| 16 | class ValidatorTest extends \PHPUnit_Framework_TestCase |
||
| 17 | { |
||
| 18 | /** |
||
| 19 | * Tests Validator::validate |
||
| 20 | */ |
||
| 21 | public function testValidOnEmptyRuleList() |
||
| 22 | { |
||
| 23 | $msg = new CommitMessage('Foo'); |
||
| 24 | $v = new Validator(); |
||
| 25 | |||
| 26 | $v->validate($msg); |
||
| 27 | $this->assertTrue(true); |
||
| 28 | |||
| 29 | } |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Tests Validator::setRules |
||
| 33 | */ |
||
| 34 | public function testSetRulesValid() |
||
| 35 | { |
||
| 36 | $msg = new CommitMessage('Foo'); |
||
| 37 | $v = new Validator(); |
||
| 38 | $v->setRules([new MsgNotEmpty()]); |
||
| 39 | |||
| 40 | $v->validate($msg); |
||
| 41 | $this->assertTrue(true); |
||
| 42 | } |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Tests Validator::setRules |
||
| 46 | * |
||
| 47 | * @expectedException \Exception |
||
| 48 | */ |
||
| 49 | public function testSetRulesInvalid() |
||
| 50 | { |
||
| 51 | $msg = new CommitMessage(''); |
||
| 52 | $v = new Validator(); |
||
| 53 | $v->setRules([new MsgNotEmpty()]); |
||
| 54 | $v->validate($msg); |
||
| 55 | } |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Tests Validator::setRules |
||
| 59 | * |
||
| 60 | * @expectedException \Exception |
||
| 61 | */ |
||
| 62 | public function testAddRuleInvalid() |
||
| 63 | { |
||
| 64 | $msg = new CommitMessage('foo bar baz'); |
||
| 65 | $v = new Validator(); |
||
| 66 | $v->setRules([new MsgNotEmpty()]); |
||
| 67 | $v->addRule(new CapitalizeSubject()); |
||
| 68 | |||
| 69 | $v->validate($msg); |
||
| 70 | } |
||
| 71 | } |
||
| 72 |