|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Overwatch\UserBundle\Tests\Entity; |
|
4
|
|
|
|
|
5
|
|
|
use Overwatch\ResultBundle\Entity\TestResult; |
|
6
|
|
|
use Overwatch\ResultBundle\Enum\ResultStatus; |
|
7
|
|
|
use Overwatch\TestBundle\Entity\TestGroup; |
|
8
|
|
|
use Overwatch\UserBundle\Entity\User; |
|
9
|
|
|
use Overwatch\UserBundle\Enum\AlertSetting; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* UserTest |
|
13
|
|
|
* A unit test for the User Entity. |
|
14
|
|
|
*/ |
|
15
|
|
|
class UserTest extends \PHPUnit_Framework_TestCase |
|
16
|
|
|
{ |
|
17
|
|
|
private $user; |
|
18
|
|
|
|
|
19
|
|
|
const EMAIL = '[email protected]'; |
|
20
|
|
|
const TELNO = '+441111111111'; |
|
21
|
|
|
|
|
22
|
|
|
public function setUp() |
|
23
|
|
|
{ |
|
24
|
|
|
$this->user = new User; |
|
25
|
|
|
$this->user->setEmail(self::EMAIL); |
|
26
|
|
|
$this->user->setTelephoneNumber(self::TELNO); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
public function testValid() |
|
30
|
|
|
{ |
|
31
|
|
|
$this->user->setAlertSetting(AlertSetting::CHANGE_BAD); |
|
32
|
|
|
|
|
33
|
|
|
$this->assertEquals(self::EMAIL, $this->user->getEmail()); |
|
34
|
|
|
$this->assertEquals(self::EMAIL, $this->user->getUsername()); |
|
35
|
|
|
$this->assertEquals(AlertSetting::CHANGE_BAD, $this->user->getAlertSetting()); |
|
36
|
|
|
$this->assertEquals(self::TELNO, $this->user->getTelephoneNumber()); |
|
37
|
|
|
$this->assertJsonStringEqualsJsonString( |
|
38
|
|
|
json_encode([ |
|
39
|
|
|
'id' => null, |
|
40
|
|
|
'email' => self::EMAIL, |
|
41
|
|
|
'alertSetting' => AlertSetting::CHANGE_BAD, |
|
42
|
|
|
'telephoneNumber' => self::TELNO, |
|
43
|
|
|
'lastLogin' => '', |
|
44
|
|
|
'locked' => false, |
|
45
|
|
|
'roles' => ['ROLE_USER'] |
|
46
|
|
|
]), |
|
47
|
|
|
json_encode($this->user) |
|
48
|
|
|
); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @expectedException InvalidArgumentException |
|
53
|
|
|
*/ |
|
54
|
|
|
public function testSetAlertSettingInvalid() |
|
55
|
|
|
{ |
|
56
|
|
|
$this->user->setAlertSetting(10); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
View Code Duplication |
public function testShouldBeAlertedReturnsFalseWithSettingNone() |
|
|
|
|
|
|
60
|
|
|
{ |
|
61
|
|
|
$this->user->setAlertSetting(AlertSetting::NONE); |
|
62
|
|
|
|
|
63
|
|
|
foreach (ResultStatus::getAll() as $status) { |
|
64
|
|
|
$result = new TestResult(); |
|
65
|
|
|
$result |
|
66
|
|
|
->setStatus($status) |
|
67
|
|
|
->setInfo('Stahp shut up and take my money scumbag stacy trollface.') |
|
68
|
|
|
; |
|
69
|
|
|
|
|
70
|
|
|
$this->assertFalse($this->user->shouldBeAlerted($result)); |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
View Code Duplication |
public function testShouldBeAlertedReturnsTrueWithSettingAll() |
|
|
|
|
|
|
75
|
|
|
{ |
|
76
|
|
|
$this->user->setAlertSetting(AlertSetting::ALL); |
|
77
|
|
|
|
|
78
|
|
|
foreach (ResultStatus::getAll() as $status) { |
|
79
|
|
|
$result = $this->getMockResult(); |
|
80
|
|
|
$result |
|
81
|
|
|
->setStatus($status) |
|
82
|
|
|
->setInfo('Stahp shut up and take my money scumbag stacy trollface.') |
|
83
|
|
|
; |
|
84
|
|
|
|
|
85
|
|
|
$this->assertTrue($this->user->shouldBeAlerted($result)); |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
View Code Duplication |
public function testShouldBeAlertedWithChangingFailedResult() |
|
|
|
|
|
|
90
|
|
|
{ |
|
91
|
|
|
$result = $this->getMockResult(true); |
|
92
|
|
|
$result |
|
93
|
|
|
->setStatus(ResultStatus::FAILED) |
|
94
|
|
|
->setInfo('Stahp shut up and take my money scumbag stacy trollface.') |
|
95
|
|
|
; |
|
96
|
|
|
|
|
97
|
|
|
$this->user->setAlertSetting(AlertSetting::CHANGE_BAD); |
|
98
|
|
|
$this->assertTrue($this->user->shouldBeAlerted($result)); |
|
99
|
|
|
|
|
100
|
|
|
$this->user->setAlertSetting(AlertSetting::CHANGE); |
|
101
|
|
|
$this->assertTrue($this->user->shouldBeAlerted($result)); |
|
102
|
|
|
|
|
103
|
|
|
$this->user->setAlertSetting(AlertSetting::CHANGE_ALL); |
|
104
|
|
|
$this->assertTrue($this->user->shouldBeAlerted($result)); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
View Code Duplication |
public function testShouldBeAlertedWithChangingPassedResult() |
|
|
|
|
|
|
108
|
|
|
{ |
|
109
|
|
|
$result = $this->getMockResult(true); |
|
110
|
|
|
$result |
|
111
|
|
|
->setStatus(ResultStatus::PASSED) |
|
112
|
|
|
->setInfo('Stahp shut up and take my money scumbag stacy trollface.') |
|
113
|
|
|
; |
|
114
|
|
|
|
|
115
|
|
|
$this->user->setAlertSetting(AlertSetting::CHANGE_BAD); |
|
116
|
|
|
$this->assertFalse($this->user->shouldBeAlerted($result)); |
|
117
|
|
|
|
|
118
|
|
|
$this->user->setAlertSetting(AlertSetting::CHANGE); |
|
119
|
|
|
$this->assertTrue($this->user->shouldBeAlerted($result)); |
|
120
|
|
|
|
|
121
|
|
|
$this->user->setAlertSetting(AlertSetting::CHANGE_ALL); |
|
122
|
|
|
$this->assertTrue($this->user->shouldBeAlerted($result)); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
View Code Duplication |
public function testShouldBeAlertedWithUnchangingFailedResult() |
|
|
|
|
|
|
126
|
|
|
{ |
|
127
|
|
|
$result = $this->getMockResult(); |
|
128
|
|
|
$result |
|
129
|
|
|
->setStatus(ResultStatus::FAILED) |
|
130
|
|
|
->setInfo('Stahp shut up and take my money scumbag stacy trollface.') |
|
131
|
|
|
; |
|
132
|
|
|
|
|
133
|
|
|
$this->user->setAlertSetting(AlertSetting::CHANGE_BAD); |
|
134
|
|
|
$this->assertFalse($this->user->shouldBeAlerted($result)); |
|
135
|
|
|
|
|
136
|
|
|
$this->user->setAlertSetting(AlertSetting::CHANGE); |
|
137
|
|
|
$this->assertFalse($this->user->shouldBeAlerted($result)); |
|
138
|
|
|
|
|
139
|
|
|
$this->user->setAlertSetting(AlertSetting::CHANGE_ALL); |
|
140
|
|
|
$this->assertTrue($this->user->shouldBeAlerted($result)); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
View Code Duplication |
public function testShouldBeAlertedWithUnchangingPassedResult() |
|
|
|
|
|
|
144
|
|
|
{ |
|
145
|
|
|
$result = $this->getMockResult(); |
|
146
|
|
|
$result |
|
147
|
|
|
->setStatus(ResultStatus::PASSED) |
|
148
|
|
|
->setInfo('Stahp shut up and take my money scumbag stacy trollface.') |
|
149
|
|
|
; |
|
150
|
|
|
|
|
151
|
|
|
$this->user->setAlertSetting(AlertSetting::CHANGE_BAD); |
|
152
|
|
|
$this->assertFalse($this->user->shouldBeAlerted($result)); |
|
153
|
|
|
|
|
154
|
|
|
$this->user->setAlertSetting(AlertSetting::CHANGE); |
|
155
|
|
|
$this->assertFalse($this->user->shouldBeAlerted($result)); |
|
156
|
|
|
|
|
157
|
|
|
$this->user->setAlertSetting(AlertSetting::CHANGE_ALL); |
|
158
|
|
|
$this->assertFalse($this->user->shouldBeAlerted($result)); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
View Code Duplication |
public function testShouldBeAlertedWithUserNotInResultGroup() |
|
|
|
|
|
|
162
|
|
|
{ |
|
163
|
|
|
$result = $this->getMockResult(true, false); |
|
164
|
|
|
$result |
|
165
|
|
|
->setStatus(ResultStatus::FAILED) |
|
166
|
|
|
->setInfo('Stahp shut up and take my money scumbag stacy trollface.') |
|
167
|
|
|
; |
|
168
|
|
|
|
|
169
|
|
|
foreach (AlertSetting::getAll() as $setting => $description) { |
|
170
|
|
|
$this->user->setAlertSetting($setting); |
|
171
|
|
|
$this->assertFalse($this->user->shouldBeAlerted($result)); |
|
172
|
|
|
} |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
private function getMockTest($userInGroup = true) |
|
176
|
|
|
{ |
|
177
|
|
|
$group = new TestGroup; |
|
178
|
|
|
$group->setName('MemeGroup1'); |
|
179
|
|
|
|
|
180
|
|
|
$test = $this->getMock('Overwatch\TestBundle\Entity\Test', ['getGroup']); |
|
181
|
|
|
$test->method('getGroup')->willReturn($group); |
|
182
|
|
|
|
|
183
|
|
|
if ($userInGroup === true) { |
|
184
|
|
|
$group->addUser($this->user); |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
return $test; |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
private function getMockResult($isAChange = false, $userInGroup = true) |
|
191
|
|
|
{ |
|
192
|
|
|
$result = $this->getMock('Overwatch\ResultBundle\Entity\TestResult', ['isAChange', 'getTest']); |
|
193
|
|
|
$result->method('isAChange')->willReturn($isAChange); |
|
194
|
|
|
$result->method('getTest')->willReturn($this->getMockTest($userInGroup)); |
|
195
|
|
|
|
|
196
|
|
|
return $result; |
|
197
|
|
|
} |
|
198
|
|
|
} |
|
199
|
|
|
|
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.