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 |
||
| 9 | class OptionParserTest extends \PHPUnit_Framework_TestCase |
||
| 10 | { |
||
| 11 | |||
| 12 | public function testReadme1() |
||
| 13 | { |
||
| 14 | $lql = <<<EOF |
||
| 15 | GET contacts |
||
| 16 | OutputFormat: json |
||
| 17 | ResponseHeader: fixed16 |
||
| 18 | |||
| 19 | |||
| 20 | EOF; |
||
| 21 | |||
| 22 | $mock = \TestMock::on( |
||
| 23 | new LqlBuilder(Table::CONTACTS) |
||
| 24 | ); |
||
| 25 | |||
| 26 | $this->assertEquals( |
||
| 27 | $lql, |
||
| 28 | $mock->build(), |
||
| 29 | 'Retrieve all contacts.' |
||
| 30 | ); |
||
| 31 | } |
||
| 32 | |||
| 33 | |||
| 34 | public function testReadme2() |
||
| 35 | { |
||
| 36 | $lql = <<<EOF |
||
| 37 | GET contacts |
||
| 38 | Columns: name alias |
||
| 39 | ColumnHeaders: on |
||
| 40 | OutputFormat: json |
||
| 41 | ResponseHeader: fixed16 |
||
| 42 | |||
| 43 | |||
| 44 | EOF; |
||
| 45 | |||
| 46 | $mock = \TestMock::on( |
||
| 47 | new LqlBuilder(Table::CONTACTS) |
||
| 48 | ); |
||
| 49 | $mock->columns(array('name', 'alias')); |
||
| 50 | |||
| 51 | $this->assertEquals( |
||
| 52 | $lql, |
||
| 53 | $mock->build(), |
||
| 54 | 'Retrieves just the columns name and alias.' |
||
| 55 | ); |
||
| 56 | } |
||
| 57 | |||
| 58 | public function testReadme3() |
||
| 59 | { |
||
| 60 | $lql = <<<EOF |
||
| 61 | GET services |
||
| 62 | Columns: host_name description state |
||
| 63 | ColumnHeaders: on |
||
| 64 | Filter: state = 2 |
||
| 65 | OutputFormat: json |
||
| 66 | ResponseHeader: fixed16 |
||
| 67 | |||
| 68 | |||
| 69 | EOF; |
||
| 70 | |||
| 71 | $mock = \TestMock::on( |
||
| 72 | new LqlBuilder(Table::SERVICES) |
||
| 73 | ); |
||
| 74 | $mock->columns(array('host_name', 'description', 'state')) |
||
| 75 | ->filterEqual('state', '2'); |
||
| 76 | |||
| 77 | $this->assertEquals( |
||
| 78 | $lql, |
||
| 79 | $mock->build(), |
||
| 80 | 'Gets all services with the current state 2 (critical).' |
||
| 81 | ); |
||
| 82 | } |
||
| 83 | |||
| 84 | public function testReadme4() |
||
| 85 | { |
||
| 86 | $lql = <<<EOF |
||
| 87 | GET services |
||
| 88 | Columns: host_name description state |
||
| 89 | ColumnHeaders: on |
||
| 90 | Filter: state = 2 |
||
| 91 | Filter: in_notification_period = 1 |
||
| 92 | OutputFormat: json |
||
| 93 | ResponseHeader: fixed16 |
||
| 94 | |||
| 95 | |||
| 96 | EOF; |
||
| 97 | |||
| 98 | $mock = \TestMock::on( |
||
| 99 | new LqlBuilder(Table::SERVICES) |
||
| 100 | ); |
||
| 101 | $mock->columns(array('host_name', 'description', 'state')) |
||
| 102 | ->filterEqual('state', '2') |
||
| 103 | ->filterEqual('in_notification_period', '1'); |
||
| 104 | |||
| 105 | $this->assertEquals( |
||
| 106 | $lql, |
||
| 107 | $mock->build(), |
||
| 108 | 'Gets all critical services which are currently within their notification period.' |
||
| 109 | ); |
||
| 110 | } |
||
| 111 | } |
||
| 112 |