Completed
Pull Request — master (#148)
by
unknown
18:09 queued 14:27
created

DashboardTest::testDisplayResults()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 1 Features 0
Metric Value
c 4
b 1
f 0
dl 0
loc 24
rs 8.9713
cc 1
eloc 16
nc 1
nop 0
1
<?php
2
3
namespace Overwatch\TestBundle\Tests\E2E;
4
5
use Facebook\WebDriver\WebDriverBy;
6
use Overwatch\TestBundle\DataFixtures\ORM\TestFixtures;
7
use Overwatch\TestBundle\DataFixtures\ORM\TestGroupFixtures;
8
use Overwatch\UserBundle\Tests\Base\WebDriverTestCase;
9
10
/**
11
 * DashboardTest
12
 * Tests the dashboard view
13
 */
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 View Code Duplication
        foreach (['user-1', 'user-2', 'user-3'] as $user) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
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 View Code Duplication
        foreach (['user-1', 'user-2'] as $user) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
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
        $this->assertEquals($this->getHoverTextForTest(TestFixtures::$tests['test-1']), $this->getHoverTextForTest($tests[0]));
121
        $this->assertContains(TestFixtures::$tests['test-2']->getName(), $tests[1]->getText());
122
        $this->assertEquals($this->getHoverTextForTest(TestFixtures::$tests['test-2']), $this->getHoverTextForTest($tests[1]));
123
124
        $tests = $this->getGroups()[1]->findElements(
125
            WebDriverBy::cssSelector('.tests li .test')
126
        );
127
        $this->assertCount(1, $tests);
128
        $this->assertContains(TestFixtures::$tests['test-3']->getName(), $tests[0]->getText());
129
        $this->assertEquals($this->getHoverTextForTest(TestFixtures::$tests['test-3']), $this->getHoverTextForTest($tests[0]));
130
    }
131
132
    public function testDeleteGroup()
133
    {
134
        $this->logInAsUser('user-1');
135
        $this->waitForLoadingAnimation();
136
137
        $this->getGroups()[2]->findElement(
138
            //Delete group button
139
            WebDriverBy::cssSelector('.widget-header .right a:nth-child(2)')
140
        )->click();
141
142
        $this->waitForAlert();
143
        $this->webDriver->switchTo()->alert()->dismiss();
144
        $this->assertCount(3, $this->getGroups());
145
146
        $this->getGroups()[2]->findElement(
147
            //Delete group button
148
            WebDriverBy::cssSelector('.widget-header .right a:nth-child(2)')
149
        )->click();
150
151
        $this->waitForAlert();
152
        $this->webDriver->switchTo()->alert()->accept();
153
154
        $this->waitForLoadingAnimation();
155
        $this->assertCount(2, $this->getGroups());
156
    }
157
158
    public function testAddGroup()
159
    {
160
        $this->logInAsUser('user-1');
161
        $this->waitForLoadingAnimation();
162
163
        $this->getAddGroupButton()->click();
164
        $this->waitForAlert();
165
        $this->webDriver->switchTo()->alert()->dismiss();
166
        $this->assertCount(3, $this->getGroups());
167
168
        $this->getAddGroupButton()->click();
169
        $this->waitForAlert();
170
        $this->webDriver->switchTo()->alert()->sendKeys('Not quite untitled group');
171
        $this->webDriver->switchTo()->alert()->accept();
172
        $this->waitForLoadingAnimation();
173
        $this->assertCount(4, $this->getGroups());
174
        $this->assertContains('Not quite untitled group', $this->getGroups()[3]->getText());
175
    }
176
177
    public function testDeleteTest()
178
    {
179
        $this->logInAsUser('user-1');
180
        $this->waitForLoadingAnimation();
181
182
        $this->getFirstTestDeleteButton()->click();
183
        $this->waitForAlert();
184
        $this->webDriver->switchTo()->alert()->dismiss();
185
        $this->assertCount(2, $this->getTestsForGroup(0));
186
187
        $this->getFirstTestDeleteButton()->click();
188
        $this->waitForAlert();
189
        $this->webDriver->switchTo()->alert()->accept();
190
        $this->waitForLoadingAnimation();
191
        $this->assertCount(1, $this->getTestsForGroup(0));
192
    }
193
194
    public function testRunTest()
195
    {
196
        $this->logInAsUser('user-1');
197
        $this->waitForLoadingAnimation();
198
199
        $this->webDriver->findElement(
200
            //First test's run button
201
            WebDriverBy::cssSelector('.tests li:nth-child(1) a:nth-child(1)')
202
        )->click();
203
        $this->waitForLoadingAnimation();
204
205
        $this->assertContains(TestFixtures::$tests['test-1']->getName(), $this->getHeaderText());
206
        $this->assertCount(4, $this->webDriver->findElements(
207
            WebDriverBy::cssSelector('.result span')
208
        ));
209
    }
210
211
    private function getGroups()
212
    {
213
        return $this->webDriver->findElements(
214
            WebDriverBy::cssSelector('.groups .widget-box')
215
        );
216
    }
217
218
    private function getTestsForGroup($group)
219
    {
220
        return $this->getGroups()[$group]->findElements(
221
            WebDriverBy::cssSelector('.tests li.ng-scope')
222
        );
223
    }
224
225
    private function getHoverTextForTest($test)
226
    {
227
        //If passed a fixture, construct expected value
228
        if ($test instanceof \Overwatch\TestBundle\Entity\Test) {
229
            return 'Expect ' . $test->getActual() . ' ' . $test->getExpectation() . ' ' . $test->getExpected();
230
        }
231
232
        //Else, find actual hover text
233
        return $test->findElement(
234
            WebDriverBy::tagName('span')
235
        )->getAttribute('title');
236
    }
237
238
    private function getAddGroupButton()
239
    {
240
        return $this->webDriver->findElement(
241
            WebDriverBy::cssSelector('.groups .add-group a')
242
        );
243
    }
244
245
    private function getFirstTestDeleteButton()
246
    {
247
        return $this->webDriver->findElement(
248
            WebDriverBy::cssSelector('.tests li:nth-child(1) a:nth-child(4)')
249
        );
250
    }
251
}
252