Failed Conditions
Push — master ( b9670c...4ba57f )
by Zac
16:20 queued 37s
created

DashboardTest::testDisplayGroupsAndTests()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 19
rs 9.4285
cc 1
eloc 14
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('.group a:nth-child(2)')
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(2)')
56
        )->isDisplayed());
57
        $this->assertFalse($this->webDriver->findElement(
58
            //Add test button
59
            WebDriverBy::cssSelector('ul.tests li:last-child a:nth-child(2)')
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
        $tests = $this->getGroups()[0]->findElements(
113
            WebDriverBy::cssSelector('.tests li.ng-scope div.passed')
114
        );
115
        
116
        $this->assertCount(2, $tests);
117
        $this->assertContains(TestFixtures::$tests['test-1']->getName(), $tests[0]->getText());
118
        $this->assertEquals($this->getHoverTextForTest(TestFixtures::$tests['test-1']), $this->getHoverTextForTest($tests[0]));
119
        $this->assertContains(TestFixtures::$tests['test-2']->getName(), $tests[1]->getText());
120
        $this->assertEquals($this->getHoverTextForTest(TestFixtures::$tests['test-2']), $this->getHoverTextForTest($tests[1]));
121
        
122
        $tests = $this->getGroups()[1]->findElements(
123
            WebDriverBy::cssSelector('.tests li.ng-scope div')
124
        );
125
        $this->assertCount(1, $tests);
126
        $this->assertContains(TestFixtures::$tests['test-3']->getName(), $tests[0]->getText());
127
        $this->assertEquals($this->getHoverTextForTest(TestFixtures::$tests['test-3']), $this->getHoverTextForTest($tests[0]));
128
    }
129
    
130
    public function testDeleteGroup()
131
    {
132
        $this->logInAsUser('user-1');
133
        $this->waitForLoadingAnimation();
134
        
135
        $this->getGroups()[2]->findElement(
136
            //Delete group button
137
            WebDriverBy::cssSelector('ul.groups li:nth-child(3) li div a:nth-child(1)')
138
        )->click();
139
        
140
        $this->waitForAlert();
141
        $this->webDriver->switchTo()->alert()->dismiss();
142
        $this->assertCount(3, $this->getGroups());
143
        
144
        $this->getGroups()[2]->findElement(
145
            //Delete group button
146
            WebDriverBy::cssSelector('ul.groups li:nth-child(3) li div a:nth-child(1)')
147
        )->click();
148
        
149
        $this->waitForAlert();
150
        $this->webDriver->switchTo()->alert()->accept();
151
        
152
        $this->waitForLoadingAnimation();
153
        $this->assertCount(2, $this->getGroups());
154
    }
155
    
156
    public function testAddGroup()
157
    {
158
        $this->logInAsUser('user-1');
159
        $this->waitForLoadingAnimation();
160
        
161
        $this->getAddGroupButton()->click();
162
        $this->waitForAlert();
163
        $this->webDriver->switchTo()->alert()->dismiss();
164
        $this->assertCount(3, $this->getGroups());
165
        
166
        $this->getAddGroupButton()->click();
167
        $this->waitForAlert();
168
        $this->webDriver->switchTo()->alert()->sendKeys('Not quite untitled group');
169
        $this->webDriver->switchTo()->alert()->accept();
170
        $this->waitForLoadingAnimation();
171
        $this->assertCount(4, $this->getGroups());
172
        $this->assertContains('Not quite untitled group', $this->getGroups()[3]->getText());
173
    }
174
    
175
    public function testDeleteTest()
176
    {
177
        $this->logInAsUser('user-1');
178
        $this->waitForLoadingAnimation();
179
        
180
        $this->getFirstTestDeleteButton()->click();
181
        $this->waitForAlert();
182
        $this->webDriver->switchTo()->alert()->dismiss();
183
        $this->assertCount(2, $this->getTestsForGroup(0));
184
        
185
        $this->getFirstTestDeleteButton()->click();
186
        $this->waitForAlert();
187
        $this->webDriver->switchTo()->alert()->accept();
188
        $this->waitForLoadingAnimation();
189
        $this->assertCount(1, $this->getTestsForGroup(0));
190
    }
191
    
192
    public function testRunTest()
193
    {
194
        $this->logInAsUser('user-1');
195
        $this->waitForLoadingAnimation();
196
        
197
        $this->webDriver->findElement(
198
            //First test's run button
199
            WebDriverBy::cssSelector('ul.tests li:nth-child(1) a:nth-child(4)')
200
        )->click();
201
        $this->waitForLoadingAnimation();
202
        
203
        $this->assertEquals(TestFixtures::$tests['test-1']->getName(), $this->getHeaderText());
204
        $this->assertCount(4, $this->webDriver->findElements(
205
            WebDriverBy::cssSelector('.result span')
206
        ));
207
    }
208
    
209
    private function getGroups()
210
    {
211
        return $this->webDriver->findElements(
212
            WebDriverBy::cssSelector('.groups > li.ng-scope')
213
        );
214
    }
215
    
216
    private function getTestsForGroup($group)
217
    {
218
        return $this->getGroups()[$group]->findElements(
219
            WebDriverBy::cssSelector('.tests li.ng-scope')
220
        );
221
    }
222
    
223
    private function getHoverTextForTest($test)
224
    {
225
        //If passed a fixture, construct expected value
226
        if ($test instanceof \Overwatch\TestBundle\Entity\Test) {
227
            return 'Expect ' . $test->getActual() . ' ' . $test->getExpectation() . ' ' . $test->getExpected();
228
        }
229
        
230
        //Else, find actual hover text
231
        return $test->findElement(
232
            WebDriverBy::tagName('span')
233
        )->getAttribute('title');
234
    }
235
    
236
    private function getAddGroupButton()
237
    {
238
        return $this->webDriver->findElement(
239
            WebDriverBy::cssSelector('ul.groups > li:last-child a')
240
        );
241
    }
242
    
243
    private function getFirstTestDeleteButton()
244
    {
245
        return $this->webDriver->findElement(
246
            WebDriverBy::cssSelector('ul.tests li:nth-child(1) a:nth-child(2)')
247
        );
248
    }
249
}
250