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  | 
            ||
| 14 | class DashboardTest extends WebDriverTestCase  | 
            ||
| 15 | { | 
            ||
| 16 | public function testDisplayGroupsAndTests()  | 
            ||
| 17 |     { | 
            ||
| 18 |         $this->logInAsUser('user-1'); | 
            ||
| 19 | $this->waitForLoadingAnimation();  | 
            ||
| 20 | |||
| 21 |         $this->assertEquals('Dashboard', $this->getHeaderText()); | 
            ||
| 22 | $this->assertCount(3, $this->getGroups());  | 
            ||
| 23 | $this->assertCount(2, $this->getTestsForGroup(0));  | 
            ||
| 24 | $this->assertCount(1, $this->getTestsForGroup(1));  | 
            ||
| 25 | $this->assertCount(0, $this->getTestsForGroup(2));  | 
            ||
| 26 | |||
| 27 | $this->assertContains(TestGroupFixtures::$groups['group-1']->getName(), $this->getGroups()[0]->getText());  | 
            ||
| 28 | $this->assertContains(TestGroupFixtures::$groups['group-2']->getName(), $this->getGroups()[1]->getText());  | 
            ||
| 29 | $this->assertContains(TestGroupFixtures::$groups['group-3']->getName(), $this->getGroups()[2]->getText());  | 
            ||
| 30 | |||
| 31 |         $this->assertContains('2 users', $this->getGroups()[0]->getText()); | 
            ||
| 32 |         $this->assertContains('1 user', $this->getGroups()[1]->getText()); | 
            ||
| 33 |         $this->assertContains('0 users', $this->getGroups()[2]->getText()); | 
            ||
| 34 | }  | 
            ||
| 35 | |||
| 36 | public function testDisplayGroupsAndTestsAsUser()  | 
            ||
| 37 |     { | 
            ||
| 38 |         $this->logInAsUser('user-2'); | 
            ||
| 39 | $this->waitForLoadingAnimation();  | 
            ||
| 40 | |||
| 41 |         $this->assertEquals('Dashboard', $this->getHeaderText()); | 
            ||
| 42 | $this->assertCount(1, $this->getGroups());  | 
            ||
| 43 | $this->assertCount(2, $this->getTestsForGroup(0));  | 
            ||
| 44 | |||
| 45 | $this->assertContains(TestGroupFixtures::$groups['group-1']->getName(), $this->getGroups()[0]->getText());  | 
            ||
| 46 |         $this->assertContains('2 users', $this->getGroups()[0]->getText()); | 
            ||
| 47 | |||
| 48 | $this->assertFalse($this->webDriver->findElement(  | 
            ||
| 49 | //Edit group button  | 
            ||
| 50 |             WebDriverBy::cssSelector('.groups .widget-box:first-child .widget-header .right a:first-child') | 
            ||
| 51 | )->isDisplayed());  | 
            ||
| 52 | $this->assertFalse($this->getFirstTestDeleteButton()->isDisplayed());  | 
            ||
| 53 | $this->assertFalse($this->webDriver->findElement(  | 
            ||
| 54 | //Second test's delete button  | 
            ||
| 55 |             WebDriverBy::cssSelector('ul.tests li:nth-child(2) a:nth-child(4)') | 
            ||
| 56 | )->isDisplayed());  | 
            ||
| 57 | $this->assertFalse($this->webDriver->findElement(  | 
            ||
| 58 | //Add test button  | 
            ||
| 59 |             WebDriverBy::cssSelector('ul.tests li:last-child a:first-child') | 
            ||
| 60 | )->isDisplayed());  | 
            ||
| 61 | $this->assertFalse($this->getAddGroupButton()->isDisplayed());  | 
            ||
| 62 | }  | 
            ||
| 63 | |||
| 64 | public function testResultAgeWarningHidden()  | 
            ||
| 65 |     { | 
            ||
| 66 |         foreach (['user-1', 'user-2', 'user-3'] as $user) { | 
            ||
| 67 | $this->logInAsUser($user);  | 
            ||
| 68 | $this->waitForLoadingAnimation();  | 
            ||
| 69 | |||
| 70 | $this->assertFalse($this->webDriver->findElement(  | 
            ||
| 71 | //Test Result Age Warning  | 
            ||
| 72 |                 WebDriverBy::cssSelector("div[data-ng-show='shouldWarnOfTestAge()']") | 
            ||
| 73 | )->isDisplayed());  | 
            ||
| 74 | |||
| 75 |             $this->webDriver->get('http://127.0.0.1:8000/logout'); | 
            ||
| 76 | }  | 
            ||
| 77 | }  | 
            ||
| 78 | |||
| 79 | public function testResultAgeWarningShown()  | 
            ||
| 80 |     { | 
            ||
| 81 |         $query = "UPDATE TestResult SET created_at = '" . date('Y-m-d H:i:s', time() - (10 * 60 * 60)) . "' WHERE 1"; | 
            ||
| 82 | $sql = $this->em->getConnection()->prepare($query);  | 
            ||
| 83 | $sql->execute();  | 
            ||
| 84 | |||
| 85 |         foreach (['user-1', 'user-2'] as $user) { | 
            ||
| 86 | $this->logInAsUser($user);  | 
            ||
| 87 | $this->waitForLoadingAnimation();  | 
            ||
| 88 | |||
| 89 | $this->assertTrue($this->webDriver->findElement(  | 
            ||
| 90 | //Test Result Age Warning  | 
            ||
| 91 |                 WebDriverBy::cssSelector("div[data-ng-show='shouldWarnOfTestAge()']") | 
            ||
| 92 | )->isDisplayed());  | 
            ||
| 93 | |||
| 94 |             $this->webDriver->get('http://127.0.0.1:8000/logout'); | 
            ||
| 95 | }  | 
            ||
| 96 | |||
| 97 | //User 3 has no results, so should not be shown the message  | 
            ||
| 98 |         $this->logInAsUser('user-3'); | 
            ||
| 99 | $this->waitForLoadingAnimation();  | 
            ||
| 100 | |||
| 101 | $this->assertFalse($this->webDriver->findElement(  | 
            ||
| 102 | //Test Result Age Warning  | 
            ||
| 103 |             WebDriverBy::cssSelector("div[data-ng-show='shouldWarnOfTestAge()']") | 
            ||
| 104 | )->isDisplayed());  | 
            ||
| 105 | }  | 
            ||
| 106 | |||
| 107 | public function testDisplayResults()  | 
            ||
| 108 |     { | 
            ||
| 109 |         $this->logInAsUser('user-1'); | 
            ||
| 110 | $this->waitForLoadingAnimation();  | 
            ||
| 111 | |||
| 112 | $this->assertCount(2, $this->getGroups()[0]->findElements(  | 
            ||
| 113 |             WebDriverBy::cssSelector('.tests li .test .status.passed') | 
            ||
| 114 | ));  | 
            ||
| 115 | |||
| 116 | $tests = $this->getGroups()[0]->findElements(  | 
            ||
| 117 |             WebDriverBy::cssSelector('.tests li .test') | 
            ||
| 118 | );  | 
            ||
| 119 | $this->assertContains(TestFixtures::$tests['test-1']->getName(), $tests[0]->getText());  | 
            ||
| 120 | |||
| 121 | $this->assertEquals($this->getTextForTest(TestFixtures::$tests['test-1']), $this->getTextForTest($tests[0]));  | 
            ||
| 122 | |||
| 123 | $this->assertContains(TestFixtures::$tests['test-2']->getName(), $tests[1]->getText());  | 
            ||
| 124 | $this->assertEquals($this->getHoverTextForTest(TestFixtures::$tests['test-2']), $this->getHoverTextForTest($tests[1]));  | 
            ||
| 125 | |||
| 126 | $tests = $this->getGroups()[1]->findElements(  | 
            ||
| 127 |             WebDriverBy::cssSelector('.tests li .test') | 
            ||
| 128 | );  | 
            ||
| 129 | $this->assertCount(1, $tests);  | 
            ||
| 130 | $this->assertContains(TestFixtures::$tests['test-3']->getName(), $tests[0]->getText());  | 
            ||
| 131 | $this->assertEquals($this->getHoverTextForTest(TestFixtures::$tests['test-3']), $this->getHoverTextForTest($tests[0]));  | 
            ||
| 132 | }  | 
            ||
| 133 | |||
| 134 | public function testDeleteGroup()  | 
            ||
| 135 |     { | 
            ||
| 136 |         $this->logInAsUser('user-1'); | 
            ||
| 137 | $this->waitForLoadingAnimation();  | 
            ||
| 138 | |||
| 139 | $this->getGroups()[2]->findElement(  | 
            ||
| 140 | //Delete group button  | 
            ||
| 141 |             WebDriverBy::cssSelector('.widget-header .right a:nth-child(2)') | 
            ||
| 142 | )->click();  | 
            ||
| 143 | |||
| 144 | $this->waitForAlert();  | 
            ||
| 145 | $this->webDriver->switchTo()->alert()->dismiss();  | 
            ||
| 146 | $this->assertCount(3, $this->getGroups());  | 
            ||
| 147 | |||
| 148 | $this->getGroups()[2]->findElement(  | 
            ||
| 149 | //Delete group button  | 
            ||
| 150 |             WebDriverBy::cssSelector('.widget-header .right a:nth-child(2)') | 
            ||
| 151 | )->click();  | 
            ||
| 152 | |||
| 153 | $this->waitForAlert();  | 
            ||
| 154 | $this->webDriver->switchTo()->alert()->accept();  | 
            ||
| 155 | |||
| 156 | $this->waitForLoadingAnimation();  | 
            ||
| 157 | $this->assertCount(2, $this->getGroups());  | 
            ||
| 158 | }  | 
            ||
| 159 | |||
| 160 | public function testAddGroup()  | 
            ||
| 161 |     { | 
            ||
| 162 |         $this->logInAsUser('user-1'); | 
            ||
| 163 | $this->waitForLoadingAnimation();  | 
            ||
| 164 | |||
| 165 | $this->getAddGroupButton()->click();  | 
            ||
| 166 | $this->waitForAlert();  | 
            ||
| 167 | $this->webDriver->switchTo()->alert()->dismiss();  | 
            ||
| 168 | $this->assertCount(3, $this->getGroups());  | 
            ||
| 169 | |||
| 170 | $this->getAddGroupButton()->click();  | 
            ||
| 171 | $this->waitForAlert();  | 
            ||
| 172 |         $this->webDriver->switchTo()->alert()->sendKeys('Not quite untitled group'); | 
            ||
| 173 | $this->webDriver->switchTo()->alert()->accept();  | 
            ||
| 174 | $this->waitForLoadingAnimation();  | 
            ||
| 175 | $this->assertCount(4, $this->getGroups());  | 
            ||
| 176 |         $this->assertContains('Not quite untitled group', $this->getGroups()[3]->getText()); | 
            ||
| 177 | }  | 
            ||
| 178 | |||
| 179 | public function testDeleteTest()  | 
            ||
| 180 |     { | 
            ||
| 181 |         $this->logInAsUser('user-1'); | 
            ||
| 182 | $this->waitForLoadingAnimation();  | 
            ||
| 183 | |||
| 184 | $this->getFirstTestDeleteButton()->click();  | 
            ||
| 185 | $this->waitForAlert();  | 
            ||
| 186 | $this->webDriver->switchTo()->alert()->dismiss();  | 
            ||
| 187 | $this->assertCount(2, $this->getTestsForGroup(0));  | 
            ||
| 188 | |||
| 189 | $this->getFirstTestDeleteButton()->click();  | 
            ||
| 190 | $this->waitForAlert();  | 
            ||
| 191 | $this->webDriver->switchTo()->alert()->accept();  | 
            ||
| 192 | $this->waitForLoadingAnimation();  | 
            ||
| 193 | $this->assertCount(1, $this->getTestsForGroup(0));  | 
            ||
| 194 | }  | 
            ||
| 195 | |||
| 196 | public function testRunTest()  | 
            ||
| 197 |     { | 
            ||
| 198 |         $this->logInAsUser('user-1'); | 
            ||
| 199 | $this->waitForLoadingAnimation();  | 
            ||
| 200 | |||
| 201 | $this->webDriver->findElement(  | 
            ||
| 202 | //First test's run button  | 
            ||
| 203 |             WebDriverBy::cssSelector('.tests li:nth-child(1) a:nth-child(1)') | 
            ||
| 204 | )->click();  | 
            ||
| 205 | $this->waitForLoadingAnimation();  | 
            ||
| 206 | |||
| 207 | $this->assertContains(TestFixtures::$tests['test-1']->getName(), $this->getHeaderText());  | 
            ||
| 208 | $this->assertCount(4, $this->webDriver->findElements(  | 
            ||
| 209 |             WebDriverBy::cssSelector('.result span') | 
            ||
| 210 | ));  | 
            ||
| 211 | }  | 
            ||
| 212 | |||
| 213 | private function getGroups()  | 
            ||
| 214 |     { | 
            ||
| 215 | return $this->webDriver->findElements(  | 
            ||
| 216 |             WebDriverBy::cssSelector('.groups .widget-box') | 
            ||
| 217 | );  | 
            ||
| 218 | }  | 
            ||
| 219 | |||
| 220 | private function getTestsForGroup($group)  | 
            ||
| 221 |     { | 
            ||
| 222 | return $this->getGroups()[$group]->findElements(  | 
            ||
| 223 |             WebDriverBy::cssSelector('.tests li.ng-scope') | 
            ||
| 224 | );  | 
            ||
| 225 | }  | 
            ||
| 226 | |||
| 227 | private function getTextForTest($test)  | 
            ||
| 228 |     { | 
            ||
| 229 | //If passed a fixture, construct expected value  | 
            ||
| 230 |         if ($test instanceof \Overwatch\TestBundle\Entity\Test) { | 
            ||
| 
                                                                                                    
                        
                         | 
                |||
| 231 | return 'Expect ' . $test->getActual() . ' ' . $test->getExpectation() . ' ' . $test->getExpected();  | 
            ||
| 232 | }  | 
            ||
| 233 | |||
| 234 | //Else, find text  | 
            ||
| 235 | return $test->findElement(  | 
            ||
| 236 |             WebDriverBy::cssSelector('p.test-expect') | 
            ||
| 237 | )->getText();  | 
            ||
| 238 | }  | 
            ||
| 239 | |||
| 240 | private function getHoverTextForTest($test)  | 
            ||
| 241 |     { | 
            ||
| 242 | //If passed a fixture, construct expected value  | 
            ||
| 243 |         if ($test instanceof \Overwatch\TestBundle\Entity\Test) { | 
            ||
| 244 | return 'Expect ' . $test->getActual() . ' ' . $test->getExpectation() . ' ' . $test->getExpected();  | 
            ||
| 245 | }  | 
            ||
| 246 | |||
| 247 | //Else, find actual hover text  | 
            ||
| 248 | return $test->findElement(  | 
            ||
| 249 |             WebDriverBy::tagName('span') | 
            ||
| 250 |         )->getAttribute('title'); | 
            ||
| 267 |