Completed
Pull Request — master (#151)
by Zac
14:02
created

ViewTestTest   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 120
Duplicated Lines 14.17 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 3
Bugs 1 Features 0
Metric Value
wmc 12
c 3
b 1
f 0
lcom 1
cbo 8
dl 17
loc 120
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A testTest1Results() 0 18 1
A testTest2Results() 0 12 1
A testRunTest() 0 10 1
A testDeleteTest() 0 16 1
A testEditDeleteAndRunInsufficentPermissions() 9 9 1
A testEditDeleteAndRunAsAdmin() 8 8 1
A clickThroughToTest() 0 7 1
A getResultsOnPage() 0 10 1
A getActionItem() 0 6 1
A assertTimestampEquals() 0 12 3

How to fix   Duplicated Code   

Duplicated Code

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
2
3
namespace Overwatch\ResultBundle\Tests\E2E;
4
5
use Facebook\WebDriver\WebDriverBy;
6
use Overwatch\ResultBundle\DataFixtures\ORM\TestResultFixtures;
7
use Overwatch\TestBundle\DataFixtures\ORM\TestFixtures;
8
use Overwatch\UserBundle\Tests\Base\WebDriverTestCase;
9
10
/**
11
 * ViewTestTest
12
 * Tests the View Test page
13
 */
14
class ViewTestTest extends WebDriverTestCase
15
{
16
    public function testTest1Results()
17
    {
18
        $this->logInAsUser('user-1');
19
        $this->clickThroughToTest(1);
20
21
        $this->waitForLoadingAnimation();
22
        $this->assertContains(TestFixtures::$tests['test-1']->getName(), $this->getHeaderText());
23
        $this->assertCount(3, $this->getResultsOnPage());
24
        $this->assertEquals(TestResultFixtures::$results['result-3']->getInfo(), $this->getResultsOnPage()[0]->getText());
25
        $this->assertTimestampEquals(TestResultFixtures::$results['result-3']->getCreatedAt(), $this->getResultsOnPage(' a')[0]->getAttribute('title'));
26
        $this->assertContains(strtolower(TestResultFixtures::$results['result-3']->getStatus()), $this->getResultsOnPage('')[0]->getAttribute('class'));
27
        $this->assertEquals(TestResultFixtures::$results['result-2']->getInfo(), $this->getResultsOnPage()[1]->getText());
28
        $this->assertTimestampEquals(TestResultFixtures::$results['result-2']->getCreatedAt(), $this->getResultsOnPage(' a')[1]->getAttribute('title'));
29
        $this->assertContains(strtolower(TestResultFixtures::$results['result-2']->getStatus()), $this->getResultsOnPage('')[1]->getAttribute('class'));
30
        $this->assertEquals(TestResultFixtures::$results['result-1']->getInfo(), $this->getResultsOnPage()[2]->getText());
31
        $this->assertTimestampEquals(TestResultFixtures::$results['result-1']->getCreatedAt(), $this->getResultsOnPage(' a')[2]->getAttribute('title'));
32
        $this->assertContains(strtolower(TestResultFixtures::$results['result-1']->getStatus()), $this->getResultsOnPage('')[2]->getAttribute('class'));
33
    }
34
35
    public function testTest2Results()
36
    {
37
        $this->logInAsUser('user-1');
38
        $this->clickThroughToTest(2);
39
40
        $this->waitForLoadingAnimation();
41
        $this->assertContains(TestFixtures::$tests['test-2']->getName(), $this->getHeaderText());
42
        $this->assertCount(1, $this->getResultsOnPage());
43
        $this->assertEquals(TestResultFixtures::$results['result-4']->getInfo(), $this->getResultsOnPage()[0]->getText());
44
        $this->assertTimestampEquals(TestResultFixtures::$results['result-4']->getCreatedAt(), $this->getResultsOnPage(' a')[0]->getAttribute('title'));
45
        $this->assertContains(strtolower(TestResultFixtures::$results['result-4']->getStatus()), $this->getResultsOnPage('')[0]->getAttribute('class'));
46
    }
47
48
    public function testDeleteTest()
49
    {
50
        $this->logInAsUser('user-1');
51
        $this->clickThroughToTest(1);
52
53
        $this->waitForLoadingAnimation();
54
        $this->getActionItem(2)->click();
55
56
        $this->waitForAlert();
57
        $this->webDriver->switchTo()->alert()->accept();
58
59
        $this->waitForLoadingAnimation();
60
        $this->assertCount(1, $this->webDriver->findElements(
61
            WebDriverBy::cssSelector('.groups .widget-box:first-child .tests li div.test')
62
        ));
63
    }
64
65
    public function testRunTest()
66
    {
67
        $this->logInAsUser('user-1');
68
        $this->clickThroughToTest(1);
69
70
        $this->waitForLoadingAnimation();
71
        $this->getActionItem(3)->click();
72
        $this->waitForLoadingAnimation();
73
        $this->assertCount(4, $this->getResultsOnPage());
74
    }
75
76 View Code Duplication
    public function testEditDeleteAndRunInsufficentPermissions()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
77
    {
78
        $this->logInAsUser('user-2');
79
        $this->clickThroughToTest(1);
80
81
        $this->assertFalse($this->getActionItem(1)->isDisplayed());
82
        $this->assertFalse($this->getActionItem(2)->isDisplayed());
83
        $this->assertFalse($this->getActionItem(3)->isDisplayed());
84
    }
85
    
86 View Code Duplication
    public function testEditDeleteAndRunAsAdmin() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
87
        $this->logInAsUser('user-3');
88
        $this->clickThroughToTest(1);
89
90
        $this->assertFalse($this->getActionItem(1)->isDisplayed());
91
        $this->assertFalse($this->getActionItem(2)->isDisplayed());
92
        $this->assertFalse($this->getActionItem(3)->isDisplayed());
93
    }
94
95
    private function clickThroughToTest($number)
96
    {
97
        $this->waitForLoadingAnimation();
98
        $this->webDriver->findElement(
99
            WebDriverBy::cssSelector('.tests li:nth-child(' . $number . ') .test a:nth-child(2)')
100
        )->click();
101
    }
102
103
    private function getResultsOnPage($selector = ' span')
104
    {
105
        $selector = '.result' . $selector;
106
107
        $results = $this->webDriver->findElements(
108
            WebDriverBy::cssSelector($selector)
109
        );
110
111
        return $results;
112
    }
113
114
    private function getActionItem($number)
115
    {
116
        return $this->webDriver->findElement(
117
            WebDriverBy::cssSelector('.widget-content .row a:nth-child(' . $number . ')')
118
        );
119
    }
120
121
    private function assertTimestampEquals($expected, $actual)
122
    {
123
        if (!$expected instanceof \DateTime) {
124
            $expected = new \DateTime($expected);
125
        }
126
127
        if (!$actual instanceof \DateTime) {
128
            $actual = new \DateTime($actual);
129
        }
130
131
        $this->assertEquals($expected, $actual);
132
    }
133
}
134