|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Storgman - Student Organizations Management |
|
5
|
|
|
* Copyright (C) 2014-2016, Dejan Angelov <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* This file is part of Storgman. |
|
8
|
|
|
* |
|
9
|
|
|
* Storgman is free software: you can redistribute it and/or modify |
|
10
|
|
|
* it under the terms of the GNU General Public License as published by |
|
11
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
|
12
|
|
|
* (at your option) any later version. |
|
13
|
|
|
* |
|
14
|
|
|
* Storgman is distributed in the hope that it will be useful, |
|
15
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
16
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
17
|
|
|
* GNU General Public License for more details. |
|
18
|
|
|
* |
|
19
|
|
|
* You should have received a copy of the GNU General Public License |
|
20
|
|
|
* along with Storgman. If not, see <http://www.gnu.org/licenses/>. |
|
21
|
|
|
* |
|
22
|
|
|
* @package Storgman |
|
23
|
|
|
* @copyright Copyright (C) 2014-2016, Dejan Angelov <[email protected]> |
|
24
|
|
|
* @license https://github.com/angelov/storgman/blob/master/LICENSE |
|
25
|
|
|
* @author Dejan Angelov <[email protected]> |
|
26
|
|
|
*/ |
|
27
|
|
|
|
|
28
|
|
|
// Note: Mess inside. |
|
29
|
|
|
|
|
30
|
|
|
namespace Angelov\Storgman\Members\FeatureContexts; |
|
31
|
|
|
|
|
32
|
|
|
use Angelov\Storgman\Core\FeatureContexts\BaseContext; |
|
33
|
|
|
use Angelov\Storgman\Members\Member; |
|
34
|
|
|
use Behat\Behat\Tester\Exception\PendingException; |
|
35
|
|
|
use Behat\Gherkin\Node\TableNode; |
|
36
|
|
|
|
|
37
|
|
|
class MembersManagementContext extends BaseContext |
|
38
|
|
|
{ |
|
39
|
|
|
/** @var $members Member[] */ |
|
40
|
|
|
private $members; |
|
41
|
|
|
|
|
42
|
|
|
private $credentials; |
|
43
|
|
|
|
|
44
|
|
|
/** @var $authenticatedMember Member */ |
|
45
|
|
|
private $authenticatedMember; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @Given /^there are the following members:$/ |
|
49
|
|
|
*/ |
|
50
|
|
|
public function thereAreTheFollowingMembers(TableNode $members) |
|
51
|
|
|
{ |
|
52
|
|
|
$this->clearMembers(); |
|
53
|
|
|
foreach ($members as $current) { |
|
54
|
|
|
$this->storeMember($current); |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
private function storeMember($current) |
|
59
|
|
|
{ |
|
60
|
|
|
$repository = $this->getMembersRepository(); |
|
61
|
|
|
$hasher = $this->getPasswordHasher(); |
|
62
|
|
|
|
|
63
|
|
|
$member = new Member(); |
|
64
|
|
|
$member->setFirstName($current['first_name']); |
|
65
|
|
|
$member->setLastName($current['last_name']); |
|
66
|
|
|
$member->setEmail($current['email']); |
|
67
|
|
|
$member->setPassword($hasher->make($current['password'])); |
|
68
|
|
|
|
|
69
|
|
|
if (isset($current['faculty'])) { |
|
70
|
|
|
$member->setFaculty($current['faculty']); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
if (isset($current['field'])) { |
|
74
|
|
|
$member->setFieldOfStudy($current['field']); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
$member->setApproved($current['approved'] === 'yes'); |
|
78
|
|
|
$member->setBoardMember($current['board'] === 'yes'); |
|
79
|
|
|
|
|
80
|
|
|
$repository->store($member); |
|
81
|
|
|
|
|
82
|
|
|
$this->members[] = $member; |
|
83
|
|
|
$this->keepMemberCredentials($member, $current); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
private function keepMemberCredentials(Member $member, $data) |
|
87
|
|
|
{ |
|
88
|
|
|
$this->credentials[$member->getId()] = [ |
|
89
|
|
|
'email' => $data['email'], |
|
90
|
|
|
'password' => $data['password'] |
|
91
|
|
|
]; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
private function getLoginPath() |
|
95
|
|
|
{ |
|
96
|
|
|
return $this->getUrlGenerator()->route('auth'); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @Given /^I am on the login page$/ |
|
101
|
|
|
* @When /^I go to the login page$/ |
|
102
|
|
|
*/ |
|
103
|
|
|
public function iAmOnTheLoginPage() |
|
104
|
|
|
{ |
|
105
|
|
|
$this->visitPath($this->getLoginPath()); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* @Then /^I should be on the login page$/ |
|
110
|
|
|
*/ |
|
111
|
|
|
public function iShouldBeOnTheLoginPage() |
|
112
|
|
|
{ |
|
113
|
|
|
$loginPath = $this->getLoginPath(); |
|
114
|
|
|
$this->assertSession()->addressEquals($loginPath); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* @Given /^I am not logged in$/ |
|
119
|
|
|
*/ |
|
120
|
|
|
public function iAmNotLoggedIn() |
|
121
|
|
|
{ |
|
122
|
|
|
$this->visitPath($this->getLogoutPath()); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
private function getLogoutPath() |
|
126
|
|
|
{ |
|
127
|
|
|
return $this->getUrlGenerator()->route('logout'); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* @Given I am logged in as (a )board member |
|
132
|
|
|
*/ |
|
133
|
|
|
public function iAmLoggedInAsABoardMember() |
|
134
|
|
|
{ |
|
135
|
|
|
$member = $this->findBoardMember(); |
|
136
|
|
|
|
|
137
|
|
|
$this->authenticate($member); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
private function authenticate(Member $member) |
|
141
|
|
|
{ |
|
142
|
|
|
$credentials = $this->getMemberCredentials($member); |
|
143
|
|
|
|
|
144
|
|
|
$loginPath = $this->getLoginPath(); |
|
145
|
|
|
$this->visitPath($loginPath); |
|
146
|
|
|
|
|
147
|
|
|
$page = $this->getPage(); |
|
148
|
|
|
|
|
149
|
|
|
$page->fillField('Email address', $credentials['email']); |
|
150
|
|
|
$page->fillField('Password', $credentials['password']); |
|
151
|
|
|
$page->pressButton('Sign in'); |
|
152
|
|
|
|
|
153
|
|
|
|
|
154
|
|
|
$this->authenticatedMember = $member; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* @todo Create separate in-memory members repository |
|
159
|
|
|
*/ |
|
160
|
|
View Code Duplication |
private function findMemberByFullName($fullName) |
|
|
|
|
|
|
161
|
|
|
{ |
|
162
|
|
|
$count = count($this->members); |
|
163
|
|
|
for ($i = 0; $i < $count; $i++) { |
|
164
|
|
|
$member = $this->members[$i]; |
|
165
|
|
|
|
|
166
|
|
|
if ($member->getFullName() === $fullName) { |
|
167
|
|
|
return $member; |
|
168
|
|
|
} |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
throw new \Exception(printf( |
|
172
|
|
|
"There is no member with the given full name [%s].", |
|
173
|
|
|
$fullName |
|
174
|
|
|
)); |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
View Code Duplication |
private function findBoardMember() |
|
|
|
|
|
|
178
|
|
|
{ |
|
179
|
|
|
$count = count($this->members); |
|
180
|
|
|
for ($i = 0; $i < $count; $i++) { |
|
181
|
|
|
$member = $this->members[$i]; |
|
182
|
|
|
|
|
183
|
|
|
if ($member->isBoardMember()) { |
|
184
|
|
|
return $member; |
|
185
|
|
|
} |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
throw new \Exception("There are no board members."); |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
private function getMemberCredentials(Member $member) |
|
192
|
|
|
{ |
|
193
|
|
|
$id = $member->getId(); |
|
194
|
|
|
return $this->credentials[$id]; |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
/** |
|
198
|
|
|
* @When /^I login as "([^"]*)"$/ |
|
199
|
|
|
* @Given /^I am logged in as "([^"]*)"$/ |
|
200
|
|
|
*/ |
|
201
|
|
|
public function iLoginAs($fullName) |
|
202
|
|
|
{ |
|
203
|
|
|
$member = $this->findMemberByFullName($fullName); |
|
204
|
|
|
$this->authenticate($member); |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
/** |
|
208
|
|
|
* @Then /^I should be on my profile page$/ |
|
209
|
|
|
*/ |
|
210
|
|
|
public function iShouldBeOnMyProfilePage() |
|
211
|
|
|
{ |
|
212
|
|
|
if (!$this->authenticatedMember) { |
|
213
|
|
|
throw new \Exception("You must login first."); |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
$id = $this->authenticatedMember->getId(); |
|
217
|
|
|
$this->assertSession()->addressEquals(route('members.show', $id)); |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
/** |
|
221
|
|
|
* @Then /^I should be on the registration page$/ |
|
222
|
|
|
*/ |
|
223
|
|
|
public function iShouldBeOnTheRegistrationPage() |
|
224
|
|
|
{ |
|
225
|
|
|
$this->assertSession()->addressEquals(route('members.register')); |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
/** |
|
229
|
|
|
* @Given /^I am on the registration page$/ |
|
230
|
|
|
* @When /^I go to the registration page$/ |
|
231
|
|
|
*/ |
|
232
|
|
|
public function iAmOnTheRegistrationPage() |
|
233
|
|
|
{ |
|
234
|
|
|
$this->visitPath(route('members.register')); |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
/** |
|
238
|
|
|
* @Given /^I am on the members page$/ |
|
239
|
|
|
*/ |
|
240
|
|
|
public function iAmOnTheMembersPage() |
|
241
|
|
|
{ |
|
242
|
|
|
$this->visitPath(route("members.index")); |
|
243
|
|
|
} |
|
244
|
|
|
|
|
245
|
|
|
/** |
|
246
|
|
|
* @Then /^I should be on the members page$/ |
|
247
|
|
|
*/ |
|
248
|
|
|
public function iShouldBeOnTheMembersPage() |
|
249
|
|
|
{ |
|
250
|
|
|
$this->assertSession()->addressEquals(route('members.index')); |
|
251
|
|
|
} |
|
252
|
|
|
|
|
253
|
|
|
private function clearMembers() |
|
254
|
|
|
{ |
|
255
|
|
|
if (!count($this->members)) { |
|
256
|
|
|
return; |
|
257
|
|
|
} |
|
258
|
|
|
|
|
259
|
|
|
foreach ($this->members as $member) { |
|
260
|
|
|
$this->getMembersRepository()->destroy($member->getId()); |
|
261
|
|
|
} |
|
262
|
|
|
} |
|
263
|
|
|
} |
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.