1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace JhUserTest\Controller; |
4
|
|
|
|
5
|
|
|
use JhUser\Entity\User; |
6
|
|
|
use JhUser\Entity\HierarchicalRole; |
7
|
|
|
use Zend\Console\Request; |
8
|
|
|
use Zend\Http\Request as HttpRequest; |
9
|
|
|
use Zend\Test\PHPUnit\Controller\AbstractConsoleControllerTestCase; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class RoleControllerTest |
13
|
|
|
* @package JhUserTest\Controller |
14
|
|
|
* @author Aydin Hassan <[email protected]> |
15
|
|
|
*/ |
16
|
|
|
class RoleControllerTest extends AbstractConsoleControllerTestCase |
17
|
|
|
{ |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var User |
21
|
|
|
*/ |
22
|
|
|
protected $user; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var Role |
26
|
|
|
*/ |
27
|
|
|
protected $role; |
28
|
|
|
|
29
|
|
|
|
30
|
|
|
public function setUp() |
31
|
|
|
{ |
32
|
|
|
$this->setApplicationConfig( |
33
|
|
|
include __DIR__ . "/../../TestConfiguration.php.dist" |
34
|
|
|
); |
35
|
|
|
parent::setUp(); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function testUsersRoleCanBeSet() |
39
|
|
|
{ |
40
|
|
|
$email = '[email protected]'; |
41
|
|
|
$roleId = 'admin'; |
42
|
|
|
|
43
|
|
|
$this->configureMocks($email, $roleId); |
44
|
|
|
|
45
|
|
|
$this->dispatch(new Request(['scriptname.php', "set role $email $roleId"])); |
46
|
|
|
|
47
|
|
|
$this->assertResponseStatusCode(0); |
48
|
|
|
$this->assertModuleName('jhuser'); |
49
|
|
|
$this->assertControllerName('jhuser\controller\role'); |
50
|
|
|
$this->assertControllerClass('rolecontroller'); |
51
|
|
|
$this->assertActionName('set-role'); |
52
|
|
|
$this->assertMatchedRouteName('set-role'); |
53
|
|
|
|
54
|
|
|
$this->assertCount(1, $this->user->getRoles()); |
55
|
|
|
$this->assertSame($this->user->getRoles()[0], $this->role); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function testUsersRoleCanBeModified() |
59
|
|
|
{ |
60
|
|
|
$email = '[email protected]'; |
61
|
|
|
$roleId = 'admin'; |
62
|
|
|
|
63
|
|
|
$this->configureMocks($email, $roleId); |
64
|
|
|
$role = new HierarchicalRole; |
65
|
|
|
$role->setName('user'); |
66
|
|
|
$this->user->addRole($role); |
67
|
|
|
|
68
|
|
|
$this->dispatch(new Request(['scriptname.php', "set role $email $roleId"])); |
69
|
|
|
|
70
|
|
|
$this->assertResponseStatusCode(0); |
71
|
|
|
$this->assertModuleName('jhuser'); |
72
|
|
|
$this->assertControllerName('jhuser\controller\role'); |
73
|
|
|
$this->assertControllerClass('rolecontroller'); |
74
|
|
|
$this->assertActionName('set-role'); |
75
|
|
|
$this->assertMatchedRouteName('set-role'); |
76
|
|
|
|
77
|
|
|
$this->assertCount(1, $this->user->getRoles()); |
78
|
|
|
$this->assertSame($this->user->getRoles()[1], $this->role); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function configureMocks($userEmail, $roleId) |
82
|
|
|
{ |
83
|
|
|
$userRepoMock = $this->getMock('JhUser\Repository\UserRepositoryInterface'); |
84
|
|
|
$roleRepoMock = $this->getMock('JhUser\Repository\RoleRepositoryInterface'); |
85
|
|
|
$objectManagerMock = $this->getMock('Doctrine\Common\Persistence\ObjectManager'); |
86
|
|
|
|
87
|
|
|
$this->user = new User(); |
88
|
|
|
$this->user->setEmail($userEmail); |
89
|
|
|
|
90
|
|
|
$userRepoMock |
91
|
|
|
->expects($this->once()) |
92
|
|
|
->method('findOneByEmail') |
93
|
|
|
->with($userEmail) |
94
|
|
|
->will($this->returnValue($this->user)); |
95
|
|
|
|
96
|
|
|
$this->role = new HierarchicalRole(); |
|
|
|
|
97
|
|
|
$this->role->setName($roleId); |
98
|
|
|
|
99
|
|
|
$roleRepoMock |
100
|
|
|
->expects($this->once()) |
101
|
|
|
->method('findByName') |
102
|
|
|
->with($roleId) |
103
|
|
|
->will($this->returnValue($this->role)); |
104
|
|
|
|
105
|
|
|
$objectManagerMock |
106
|
|
|
->expects($this->once()) |
107
|
|
|
->method('flush'); |
108
|
|
|
|
109
|
|
|
$serviceManager = $this->getApplicationServiceLocator(); |
110
|
|
|
$serviceManager->setAllowOverride(true); |
111
|
|
|
$serviceManager->setService('JhUser\Repository\UserRepository', $userRepoMock); |
112
|
|
|
$serviceManager->setService('JhUser\Repository\RoleRepository', $roleRepoMock); |
113
|
|
|
$serviceManager->setService('JhUser\ObjectManager', $objectManagerMock); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
|
117
|
|
|
public function testExceptionIsThrownIfRoleNotExists() |
118
|
|
|
{ |
119
|
|
|
$email = '[email protected]'; |
120
|
|
|
$roleId = 'admin'; |
121
|
|
|
|
122
|
|
|
$userRepoMock = $this->getMock('JhUser\Repository\UserRepositoryInterface'); |
123
|
|
|
$roleRepoMock = $this->getMock('JhUser\Repository\RoleRepositoryInterface'); |
124
|
|
|
$objectManagerMock = $this->getMock('Doctrine\Common\Persistence\ObjectManager'); |
125
|
|
|
|
126
|
|
|
$user = new User(); |
127
|
|
|
$user->setEmail($email); |
128
|
|
|
|
129
|
|
|
$userRepoMock |
130
|
|
|
->expects($this->once()) |
131
|
|
|
->method('findOneByEmail') |
132
|
|
|
->with($email) |
133
|
|
|
->will($this->returnValue($user)); |
134
|
|
|
|
135
|
|
|
$roleRepoMock |
136
|
|
|
->expects($this->once()) |
137
|
|
|
->method('findByName') |
138
|
|
|
->with($roleId) |
139
|
|
|
->will($this->returnValue(null)); |
140
|
|
|
|
141
|
|
|
$objectManagerMock->expects($this->never())->method('flush'); |
142
|
|
|
|
143
|
|
|
$serviceManager = $this->getApplicationServiceLocator(); |
144
|
|
|
$serviceManager->setAllowOverride(true); |
145
|
|
|
$serviceManager->setService('JhUser\Repository\UserRepository', $userRepoMock); |
146
|
|
|
$serviceManager->setService('JhUser\Repository\RoleRepository', $roleRepoMock); |
147
|
|
|
$serviceManager->setService('JhUser\ObjectManager', $objectManagerMock); |
148
|
|
|
|
149
|
|
|
$this->dispatch(new Request(['scriptname.php', "set role $email $roleId"])); |
150
|
|
|
$this->assertResponseStatusCode(1); |
151
|
|
|
$this->assertModuleName('jhuser'); |
152
|
|
|
$this->assertControllerName('jhuser\controller\role'); |
153
|
|
|
$this->assertControllerClass('rolecontroller'); |
154
|
|
|
$this->assertActionName('set-role'); |
155
|
|
|
$this->assertMatchedRouteName('set-role'); |
156
|
|
|
$this->assertApplicationException('RuntimeException', sprintf('Role: "%s" could not be found', $roleId)); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
public function testExceptionIsThrownIfUserNotExists() |
160
|
|
|
{ |
161
|
|
|
$email = '[email protected]'; |
162
|
|
|
$roleId = 'admin'; |
163
|
|
|
|
164
|
|
|
$userRepoMock = $this->getMock('JhUser\Repository\UserRepositoryInterface'); |
165
|
|
|
$roleRepoMock = $this->getMock('JhUser\Repository\RoleRepositoryInterface'); |
166
|
|
|
$objectManagerMock = $this->getMock('Doctrine\Common\Persistence\ObjectManager'); |
167
|
|
|
|
168
|
|
|
$userRepoMock |
169
|
|
|
->expects($this->once()) |
170
|
|
|
->method('findOneByEmail') |
171
|
|
|
->with($email) |
172
|
|
|
->will($this->returnValue(null)); |
173
|
|
|
|
174
|
|
|
$roleRepoMock->expects($this->never())->method('findByName'); |
175
|
|
|
$objectManagerMock->expects($this->never())->method('flush'); |
176
|
|
|
|
177
|
|
|
$serviceManager = $this->getApplicationServiceLocator(); |
178
|
|
|
$serviceManager->setAllowOverride(true); |
179
|
|
|
$serviceManager->setService('JhUser\Repository\UserRepository', $userRepoMock); |
180
|
|
|
$serviceManager->setService('JhUser\Repository\RoleRepository', $roleRepoMock); |
181
|
|
|
$serviceManager->setService('JhUser\ObjectManager', $objectManagerMock); |
182
|
|
|
|
183
|
|
|
$this->dispatch(new Request(['scriptname.php', "set role $email $roleId"])); |
184
|
|
|
$this->assertResponseStatusCode(1); |
185
|
|
|
$this->assertModuleName('jhuser'); |
186
|
|
|
$this->assertControllerName('jhuser\controller\role'); |
187
|
|
|
$this->assertControllerClass('rolecontroller'); |
188
|
|
|
$this->assertActionName('set-role'); |
189
|
|
|
$this->assertMatchedRouteName('set-role'); |
190
|
|
|
$this->assertApplicationException( |
191
|
|
|
'RuntimeException', |
192
|
|
|
sprintf('User with email: "%s" could not be found', $email) |
193
|
|
|
); |
194
|
|
|
} |
195
|
|
|
} |
196
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..