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 ReportsControllerTest extends IntegrationTestCase |
||
| 10 | { |
||
| 11 | public $fixtures = array( |
||
| 12 | 'app.notifications', |
||
| 13 | 'app.developers', |
||
| 14 | 'app.reports', |
||
| 15 | 'app.incidents', |
||
| 16 | ); |
||
| 17 | |||
| 18 | public function setUp() |
||
| 19 | { |
||
| 20 | $this->Reports = TableRegistry::get('Reports'); |
||
|
|
|||
| 21 | $this->session(array('Developer.id' => 1)); |
||
| 22 | } |
||
| 23 | |||
| 24 | public function testIndex() |
||
| 25 | { |
||
| 26 | $this->get('/reports'); |
||
| 27 | $this->assertEquals(array('3.8', '4.0'), $this->viewVariable('distinct_versions')); |
||
| 28 | $this->assertEquals(array('forwarded', 'new'), $this->viewVariable('distinct_statuses')); |
||
| 29 | $this->assertEquals(array('error1', 'error2'), |
||
| 30 | $this->viewVariable('distinct_error_names')); |
||
| 31 | } |
||
| 32 | |||
| 33 | public function testView() |
||
| 34 | { |
||
| 35 | $this->get('/reports/view/1'); |
||
| 36 | |||
| 37 | $this->assertEquals(1, $this->viewVariable('report')[0]['id']); |
||
| 38 | $this->assertEquals('error2', $this->viewVariable('report')[0]['error_name']); |
||
| 39 | |||
| 40 | $this->assertNotEmpty($this->viewVariable('project_name')); |
||
| 41 | $this->assertNotEmpty($this->viewVariable('columns')); |
||
| 42 | |||
| 43 | $this->assertNotEmpty($this->viewVariable('related_entries')); |
||
| 44 | $this->assertEquals(count($this->viewVariable('columns')), |
||
| 45 | count($this->viewVariable('related_entries'))); |
||
| 46 | |||
| 47 | foreach ($this->viewVariable('columns') as $column) { |
||
| 48 | $this->assertNotEmpty($this->viewVariable("${column}_distinct_count")); |
||
| 49 | } |
||
| 50 | |||
| 51 | $this->assertNotEmpty($this->viewVariable('incidents')); |
||
| 52 | $this->assertEquals(1, count($this->viewVariable('incidents'))); |
||
| 53 | |||
| 54 | $this->assertNotEmpty($this->viewVariable('incidents_with_description')); |
||
| 55 | $this->assertEquals(1, count($this->viewVariable('incidents_with_description'))); |
||
| 56 | |||
| 57 | $this->assertNotEmpty($this->viewVariable('incidents_with_stacktrace')); |
||
| 58 | $this->assertEquals(1, count($this->viewVariable('incidents_with_stacktrace'))); |
||
| 59 | |||
| 60 | $this->assertNotEmpty($this->viewVariable('related_reports')); |
||
| 61 | $this->assertEquals(1, count($this->viewVariable('related_reports'))); |
||
| 62 | |||
| 63 | $this->get('/reports/view/3'); |
||
| 64 | $this->assertResponseContains('Invalid Report'); |
||
| 65 | $this->assertResponseContains('/reports/view/3'); |
||
| 66 | } |
||
| 67 | |||
| 68 | public function testDataTables() |
||
| 69 | { |
||
| 70 | $this->get('/reports/data_tables?sEcho=1&iDisplayLength=25'); |
||
| 71 | $expected = array( |
||
| 72 | 'iTotalRecords' => 4, |
||
| 73 | 'iTotalDisplayRecords' => 4, |
||
| 74 | 'sEcho' => 1, |
||
| 75 | 'aaData' => array( |
||
| 76 | array("<input type='checkbox' name='reports[]' value='1'/>", 1, 'error2', 'Lorem ipsum dolor sit amet', 'filename_1.php', '4.0', 'Forwarded', 'js', '1'), |
||
| 77 | array("<input type='checkbox' name='reports[]' value='2'/>", 2, 'error2', 'Lorem ipsum dolor sit amet', 'filename_2.php', '4.0', 'Forwarded', 'js', '1'), |
||
| 78 | array("<input type='checkbox' name='reports[]' value='4'/>", 4, 'error1', 'Lorem ipsum dolor sit amet', 'filename_3.js', '3.8', 'Forwarded', 'js', '2'), |
||
| 79 | array("<input type='checkbox' name='reports[]' value='5'/>", 5, 'error1', 'Lorem ipsum dolor sit amet', 'filename_4.js', '3.8', 'New', 'js', '1') |
||
| 80 | ), |
||
| 81 | ); |
||
| 82 | $this->assertEquals($expected, json_decode($this->_response->body(), true)); |
||
| 83 | |||
| 84 | $this->get('/reports/data_tables?sEcho=1&sSearch=error2&bSearchable_2=true&iSortCol_0=0&sSortDir_0=desc&bSortable_0=true&iSortingCols=2&iDisplayLength=25'); |
||
| 85 | $expected = array( |
||
| 86 | 'iTotalRecords' => 4, |
||
| 87 | 'iTotalDisplayRecords' => 2, |
||
| 88 | 'sEcho' => 1, |
||
| 89 | 'aaData' => array( |
||
| 90 | array("<input type='checkbox' name='reports[]' value='1'/>", 1, 'error2', 'Lorem ipsum dolor sit amet', 'filename_1.php', '4.0', 'Forwarded', 'js', '1'), |
||
| 91 | array("<input type='checkbox' name='reports[]' value='2'/>", 2, 'error2', 'Lorem ipsum dolor sit amet', 'filename_2.php', '4.0', 'Forwarded', 'js', '1'), |
||
| 92 | ), |
||
| 93 | ); |
||
| 94 | $result = json_decode($this->_response->body(), true); |
||
| 95 | $this->assertEquals($expected, $result); |
||
| 96 | |||
| 97 | $this->get('/reports/data_tables?sEcho=1&sSearch_1=1&iDisplayLength=25'); |
||
| 98 | $expected = array( |
||
| 99 | 'iTotalRecords' => 4, |
||
| 100 | 'iTotalDisplayRecords' => 1, |
||
| 101 | 'sEcho' => 1, |
||
| 102 | 'aaData' => array( |
||
| 103 | array("<input type='checkbox' name='reports[]' value='1'/>", 1, 'error2', 'Lorem ipsum dolor sit amet', 'filename_1.php', '4.0', 'Forwarded', 'js', '1'), |
||
| 104 | ), |
||
| 105 | ); |
||
| 106 | $result = json_decode($this->_response->body(), true); |
||
| 107 | $this->assertEquals($expected, $result); |
||
| 108 | |||
| 109 | $this->get('/reports/data_tables?sEcho=1&sSearch_1=error&iDisplayLength=25'); |
||
| 110 | $expected = array( |
||
| 111 | 'iTotalRecords' => 4, |
||
| 112 | 'iTotalDisplayRecords' => 0, |
||
| 113 | 'sEcho' => 1, |
||
| 114 | 'aaData' => array( |
||
| 115 | ), |
||
| 116 | ); |
||
| 117 | $result = json_decode($this->_response->body(), true); |
||
| 118 | $this->assertEquals($expected, $result); |
||
| 119 | } |
||
| 120 | |||
| 121 | View Code Duplication | public function testMarkRelatedTo() { |
|
| 137 | |||
| 138 | |||
| 139 | /** |
||
| 140 | * Don't run this as a separate test, |
||
| 141 | * as the fixture tables are re-created and |
||
| 142 | * we lose the previous related_to updations. |
||
| 143 | * |
||
| 144 | * So, call at the end of testMarkRelatedTo() |
||
| 145 | */ |
||
| 146 | View Code Duplication | private function _testUnmarkRelatedTo() |
|
| 158 | |||
| 159 | /** |
||
| 160 | * Test for 'mass_action' action |
||
| 161 | */ |
||
| 162 | public function testMassAction() |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Test for 'change_state' action |
||
| 214 | */ |
||
| 215 | public function testChangeState() |
||
| 241 | } |
||
| 242 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: