Completed
Push — master ( 3c7667...799620 )
by Kamil
94:52 queued 57:43
created

Behat/Context/Ui/Shop/RegistrationContext.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
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 Sylius\Behat\Context\Ui\Shop;
15
16
use Behat\Behat\Context\Context;
17
use Sylius\Behat\NotificationType;
18
use Sylius\Behat\Page\Shop\Account\DashboardPageInterface;
19
use Sylius\Behat\Page\Shop\Account\LoginPageInterface;
20
use Sylius\Behat\Page\Shop\Account\ProfileUpdatePageInterface;
21
use Sylius\Behat\Page\Shop\Account\RegisterPageInterface;
22
use Sylius\Behat\Page\Shop\Account\VerificationPageInterface;
23
use Sylius\Behat\Page\Shop\HomePageInterface;
24
use Sylius\Behat\Service\NotificationCheckerInterface;
25
use Sylius\Behat\Service\SharedStorageInterface;
26
use Sylius\Component\Core\Model\CustomerInterface;
27
use Sylius\Component\Core\Model\ShopUserInterface;
28
use Webmozart\Assert\Assert;
29
30
class RegistrationContext implements Context
31
{
32
    /**
33
     * @var SharedStorageInterface
34
     */
35
    private $sharedStorage;
36
37
    /**
38
     * @var DashboardPageInterface
39
     */
40
    private $dashboardPage;
41
42
    /**
43
     * @var HomePageInterface
44
     */
45
    private $homePage;
46
47
    /**
48
     * @var LoginPageInterface
49
     */
50
    private $loginPage;
51
52
    /**
53
     * @var RegisterPageInterface
54
     */
55
    private $registerPage;
56
57
    /**
58
     * @var VerificationPageInterface
59
     */
60
    private $verificationPage;
61
62
    /**
63
     * @var ProfileUpdatePageInterface
64
     */
65
    private $profileUpdatePage;
66
67
    /**
68
     * @var NotificationCheckerInterface
69
     */
70
    private $notificationChecker;
71
72
    /**
73
     * @param SharedStorageInterface $sharedStorage
74
     * @param DashboardPageInterface $dashboardPage
75
     * @param HomePageInterface $homePage
76
     * @param LoginPageInterface $loginPage
77
     * @param RegisterPageInterface $registerPage
78
     * @param VerificationPageInterface $verificationPage
79
     * @param ProfileUpdatePageInterface $profileUpdatePage
80
     * @param NotificationCheckerInterface $notificationChecker
81
     */
82
    public function __construct(
83
        SharedStorageInterface $sharedStorage,
84
        DashboardPageInterface $dashboardPage,
85
        HomePageInterface $homePage,
86
        LoginPageInterface $loginPage,
87
        RegisterPageInterface $registerPage,
88
        VerificationPageInterface $verificationPage,
89
        ProfileUpdatePageInterface $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($firstName = null)
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($lastName = null)
124
    {
125
        $this->registerPage->specifyLastName($lastName);
126
    }
127
128
    /**
129
     * @When I specify the email as :email
130
     * @When I do not specify the email
131
     */
132
    public function iSpecifyTheEmail($email = null)
133
    {
134
        $this->registerPage->specifyEmail($email);
135
    }
136
137
    /**
138
     * @When I specify the password as :password
139
     * @When I do not specify the password
140
     */
141
    public function iSpecifyThePasswordAs($password = null)
142
    {
143
        $this->registerPage->specifyPassword($password);
144
        $this->sharedStorage->set('password', $password);
145
    }
146
147
    /**
148
     * @When /^I confirm (this password)$/
149
     */
150
    public function iConfirmThisPassword($password)
151
    {
152
        $this->registerPage->verifyPassword($password);
153
    }
154
155
    /**
156
     * @Given I do not confirm the password
157
     */
158
    public function iDoNotConfirmPassword()
159
    {
160
        $this->registerPage->verifyPassword(null);
161
    }
162
163
    /**
164
     * @When I specify the phone number as :phoneNumber
165
     */
166
    public function iSpecifyThePhoneNumberAs($phoneNumber)
167
    {
168
        $this->registerPage->specifyPhoneNumber($phoneNumber);
169
    }
170
171
    /**
172
     * @When I register this account
173
     * @When I try to register this account
174
     */
175
    public function iRegisterThisAccount()
176
    {
177
        $this->registerPage->register();
178
    }
179
180
    /**
181
     * @Then my email should be :email
182
     * @Then my email should still be :email
183
     */
184
    public function myEmailShouldBe($email)
185
    {
186
        $this->dashboardPage->open();
187
188
        Assert::true($this->dashboardPage->hasCustomerEmail($email));
189
    }
190
191
    /**
192
     * @Then /^I should be notified that the ([^"]+) is required$/
193
     */
194
    public function iShouldBeNotifiedThatElementIsRequired($element)
195
    {
196
        $this->assertFieldValidationMessage($element, sprintf('Please enter your %s.', $element));
197
    }
198
199
    /**
200
     * @Then I should be notified that the email is already used
201
     */
202
    public function iShouldBeNotifiedThatTheEmailIsAlreadyUsed()
203
    {
204
        $this->assertFieldValidationMessage('email', 'This email is already used.');
205
    }
206
207
    /**
208
     * @Then I should be notified that the password do not match
209
     */
210
    public function iShouldBeNotifiedThatThePasswordDoNotMatch()
211
    {
212
        $this->assertFieldValidationMessage('password', 'The entered passwords don\'t match');
213
    }
214
215
    /**
216
     * @Then I should be notified that new account has been successfully created
217
     * @Then I should be notified that my account has been created and the verification email has been sent
218
     */
219
    public function iShouldBeNotifiedThatNewAccountHasBeenSuccessfullyCreated()
220
    {
221
        $this->notificationChecker->checkNotification(
222
            'Thank you for registering, check your email to verify your account.',
223
            NotificationType::success()
224
        );
225
    }
226
227
    /**
228
     * @Then I should be logged in
229
     */
230
    public function iShouldBeLoggedIn()
231
    {
232
        Assert::true($this->homePage->hasLogoutButton());
233
    }
234
235
    /**
236
     * @Then I should not be logged in
237
     */
238
    public function iShouldNotBeLoggedIn()
239
    {
240
        Assert::false($this->homePage->hasLogoutButton());
241
    }
242
243
    /**
244
     * @Then I should be able to log in as :email with :password password
245
     */
246
    public function iShouldBeAbleToLogInAsWithPassword($email, $password)
247
    {
248
        $this->iLogInAsWithPassword($email, $password);
249
        $this->iShouldBeLoggedIn();
250
    }
251
252
    /**
253
     * @Then I should not be able to log in as :email with :password password
254
     */
255
    public function iShouldNotBeAbleToLogInAsWithPassword($email, $password)
256
    {
257
        $this->iLogInAsWithPassword($email, $password);
258
259
        Assert::true($this->loginPage->hasValidationErrorWith('Error Account is disabled.'));
260
    }
261
262
    /**
263
     * @When I log in as :email with :password password
264
     */
265
    public function iLogInAsWithPassword($email, $password)
266
    {
267
        $this->loginPage->open();
268
        $this->loginPage->specifyUsername($email);
269
        $this->loginPage->specifyPassword($password);
270
        $this->loginPage->logIn();
271
    }
272
273
    /**
274
     * @When I register with email :email and password :password
275
     */
276
    public function iRegisterWithEmailAndPassword($email, $password)
277
    {
278
        $this->registerPage->open();
279
        $this->registerPage->specifyEmail($email);
280
        $this->registerPage->specifyPassword($password);
281
        $this->registerPage->verifyPassword($password);
282
        $this->registerPage->specifyFirstName('Carrot');
283
        $this->registerPage->specifyLastName('Ironfoundersson');
284
        $this->registerPage->register();
285
    }
286
287
    /**
288
     * @Then /^my account should be verified$/
289
     */
290
    public function myAccountShouldBeVerified()
291
    {
292
        Assert::true($this->dashboardPage->isVerified());
293
    }
294
295
    /**
296
     * @When /^(I) try to verify my account using the link from this email$/
297
     */
298
    public function iUseItToVerify(ShopUserInterface $user)
299
    {
300
        $this->verificationPage->verifyAccount($user->getEmailVerificationToken());
301
    }
302
303
    /**
304
     * @When I verify my account using link sent to :customer
305
     */
306
    public function iVerifyMyAccount(CustomerInterface $customer)
307
    {
308
        $user = $customer->getUser();
309
        Assert::notNull($user, 'No account for given customer');
310
311
        $this->iUseItToVerify($user);
0 ignored issues
show
$user is of type object<Sylius\Component\...del\UserInterface>|null, but the function expects a object<Sylius\Component\...odel\ShopUserInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

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