Failed Conditions
Pull Request — master (#148)
by
unknown
15:01
created

DashboardTest::testResultAgeWarningShown()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 27
Code Lines 16

Duplication

Lines 11
Ratio 40.74 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 11
loc 27
rs 8.8571
cc 2
eloc 16
nc 2
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
121
        $expect = $this->getGroups()[0]->findElements(
122
            WebDriverBy::tagName('.tests li .test .test-expect')
123
        );
124
        $this->assertEquals($expect[0]->getText());
0 ignored issues
show
Bug introduced by
The call to assertEquals() misses a required argument $actual.

This check looks for function calls that miss required arguments.

Loading history...
125
126
        $this->assertContains(TestFixtures::$tests['test-2']->getName(), $tests[1]->getText());
127
        $this->assertEquals($this->getHoverTextForTest(TestFixtures::$tests['test-2']), $this->getHoverTextForTest($tests[1]));
128
129
        $tests = $this->getGroups()[1]->findElements(
130
            WebDriverBy::cssSelector('.tests li .test')
131
        );
132
        $this->assertCount(1, $tests);
133
        $this->assertContains(TestFixtures::$tests['test-3']->getName(), $tests[0]->getText());
134
        $this->assertEquals($this->getHoverTextForTest(TestFixtures::$tests['test-3']), $this->getHoverTextForTest($tests[0]));
135
    }
136
137
    public function testDeleteGroup()
138
    {
139
        $this->logInAsUser('user-1');
140
        $this->waitForLoadingAnimation();
141
142
        $this->getGroups()[2]->findElement(
143
            //Delete group button
144
            WebDriverBy::cssSelector('.widget-header .right a:nth-child(2)')
145
        )->click();
146
147
        $this->waitForAlert();
148
        $this->webDriver->switchTo()->alert()->dismiss();
149
        $this->assertCount(3, $this->getGroups());
150
151
        $this->getGroups()[2]->findElement(
152
            //Delete group button
153
            WebDriverBy::cssSelector('.widget-header .right a:nth-child(2)')
154
        )->click();
155
156
        $this->waitForAlert();
157
        $this->webDriver->switchTo()->alert()->accept();
158
159
        $this->waitForLoadingAnimation();
160
        $this->assertCount(2, $this->getGroups());
161
    }
162
163
    public function testAddGroup()
164
    {
165
        $this->logInAsUser('user-1');
166
        $this->waitForLoadingAnimation();
167
168
        $this->getAddGroupButton()->click();
169
        $this->waitForAlert();
170
        $this->webDriver->switchTo()->alert()->dismiss();
171
        $this->assertCount(3, $this->getGroups());
172
173
        $this->getAddGroupButton()->click();
174
        $this->waitForAlert();
175
        $this->webDriver->switchTo()->alert()->sendKeys('Not quite untitled group');
176
        $this->webDriver->switchTo()->alert()->accept();
177
        $this->waitForLoadingAnimation();
178
        $this->assertCount(4, $this->getGroups());
179
        $this->assertContains('Not quite untitled group', $this->getGroups()[3]->getText());
180
    }
181
182
    public function testDeleteTest()
183
    {
184
        $this->logInAsUser('user-1');
185
        $this->waitForLoadingAnimation();
186
187
        $this->getFirstTestDeleteButton()->click();
188
        $this->waitForAlert();
189
        $this->webDriver->switchTo()->alert()->dismiss();
190
        $this->assertCount(2, $this->getTestsForGroup(0));
191
192
        $this->getFirstTestDeleteButton()->click();
193
        $this->waitForAlert();
194
        $this->webDriver->switchTo()->alert()->accept();
195
        $this->waitForLoadingAnimation();
196
        $this->assertCount(1, $this->getTestsForGroup(0));
197
    }
198
199
    public function testRunTest()
200
    {
201
        $this->logInAsUser('user-1');
202
        $this->waitForLoadingAnimation();
203
204
        $this->webDriver->findElement(
205
            //First test's run button
206
            WebDriverBy::cssSelector('.tests li:nth-child(1) a:nth-child(1)')
207
        )->click();
208
        $this->waitForLoadingAnimation();
209
210
        $this->assertContains(TestFixtures::$tests['test-1']->getName(), $this->getHeaderText());
211
        $this->assertCount(4, $this->webDriver->findElements(
212
            WebDriverBy::cssSelector('.result span')
213
        ));
214
    }
215
216
    private function getGroups()
217
    {
218
        return $this->webDriver->findElements(
219
            WebDriverBy::cssSelector('.groups .widget-box')
220
        );
221
    }
222
223
    private function getTestsForGroup($group)
224
    {
225
        return $this->getGroups()[$group]->findElements(
226
            WebDriverBy::cssSelector('.tests li.ng-scope')
227
        );
228
    }
229
230
    private function getHoverTextForTest($test)
231
    {
232
        //If passed a fixture, construct expected value
233
        if ($test instanceof \Overwatch\TestBundle\Entity\Test) {
234
            return 'Expect ' . $test->getActual() . ' ' . $test->getExpectation() . ' ' . $test->getExpected();
235
        }
236
237
        //Else, find actual hover text
238
        return $test->findElement(
239
            WebDriverBy::tagName('span')
240
        )->getAttribute('title');
241
    }
242
243
    private function getAddGroupButton()
244
    {
245
        return $this->webDriver->findElement(
246
            WebDriverBy::cssSelector('.groups .add-group a')
247
        );
248
    }
249
250
    private function getFirstTestDeleteButton()
251
    {
252
        return $this->webDriver->findElement(
253
            WebDriverBy::cssSelector('.tests li:nth-child(1) a:nth-child(4)')
254
        );
255
    }
256
}
257