Completed
Push — master ( 5a50ca...7c7881 )
by Michał
13:05
created

RegistrationContext::assertEmailContainsMessage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
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
namespace Sylius\Behat\Context\Ui\Shop;
13
14
use Behat\Behat\Context\Context;
15
use Sylius\Behat\NotificationType;
16
use Sylius\Behat\Page\Shop\Account\DashboardPageInterface;
17
use Sylius\Behat\Page\Shop\Account\VerificationPageInterface;
18
use Sylius\Behat\Page\Shop\Account\RegisterPageInterface;
19
use Sylius\Behat\Page\Shop\HomePageInterface;
20
use Sylius\Behat\Service\NotificationCheckerInterface;
21
use Sylius\Behat\Service\Resolver\CurrentPageResolverInterface;
22
use Sylius\Behat\Service\SharedStorageInterface;
23
use Sylius\Behat\Service\SecurityServiceInterface;
24
use Sylius\Component\Core\Model\ShopUserInterface;
25
use Sylius\Component\Core\Test\Services\EmailCheckerInterface;
26
use Webmozart\Assert\Assert;
27
28
/**
29
 * @author Arkadiusz Krakowiak <[email protected]>
30
 */
31
class RegistrationContext implements Context
32
{
33
    /**
34
     * @var CurrentPageResolverInterface
35
     */
36
    private $currentPageResolver;
37
38
    /**
39
     * @var DashboardPageInterface
40
     */
41
    private $dashboardPage;
42
43
    /**
44
     * @var HomePageInterface
45
     */
46
    private $homePage;
47
48
    /**
49
     * @var NotificationCheckerInterface
50
     */
51
    private $notificationChecker;
52
53
    /**
54
     * @var RegisterPageInterface
55
     */
56
    private $registerPage;
57
58
    /**
59
     * @var VerificationPageInterface
60
     */
61
    private $verificationPage;
62
63
    /**
64
     * @var SecurityServiceInterface
65
     */
66
    private $securityService;
67
68
    /**
69
     * @var EmailCheckerInterface
70
     */
71
    private $emailChecker;
72
73
    /**
74
     * @var SharedStorageInterface
75
     */
76
    private $sharedStorage;
77
78
    /**
79
     * @param CurrentPageResolverInterface $currentPageResolver
80
     * @param DashboardPageInterface $dashboardPage
81
     * @param HomePageInterface $homePage
82
     * @param NotificationCheckerInterface $notificationChecker
83
     * @param RegisterPageInterface $registerPage
84
     * @param VerificationPageInterface $verificationPage
85
     * @param SecurityServiceInterface $securityService
86
     * @param EmailCheckerInterface $emailChecker
87
     * @param SharedStorageInterface $sharedStorage
88
     */
89
    public function __construct(
90
        CurrentPageResolverInterface $currentPageResolver,
91
        DashboardPageInterface $dashboardPage,
92
        HomePageInterface $homePage,
93
        NotificationCheckerInterface $notificationChecker,
94
        RegisterPageInterface $registerPage,
95
        VerificationPageInterface $verificationPage,
96
        SecurityServiceInterface $securityService,
97
        EmailCheckerInterface $emailChecker,
98
        SharedStorageInterface $sharedStorage
99
    ) {
100
        $this->currentPageResolver = $currentPageResolver;
101
        $this->dashboardPage = $dashboardPage;
102
        $this->homePage = $homePage;
103
        $this->notificationChecker = $notificationChecker;
104
        $this->registerPage = $registerPage;
105
        $this->verificationPage = $verificationPage;
106
        $this->securityService = $securityService;
107
        $this->emailChecker = $emailChecker;
108
        $this->sharedStorage = $sharedStorage;
109
    }
110
111
    /**
112
     * @Given /^I want to(?:| again) register a new account$/
113
     */
114
    public function iWantToRegisterANewAccount()
115
    {
116
        $this->registerPage->open();
117
    }
118
119
    /**
120
     * @When I specify the first name as :firstName
121
     * @When I do not specify the first name
122
     */
123
    public function iSpecifyTheFirstName($firstName = null)
124
    {
125
        $this->registerPage->specifyFirstName($firstName);
126
    }
127
128
    /**
129
     * @When I specify the last name as :lastName
130
     * @When I do not specify the last name
131
     */
132
    public function iSpecifyTheLastName($lastName = null)
133
    {
134
        $this->registerPage->specifyLastName($lastName);
135
    }
136
137
    /**
138
     * @When I specify the email as :email
139
     * @When I do not specify the email
140
     */
141
    public function iSpecifyTheEmail($email = null)
142
    {
143
        $this->registerPage->specifyEmail($email);
144
    }
145
146
    /**
147
     * @When I specify the password as :password
148
     * @When I do not specify the password
149
     */
150
    public function iSpecifyThePasswordAs($password = null)
151
    {
152
        $this->registerPage->specifyPassword($password);
153
        $this->sharedStorage->set('password', $password);
154
    }
155
156
    /**
157
     * @When /^I confirm (this password)$/
158
     */
159
    public function iConfirmThisPassword($password)
160
    {
161
        $this->registerPage->verifyPassword($password);
162
    }
163
164
    /**
165
     * @Given I do not confirm the password
166
     */
167
    public function iDoNotConfirmPassword()
168
    {
169
        $this->registerPage->verifyPassword(null);
170
    }
171
172
    /**
173
     * @When I specify the phone number as :phoneNumber
174
     */
175
    public function iSpecifyThePhoneNumberAs($phoneNumber)
176
    {
177
        $this->registerPage->specifyPhoneNumber($phoneNumber);
178
    }
179
180
    /**
181
     * @When I register this account
182
     * @When I try to register this account
183
     */
184
    public function iRegisterThisAccount()
185
    {
186
        $this->registerPage->register();
187
    }
188
189
    /**
190
     * @Then my email should be :email
191
     * @Then my email should still be :email
192
     */
193
    public function myEmailShouldBe($email)
194
    {
195
        $this->dashboardPage->open();
196
197
        Assert::true(
198
            $this->dashboardPage->hasCustomerEmail($email),
199
            sprintf('Cannot find customer email "%s".', $email)
200
        );
201
    }
202
203
    /**
204
     * @Then /^I should be notified that the ([^"]+) is required$/
205
     */
206
    public function iShouldBeNotifiedThatElementIsRequired($element)
207
    {
208
        $this->assertFieldValidationMessage($element, sprintf('Please enter your %s.', $element));
209
    }
210
211
    /**
212
     * @Then I should be notified that the email is already used
213
     */
214
    public function iShouldBeNotifiedThatTheEmailIsAlreadyUsed()
215
    {
216
        $this->assertFieldValidationMessage('email', 'This email is already used.');
217
    }
218
219
    /**
220
     * @Then I should be notified that the password do not match
221
     */
222
    public function iShouldBeNotifiedThatThePasswordDoNotMatch()
223
    {
224
        $this->assertFieldValidationMessage('password', 'The entered passwords don\'t match');
225
    }
226
227
    /**
228
     * @Then I should be notified that new account has been successfully created
229
     * @Then I should be notified that my account has been created and the verification email has been sent
230
     */
231
    public function iShouldBeNotifiedThatNewAccountHasBeenSuccessfullyCreated()
232
    {
233
        $this->notificationChecker->checkNotification(
234
            'Thank you for registering, check your email to verify your account.',
235
            NotificationType::success()
236
        );
237
    }
238
239
    /**
240
     * @Then I should be logged in
241
     */
242
    public function iShouldBeLoggedIn()
243
    {
244
        Assert::true(
245
            $this->homePage->hasLogoutButton(),
246
            'I should be on home page and, also i should be able to sign out.'
247
        );
248
    }
249
250
    /**
251
     * @Then I should not be logged in
252
     */
253
    public function iShouldNotBeLoggedIn()
254
    {
255
        Assert::false(
256
            $this->homePage->hasLogoutButton(),
257
            'I should not be logged in.'
258
        );
259
    }
260
261
    /**
262
     * @When I register with email :email and password :password
263
     */
264
    public function iRegisterWithEmailAndPassword($email, $password)
265
    {
266
        $this->registerPage->open();
267
        $this->registerPage->specifyEmail($email);
268
        $this->registerPage->specifyPassword($password);
269
        $this->registerPage->verifyPassword($password);
270
        $this->registerPage->specifyFirstName('Carrot');
271
        $this->registerPage->specifyLastName('Ironfoundersson');
272
        $this->registerPage->register();
273
    }
274
275
    /**
276
     * @Then /^(my) account should be verified$/
277
     */
278
    public function myAccountShouldBeVerified(ShopUserInterface $user)
279
    {
280
        $this->securityService->logIn($user);
281
282
        Assert::true(
283
            $this->dashboardPage->isVerified(),
284
            'My account should be verified.'
285
        );
286
    }
287
288
    /**
289
     * @When /^(I) try to verify my account using the link from this email$/
290
     */
291
    public function iUseItToVerify(ShopUserInterface $user)
292
    {
293
        $this->verificationPage->verifyAccount($user->getEmailVerificationToken());
294
    }
295
296
    /**
297
     * @When I resend the verification email
298
     */
299
    public function iResendVerificationEmail()
300
    {
301
        $this->dashboardPage->open();
302
        $this->dashboardPage->pressResendVerificationEmail();
303
    }
304
305
    /**
306
     * @When I use the verification link from the first email to verify
307
     */
308
    public function iUseVerificationLinkFromFirstEmailToVerify()
309
    {
310
        $token = $this->sharedStorage->get('verification_token');
311
312
        $this->verificationPage->verifyAccount($token);
313
    }
314
315
    /**
316
     * @When I (try to )verify using :token token
317
     */
318
    public function iTryToVerifyUsing($token)
319
    {
320
        $this->verificationPage->verifyAccount($token);
321
    }
322
323
    /**
324
     * @Then /^(?:my|his|her) account should not be verified$/
325
     */
326
    public function myAccountShouldNotBeVerified()
327
    {
328
        $this->dashboardPage->open();
329
330
        Assert::false(
331
            $this->dashboardPage->isVerified(),
332
            'Account should not be verified.'
333
        );
334
    }
335
336
    /**
337
     * @Then I should be unable to resend the verification email
338
     */
339
    public function iShouldBeUnableToResendVerificationEmail()
340
    {
341
        $this->dashboardPage->open();
342
343
        Assert::false(
344
            $this->dashboardPage->hasResendVerificationEmailButton(),
345
            'You should not be able to resend the verification email.'
346
        );
347
    }
348
349
    /**
350
     * @Then I should be notified that the verification was successful
351
     */
352
    public function iShouldBeNotifiedThatTheVerificationWasSuccessful()
353
    {
354
        $this->notificationChecker->checkNotification('has been successfully verified.', NotificationType::success());
355
    }
356
357
    /**
358
     * @Then I should be notified that the verification token is invalid
359
     */
360
    public function iShouldBeNotifiedThatTheVerificationTokenIsInvalid()
361
    {
362
        $this->notificationChecker->checkNotification('The verification token is invalid.', NotificationType::failure());
363
    }
364
365
    /**
366
     * @Then I should be notified that the verification email has been sent
367
     */
368
    public function iShouldBeNotifiedThatTheVerificationEmailHasBeenSent()
369
    {
370
        $this->notificationChecker->checkNotification(
371
            'An email with the verification link has been sent to your email address.',
372
            NotificationType::success()
373
        );
374
    }
375
376
    /**
377
     * @Then it should be sent to :recipient
378
     */
379
    public function anEmailShouldBeSentTo($recipient)
380
    {
381
        $this->assertEmailHasRecipient($recipient);
382
    }
383
384
    /**
385
     * @Then :count email(s) should be sent to :recipient
386
     */
387
    public function numberOfEmailsShouldBeSentTo($count, $recipient)
388
    {
389
        $this->assertEmailHasRecipient($recipient);
390
391
        Assert::eq(
392
            $this->emailChecker->getMessagesCount(),
393
            $count,
394
            sprintf(
395
                '%d messages were sent, while there should be %d.',
396
                $this->emailChecker->getMessagesCount(),
397
                $count
398
            )
399
        );
400
    }
401
402
    /**
403
     * @Then a welcoming email should have been sent to :recipient
404
     */
405
    public function aWelcomingEmailShouldHaveBeenSentTo($recipient)
406
    {
407
        $this->assertEmailHasRecipient($recipient);
408
        $this->assertEmailContainsMessage('Welcome to our store');
409
410
    }
411
412
    /**
413
     * @param string $recipient
414
     */
415
    private function assertEmailHasRecipient($recipient)
416
    {
417
        Assert::true(
418
            $this->emailChecker->hasRecipient($recipient),
419
            'An email should have been sent to %s.'
420
        );
421
    }
422
423
    /**
424
     * @param string $message
425
     */
426
    private function assertEmailContainsMessage($message)
427
    {
428
        Assert::true(
429
            $this->emailChecker->hasMessage($message),
430
            sprintf('Message "%s" was not found in any email.', $message)
431
        );
432
    }
433
434
    /**
435
     * @param string $element
436
     * @param string $expectedMessage
437
     */
438
    private function assertFieldValidationMessage($element, $expectedMessage)
439
    {
440
        Assert::true(
441
            $this->registerPage->checkValidationMessageFor($element, $expectedMessage),
442
            sprintf('The %s should be required.', $element)
443
        );
444
    }
445
}
446