1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* Copyright (C) 2015 Michael Giesler |
4
|
|
|
* |
5
|
|
|
* This file is part of Dembelo. |
6
|
|
|
* |
7
|
|
|
* Dembelo is free software: you can redistribute it and/or modify |
8
|
|
|
* it under the terms of the GNU Affero General Public License as published by |
9
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
10
|
|
|
* (at your option) any later version. |
11
|
|
|
* |
12
|
|
|
* Dembelo is distributed in the hope that it will be useful, |
13
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15
|
|
|
* GNU Affero General Public License 3 for more details. |
16
|
|
|
* |
17
|
|
|
* You should have received a copy of the GNU Affero General Public License 3 |
18
|
|
|
* along with Dembelo. If not, see <http://www.gnu.org/licenses/>. |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @package DembeloMain |
24
|
|
|
*/ |
25
|
|
|
namespace DembeloMain\Tests\Controller; |
26
|
|
|
|
27
|
|
|
use DembeloMain\Document\User; |
28
|
|
|
use DembeloMain\Model\Repository\UserRepositoryInterface; |
29
|
|
|
use Doctrine\ODM\MongoDB\DocumentManager; |
30
|
|
|
use PHPUnit\Framework\TestCase; |
31
|
|
|
use DembeloMain\Controller\UserController; |
32
|
|
|
use Symfony\Bundle\FrameworkBundle\Routing\Router; |
33
|
|
|
use Symfony\Component\Form\FormFactoryInterface; |
34
|
|
|
use Symfony\Component\Form\FormInterface; |
35
|
|
|
use Symfony\Component\HttpFoundation\Request; |
36
|
|
|
use Symfony\Component\HttpFoundation\Response; |
37
|
|
|
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoder; |
38
|
|
|
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils; |
39
|
|
|
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface as Templating; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Class DefaultControllerTest |
43
|
|
|
*/ |
44
|
|
|
class UserControllerTest extends TestCase |
45
|
|
|
{ |
46
|
|
|
/** |
47
|
|
|
* @var UserController |
48
|
|
|
*/ |
49
|
|
|
private $controller; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var AuthenticationUtils|\PHPUnit_Framework_MockObject_MockObject |
53
|
|
|
*/ |
54
|
|
|
private $authenticationUtilsMock; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @var UserRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject |
58
|
|
|
*/ |
59
|
|
|
private $userRepositoryMock; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @var DocumentManager|\PHPUnit_Framework_MockObject_MockObject |
63
|
|
|
*/ |
64
|
|
|
private $documentManagerMock; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @var Templating|\PHPUnit_Framework_MockObject_MockObject |
68
|
|
|
*/ |
69
|
|
|
private $templatingMock; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @var Router|\PHPUnit_Framework_MockObject_MockObject |
73
|
|
|
*/ |
74
|
|
|
private $routerMock; |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @var FormFactoryInterface|\PHPUnit_Framework_MockObject_MockObject |
78
|
|
|
*/ |
79
|
|
|
private $formFactoryMock; |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @var UserPasswordEncoder |
83
|
|
|
*/ |
84
|
|
|
private $passwordEncoderMock; |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @return void |
88
|
|
|
*/ |
89
|
|
|
protected function setUp(): void |
90
|
|
|
{ |
91
|
|
|
parent::setUp(); |
92
|
|
|
|
93
|
|
|
$this->authenticationUtilsMock = $this->createMock(AuthenticationUtils::class); |
94
|
|
|
$this->userRepositoryMock = $this->createMock(UserRepositoryInterface::class); |
95
|
|
|
$this->documentManagerMock = $this->createMock(DocumentManager::class); |
96
|
|
|
$this->templatingMock = $this->createMock(Templating::class); |
97
|
|
|
$this->routerMock = $this->createMock(Router::class); |
98
|
|
|
$this->formFactoryMock = $this->createMock(FormFactoryInterface::class); |
99
|
|
|
$this->passwordEncoderMock = $this->createMock(UserPasswordEncoder::class); |
|
|
|
|
100
|
|
|
|
101
|
|
|
$this->controller = new UserController( |
102
|
|
|
$this->authenticationUtilsMock, |
103
|
|
|
$this->userRepositoryMock, |
104
|
|
|
$this->documentManagerMock, |
105
|
|
|
$this->templatingMock, |
106
|
|
|
$this->routerMock, |
107
|
|
|
$this->formFactoryMock, |
108
|
|
|
$this->passwordEncoderMock |
109
|
|
|
); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @return void |
114
|
|
|
*/ |
115
|
|
|
public function testLoginAction(): void |
116
|
|
|
{ |
117
|
|
|
$loginFormMock = $this->createMock(FormInterface::class); |
118
|
|
|
$this->formFactoryMock->method('create')->willReturn($loginFormMock); |
|
|
|
|
119
|
|
|
|
120
|
|
|
$responseMock = $this->createMock(Response::class); |
121
|
|
|
|
122
|
|
|
$this->templatingMock->expects(self::once()) |
123
|
|
|
->method('renderResponse') |
124
|
|
|
->willReturn($responseMock); |
125
|
|
|
|
126
|
|
|
$this->controller->loginAction(); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @return void |
131
|
|
|
* |
132
|
|
|
* @throws \Exception |
133
|
|
|
*/ |
134
|
|
|
public function testRegistrationActionNotSubmitted(): void |
135
|
|
|
{ |
136
|
|
|
$registrationFormMock = $this->createMock(FormInterface::class); |
137
|
|
|
$this->formFactoryMock->method('create')->willReturn($registrationFormMock); |
138
|
|
|
$registrationFormMock->method('isSubmitted')->willReturn(false); |
|
|
|
|
139
|
|
|
|
140
|
|
|
/** @var Request|\PHPUnit_Framework_MockObject_MockObject $request */ |
141
|
|
|
$request = $this->createMock(Request::class); |
142
|
|
|
|
143
|
|
|
$responseMock = $this->createMock(Response::class); |
144
|
|
|
|
145
|
|
|
$this->templatingMock->expects(self::once()) |
146
|
|
|
->method('renderResponse') |
147
|
|
|
->willReturn($responseMock); |
148
|
|
|
|
149
|
|
|
$this->documentManagerMock->expects(self::never())->method('persist'); |
150
|
|
|
$this->documentManagerMock->expects(self::never())->method('flush'); |
151
|
|
|
|
152
|
|
|
$this->controller->registrationAction($request); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @return void |
157
|
|
|
* |
158
|
|
|
* @throws \Exception |
159
|
|
|
*/ |
160
|
|
|
public function testRegistrationActionSubmittedAndValid(): void |
161
|
|
|
{ |
162
|
|
|
$registrationFormMock = $this->createMock(FormInterface::class); |
163
|
|
|
$this->formFactoryMock->method('create')->willReturn($registrationFormMock); |
164
|
|
|
$registrationFormMock->method('isSubmitted')->willReturn(true); |
165
|
|
|
$registrationFormMock->method('isValid')->willReturn(true); |
166
|
|
|
|
167
|
|
|
/** @var Request|\PHPUnit_Framework_MockObject_MockObject $request */ |
168
|
|
|
$request = $this->createMock(Request::class); |
169
|
|
|
|
170
|
|
|
$responseMock = $this->createMock(Response::class); |
|
|
|
|
171
|
|
|
|
172
|
|
|
$this->templatingMock->expects(self::never())->method('renderResponse'); |
173
|
|
|
|
174
|
|
|
$this->routerMock->method('generate')->willReturn('registration_success'); |
|
|
|
|
175
|
|
|
|
176
|
|
|
$this->documentManagerMock->expects(self::once())->method('persist'); |
177
|
|
|
$this->documentManagerMock->expects(self::once())->method('flush'); |
178
|
|
|
|
179
|
|
|
$this->controller->registrationAction($request); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* @return void |
184
|
|
|
*/ |
185
|
|
|
public function testRegistrationsuccessAction(): void |
186
|
|
|
{ |
187
|
|
|
$responseMock = $this->createMock(Response::class); |
188
|
|
|
|
189
|
|
|
$this->templatingMock->expects(self::once()) |
190
|
|
|
->method('renderResponse') |
191
|
|
|
->willReturn($responseMock); |
192
|
|
|
$this->controller->registrationsuccessAction(); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* @return void |
197
|
|
|
*/ |
198
|
|
|
public function testActivateemailActionUserNotFound(): void |
199
|
|
|
{ |
200
|
|
|
$hash = 'someHash'; |
201
|
|
|
$responseMock = $this->createMock(Response::class); |
|
|
|
|
202
|
|
|
|
203
|
|
|
$this->templatingMock->expects(self::never())->method('renderResponse'); |
204
|
|
|
|
205
|
|
|
$this->expectException(\InvalidArgumentException::class); |
206
|
|
|
$this->controller->activateemailAction($hash); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* @return void |
211
|
|
|
*/ |
212
|
|
|
public function testActivateemailActionUserFound(): void |
213
|
|
|
{ |
214
|
|
|
$hash = 'someHash'; |
215
|
|
|
$responseMock = $this->createMock(Response::class); |
216
|
|
|
|
217
|
|
|
$this->templatingMock->expects(self::once()) |
218
|
|
|
->method('renderResponse') |
219
|
|
|
->willReturn($responseMock); |
220
|
|
|
|
221
|
|
|
$userMock = $this->createMock(User::class); |
222
|
|
|
|
223
|
|
|
$this->userRepositoryMock->method('findOneBy') |
|
|
|
|
224
|
|
|
->willReturn($userMock); |
225
|
|
|
|
226
|
|
|
$this->controller->activateemailAction($hash); |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* @return void |
231
|
|
|
*/ |
232
|
|
|
public function testLoginCheckAction(): void |
233
|
|
|
{ |
234
|
|
|
self::assertNull($this->controller->loginCheckAction()); |
|
|
|
|
235
|
|
|
} |
236
|
|
|
} |
237
|
|
|
|
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..