Completed
Pull Request — master (#178)
by Loïc
03:00
created

RegistrationContext::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.6333
c 0
b 0
f 0
cc 1
nc 1
nop 8

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
/*
4
 * This file is part of AppName.
5
 *
6
 * (c) Monofony
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace App\Behat\Context\Ui\Frontend;
15
16
use App\Entity\CustomerInterface;
17
use Behat\Behat\Context\Context;
18
use App\Behat\NotificationType;
19
use App\Behat\Page\Frontend\Account\DashboardPage;
20
use App\Behat\Page\Frontend\Account\LoginPage;
21
use App\Behat\Page\Frontend\Account\ProfileUpdatePage;
22
use App\Behat\Page\Frontend\Account\RegisterPage;
23
use App\Behat\Page\Frontend\Account\VerificationPage;
24
use App\Behat\Page\Frontend\HomePage;
25
use App\Behat\Service\NotificationCheckerInterface;
26
use App\Behat\Service\SharedStorageInterface;
27
use Sylius\Component\User\Model\UserInterface;
28
use Webmozart\Assert\Assert;
29
30
class RegistrationContext implements Context
31
{
32
    /**
33
     * @var SharedStorageInterface
34
     */
35
    private $sharedStorage;
36
37
    /**
38
     * @var DashboardPage
39
     */
40
    private $dashboardPage;
41
42
    /**
43
     * @var HomePage
44
     */
45
    private $homePage;
46
47
    /**
48
     * @var LoginPage
49
     */
50
    private $loginPage;
51
52
    /**
53
     * @var RegisterPage
54
     */
55
    private $registerPage;
56
57
    /**
58
     * @var VerificationPage
59
     */
60
    private $verificationPage;
61
62
    /**
63
     * @var ProfileUpdatePage
64
     */
65
    private $profileUpdatePage;
66
67
    /**
68
     * @var NotificationCheckerInterface
69
     */
70
    private $notificationChecker;
71
72
    /**
73
     * @param SharedStorageInterface       $sharedStorage
74
     * @param DashboardPage                $dashboardPage
75
     * @param HomePage                     $homePage
76
     * @param LoginPage                    $loginPage
77
     * @param RegisterPage                 $registerPage
78
     * @param VerificationPage             $verificationPage
79
     * @param ProfileUpdatePage            $profileUpdatePage
80
     * @param NotificationCheckerInterface $notificationChecker
81
     */
82
    public function __construct(
83
        SharedStorageInterface $sharedStorage,
84
        DashboardPage $dashboardPage,
85
        HomePage $homePage,
86
        LoginPage $loginPage,
87
        RegisterPage $registerPage,
88
        VerificationPage $verificationPage,
89
        ProfileUpdatePage $profileUpdatePage,
90
        NotificationCheckerInterface $notificationChecker
91
    ) {
92
        $this->sharedStorage = $sharedStorage;
93
        $this->dashboardPage = $dashboardPage;
94
        $this->homePage = $homePage;
95
        $this->loginPage = $loginPage;
96
        $this->registerPage = $registerPage;
97
        $this->verificationPage = $verificationPage;
98
        $this->profileUpdatePage = $profileUpdatePage;
99
        $this->notificationChecker = $notificationChecker;
100
    }
101
102
    /**
103
     * @When /^I want to(?:| again) register a new account$/
104
     */
105
    public function iWantToRegisterANewAccount()
106
    {
107
        $this->registerPage->open();
108
    }
109
110
    /**
111
     * @When I specify the first name as :firstName
112
     * @When I do not specify the first name
113
     */
114
    public function iSpecifyTheFirstName(?string $firstName = null): void
115
    {
116
        $this->registerPage->specifyFirstName($firstName);
117
    }
118
119
    /**
120
     * @When I specify the last name as :lastName
121
     * @When I do not specify the last name
122
     */
123
    public function iSpecifyTheLastName(?string $lastName = null): void
124
    {
125
        $this->registerPage->specifyLastName($lastName);
126
    }
127
128
129
    /**
130
     * @When I specify the email as :email
131
     * @When I do not specify the email
132
     */
133
    public function iSpecifyTheEmail($email = null)
134
    {
135
        $this->registerPage->specifyEmail($email);
136
    }
137
138
    /**
139
     * @When I specify the password as :password
140
     * @When I do not specify the password
141
     */
142
    public function iSpecifyThePasswordAs($password = null)
143
    {
144
        $this->registerPage->specifyPassword($password);
145
        $this->sharedStorage->set('password', $password);
146
    }
147
148
    /**
149
     * @When /^I confirm (this password)$/
150
     */
151
    public function iConfirmThisPassword($password)
152
    {
153
        $this->registerPage->verifyPassword($password);
154
    }
155
156
    /**
157
     * @Given I do not confirm the password
158
     */
159
    public function iDoNotConfirmPassword()
160
    {
161
        $this->registerPage->verifyPassword(null);
162
    }
163
164
    /**
165
     * @When I register this account
166
     * @When I try to register this account
167
     */
168
    public function iRegisterThisAccount()
169
    {
170
        $this->registerPage->register();
171
    }
172
173
    /**
174
     * @Then my email should be :email
175
     * @Then my email should still be :email
176
     */
177
    public function myEmailShouldBe($email)
178
    {
179
        $this->dashboardPage->open();
180
181
        Assert::true($this->dashboardPage->hasCustomerEmail($email));
182
    }
183
184
    /**
185
     * @Then /^I should be notified that the ([^"]+) is required$/
186
     */
187
    public function iShouldBeNotifiedThatElementIsRequired($element)
188
    {
189
        $this->assertFieldValidationMessage($element, sprintf('Please enter your %s.', $element));
190
    }
191
192
    /**
193
     * @Then I should be notified that the email is already used
194
     */
195
    public function iShouldBeNotifiedThatTheEmailIsAlreadyUsed()
196
    {
197
        $this->assertFieldValidationMessage('email', 'This email is already used.');
198
    }
199
200
    /**
201
     * @Then I should be notified that the password do not match
202
     */
203
    public function iShouldBeNotifiedThatThePasswordDoNotMatch()
204
    {
205
        $this->assertFieldValidationMessage('password', 'The entered passwords don\'t match');
206
    }
207
208
    /**
209
     * @Then I should be notified that new account has been successfully created
210
     * @Then I should be notified that my account has been created and the verification email has been sent
211
     */
212
    public function iShouldBeNotifiedThatNewAccountHasBeenSuccessfullyCreated()
213
    {
214
        $this->notificationChecker->checkNotification(
215
            'Thank you for registering, check your email to verify your account.',
216
            NotificationType::success()
217
        );
218
    }
219
220
    /**
221
     * @Then I should be logged in
222
     */
223
    public function iShouldBeLoggedIn()
224
    {
225
        Assert::true($this->homePage->hasLogoutButton());
226
    }
227
228
    /**
229
     * @Then I should not be logged in
230
     */
231
    public function iShouldNotBeLoggedIn()
232
    {
233
        Assert::false($this->homePage->hasLogoutButton());
234
    }
235
236
    /**
237
     * @Then I should be able to log in as :email with :password password
238
     */
239
    public function iShouldBeAbleToLogInAsWithPassword($email, $password)
240
    {
241
        $this->iLogInAsWithPassword($email, $password);
242
        $this->iShouldBeLoggedIn();
243
    }
244
245
    /**
246
     * @Then I should not be able to log in as :email with :password password
247
     */
248
    public function iShouldNotBeAbleToLogInAsWithPassword($email, $password)
249
    {
250
        $this->iLogInAsWithPassword($email, $password);
251
252
        Assert::true($this->loginPage->hasValidationErrorWith('Error Account is disabled.'));
253
    }
254
255
    /**
256
     * @When I log in as :email with :password password
257
     */
258
    public function iLogInAsWithPassword($email, $password)
259
    {
260
        $this->loginPage->open();
261
        $this->loginPage->specifyUsername($email);
262
        $this->loginPage->specifyPassword($password);
263
        $this->loginPage->logIn();
264
    }
265
266
    /**
267
     * @When I register with email :email and password :password
268
     */
269
    public function iRegisterWithEmailAndPassword($email, $password)
270
    {
271
        $this->registerPage->open();
272
        $this->registerPage->specifyEmail($email);
273
        $this->registerPage->specifyPassword($password);
274
        $this->registerPage->verifyPassword($password);
275
        $this->registerPage->specifyFirstName('Carrot');
276
        $this->registerPage->specifyLastName('Ironfoundersson');
277
        $this->registerPage->register();
278
    }
279
280
    /**
281
     * @Then /^my account should be verified$/
282
     */
283
    public function myAccountShouldBeVerified()
284
    {
285
        Assert::true($this->dashboardPage->isVerified());
286
    }
287
288
    /**
289
     * @When /^(I) try to verify my account using the link from this email$/
290
     */
291
    public function iUseItToVerify(UserInterface $user)
292
    {
293
        $this->verificationPage->verifyAccount($user->getEmailVerificationToken());
294
    }
295
296
    /**
297
     * @When I verify my account using link sent to :customer
298
     */
299
    public function iVerifyMyAccount(CustomerInterface $customer)
300
    {
301
        $user = $customer->getUser();
302
        Assert::notNull($user, 'No account for given customer');
303
304
        $this->iUseItToVerify($user);
0 ignored issues
show
Bug introduced by
It seems like $user defined by $customer->getUser() on line 301 can also be of type null; however, App\Behat\Context\Ui\Fro...ntext::iUseItToVerify() does only seem to accept object<Sylius\Component\User\Model\UserInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
305
    }
306
307
    /**
308
     * @When I resend the verification email
309
     */
310
    public function iResendVerificationEmail()
311
    {
312
        $this->dashboardPage->open();
313
        $this->dashboardPage->pressResendVerificationEmail();
314
    }
315
316
    /**
317
     * @When I use the verification link from the first email to verify
318
     */
319
    public function iUseVerificationLinkFromFirstEmailToVerify()
320
    {
321
        $token = $this->sharedStorage->get('verification_token');
322
323
        $this->verificationPage->verifyAccount($token);
324
    }
325
326
    /**
327
     * @When I (try to )verify using :token token
328
     */
329
    public function iTryToVerifyUsing($token)
330
    {
331
        $this->verificationPage->verifyAccount($token);
332
    }
333
334
    /**
335
     * @Then /^(?:my|his|her) account should not be verified$/
336
     */
337
    public function myAccountShouldNotBeVerified()
338
    {
339
        $this->dashboardPage->open();
340
341
        Assert::false($this->dashboardPage->isVerified());
342
    }
343
344
    /**
345
     * @Then I should not be able to resend the verification email
346
     */
347
    public function iShouldBeUnableToResendVerificationEmail()
348
    {
349
        $this->dashboardPage->open();
350
351
        Assert::false($this->dashboardPage->hasResendVerificationEmailButton());
352
    }
353
354
    /**
355
     * @Then I should be notified that the verification was successful
356
     */
357
    public function iShouldBeNotifiedThatTheVerificationWasSuccessful()
358
    {
359
        $this->notificationChecker->checkNotification('has been successfully verified.', NotificationType::success());
360
    }
361
362
    /**
363
     * @Then I should be notified that the verification token is invalid
364
     */
365
    public function iShouldBeNotifiedThatTheVerificationTokenIsInvalid()
366
    {
367
        $this->notificationChecker->checkNotification('The verification token is invalid.', NotificationType::failure());
368
    }
369
370
    /**
371
     * @Then I should be notified that the verification email has been sent
372
     */
373
    public function iShouldBeNotifiedThatTheVerificationEmailHasBeenSent()
374
    {
375
        $this->notificationChecker->checkNotification(
376
            'An email with the verification link has been sent to your email address.',
377
            NotificationType::success()
378
        );
379
    }
380
381
    /**
382
     * @When I subscribe to the newsletter
383
     */
384
    public function iSubscribeToTheNewsletter()
385
    {
386
        $this->registerPage->subscribeToTheNewsletter();
387
    }
388
389
    /**
390
     * @Then I should be subscribed to the newsletter
391
     */
392
    public function iShouldBeSubscribedToTheNewsletter()
393
    {
394
        $this->profileUpdatePage->open();
395
396
        Assert::true($this->profileUpdatePage->isSubscribedToTheNewsletter());
397
    }
398
399
    /**
400
     * @param string $element
401
     * @param string $expectedMessage
402
     */
403
    private function assertFieldValidationMessage($element, $expectedMessage)
404
    {
405
        Assert::true($this->registerPage->checkValidationMessageFor($element, $expectedMessage));
406
    }
407
}
408