Completed
Push — master ( 84981d...080a22 )
by Kamil
05:57
created

ManagingCustomersContext::iSortThemByChannel()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
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
declare(strict_types=1);
13
14
namespace Sylius\Behat\Context\Ui\Admin;
15
16
use Behat\Behat\Context\Context;
17
use Sylius\Behat\Page\Admin\Crud\IndexPageInterface;
18
use Sylius\Behat\Page\Admin\Customer\CreatePageInterface;
19
use Sylius\Behat\Page\Admin\Customer\IndexPageInterface as CustomerIndexPageInterface;
20
use Sylius\Behat\Page\Admin\Customer\ShowPageInterface;
21
use Sylius\Behat\Page\Admin\Customer\UpdatePageInterface;
22
use Sylius\Behat\Service\Resolver\CurrentPageResolverInterface;
23
use Sylius\Component\Core\Model\CustomerInterface;
24
use Webmozart\Assert\Assert;
25
26
final class ManagingCustomersContext implements Context
27
{
28
    /** @var CustomerIndexPageInterface */
29
    private $indexPage;
30
31
    /** @var CreatePageInterface */
32
    private $createPage;
33
34
    /** @var UpdatePageInterface */
35
    private $updatePage;
36
37
    /** @var ShowPageInterface */
38
    private $showPage;
39
40
    /** @var IndexPageInterface */
41
    private $ordersIndexPage;
42
43
    /** @var CurrentPageResolverInterface */
44
    private $currentPageResolver;
45
46
    /**
47
     * @param CustomerIndexPageInterface $indexPage
0 ignored issues
show
Documentation introduced by
Consider making the type for parameter $indexPage a bit more specific; maybe use IndexPageInterface.
Loading history...
48
     */
49
    public function __construct(
50
        CreatePageInterface $createPage,
51
        IndexPageInterface $indexPage,
52
        UpdatePageInterface $updatePage,
53
        ShowPageInterface $showPage,
54
        IndexPageInterface $ordersIndexPage,
55
        CurrentPageResolverInterface $currentPageResolver
56
    ) {
57
        $this->createPage = $createPage;
58
        $this->indexPage = $indexPage;
59
        $this->updatePage = $updatePage;
60
        $this->showPage = $showPage;
61
        $this->ordersIndexPage = $ordersIndexPage;
62
        $this->currentPageResolver = $currentPageResolver;
63
    }
64
65
    /**
66
     * @Given I want to create a new customer
67
     * @Given I want to create a new customer account
68
     */
69
    public function iWantToCreateANewCustomer()
70
    {
71
        $this->createPage->open();
72
    }
73
74
    /**
75
     * @When /^I specify (?:their|his) first name as "([^"]*)"$/
76
     */
77
    public function iSpecifyItsFirstNameAs($name)
78
    {
79
        $this->createPage->specifyFirstName($name);
80
    }
81
82
    /**
83
     * @When /^I specify (?:their|his) last name as "([^"]*)"$/
84
     */
85
    public function iSpecifyItsLastNameAs($name)
86
    {
87
        $this->createPage->specifyLastName($name);
88
    }
89
90
    /**
91
     * @When I specify their email as :name
92
     * @When I do not specify their email
93
     */
94
    public function iSpecifyItsEmailAs($email = null)
95
    {
96
        $this->createPage->specifyEmail($email ?? '');
97
    }
98
99
    /**
100
     * @When I change their email to :email
101
     * @When I remove its email
102
     */
103
    public function iChangeTheirEmailTo($email = null): void
104
    {
105
        $this->updatePage->changeEmail($email ?? '');
106
    }
107
108
    /**
109
     * @When I add them
110
     * @When I try to add them
111
     */
112
    public function iAddIt()
113
    {
114
        $this->createPage->create();
115
    }
116
117
    /**
118
     * @Then the customer :customer should appear in the store
119
     * @Then the customer :customer should still have this email
120
     */
121
    public function theCustomerShould(CustomerInterface $customer)
122
    {
123
        $this->indexPage->open();
124
125
        Assert::true($this->indexPage->isSingleResourceOnPage(['email' => $customer->getEmail()]));
126
    }
127
128
    /**
129
     * @When I select :gender as its gender
130
     */
131
    public function iSelectGender($gender)
132
    {
133
        $this->createPage->chooseGender($gender);
134
    }
135
136
    /**
137
     * @When I select :group as their group
138
     */
139
    public function iSelectGroup($group)
140
    {
141
        $this->createPage->chooseGroup($group);
142
    }
143
144
    /**
145
     * @When I specify its birthday as :birthday
146
     */
147
    public function iSpecifyItsBirthdayAs($birthday)
148
    {
149
        $this->createPage->specifyBirthday($birthday);
150
    }
151
152
    /**
153
     * @When /^I want to edit (this customer)$/
154
     */
155
    public function iWantToEditThisCustomer(CustomerInterface $customer)
156
    {
157
        $this->updatePage->open(['id' => $customer->getId()]);
158
    }
159
160
    /**
161
     * @When I save my changes
162
     * @When I try to save my changes
163
     */
164
    public function iSaveMyChanges()
165
    {
166
        $this->updatePage->saveChanges();
167
    }
168
169
    /**
170
     * @Then /^(this customer) with name "([^"]*)" should appear in the store$/
171
     */
172
    public function theCustomerWithNameShouldAppearInTheRegistry(CustomerInterface $customer, $name)
173
    {
174
        $this->updatePage->open(['id' => $customer->getId()]);
175
176
        Assert::same($this->updatePage->getFullName(), $name);
177
    }
178
179
    /**
180
     * @When I want to see all customers in store
181
     */
182
    public function iWantToSeeAllCustomersInStore()
183
    {
184
        $this->indexPage->open();
185
    }
186
187
    /**
188
     * @Then /^I should see (\d+) customers in the list$/
189
     */
190
    public function iShouldSeeCustomersInTheList($amountOfCustomers)
191
    {
192
        Assert::same($this->indexPage->countItems(), (int) $amountOfCustomers);
193
    }
194
195
    /**
196
     * @Then I should see the customer :email in the list
197
     */
198
    public function iShouldSeeTheCustomerInTheList($email)
199
    {
200
        Assert::true($this->indexPage->isSingleResourceOnPage(['email' => $email]));
201
    }
202
203
    /**
204
     * @Then /^I should be notified that ([^"]+) is required$/
205
     */
206
    public function iShouldBeNotifiedThatFirstNameIsRequired($elementName)
207
    {
208
        Assert::same(
209
            $this->createPage->getValidationMessage($elementName),
210
            sprintf('Please enter your %s.', $elementName)
211
        );
212
    }
213
214
    /**
215
     * @Then /^I should be notified that ([^"]+) should be ([^"]+)$/
216
     */
217
    public function iShouldBeNotifiedThatTheElementShouldBe($elementName, $validationMessage)
218
    {
219
        Assert::same(
220
            $this->updatePage->getValidationMessage($elementName),
221
            sprintf('%s must be %s.', ucfirst($elementName), $validationMessage)
222
        );
223
    }
224
225
    /**
226
     * @Then the customer with email :email should not appear in the store
227
     */
228
    public function theCustomerShouldNotAppearInTheStore($email)
229
    {
230
        $this->indexPage->open();
231
232
        Assert::false($this->indexPage->isSingleResourceOnPage(['email' => $email]));
233
    }
234
235
    /**
236
     * @When I remove its first name
237
     */
238
    public function iRemoveItsFirstName()
239
    {
240
        $this->updatePage->changeFirstName('');
241
    }
242
243
    /**
244
     * @Then /^(this customer) should have an empty first name$/
245
     * @Then the customer :customer should still have an empty first name
246
     */
247
    public function theCustomerShouldStillHaveAnEmptyFirstName(CustomerInterface $customer)
248
    {
249
        $this->updatePage->open(['id' => $customer->getId()]);
250
251
        Assert::eq($this->updatePage->getFirstName(), '');
252
    }
253
254
    /**
255
     * @When I remove its last name
256
     */
257
    public function iRemoveItsLastName()
258
    {
259
        $this->updatePage->changeLastName('');
260
    }
261
262
    /**
263
     * @Then /^(this customer) should have an empty last name$/
264
     * @Then the customer :customer should still have an empty last name
265
     */
266
    public function theCustomerShouldStillHaveAnEmptyLastName(CustomerInterface $customer)
267
    {
268
        $this->updatePage->open(['id' => $customer->getId()]);
269
270
        Assert::eq($this->updatePage->getLastName(), '');
271
    }
272
273
    /**
274
     * @Then I should be notified that email is not valid
275
     */
276
    public function iShouldBeNotifiedThatEmailIsNotValid()
277
    {
278
        Assert::same($this->createPage->getValidationMessage('email'), 'This email is invalid.');
279
    }
280
281
    /**
282
     * @Then I should be notified that email must be unique
283
     */
284
    public function iShouldBeNotifiedThatEmailMustBeUnique()
285
    {
286
        Assert::same($this->createPage->getValidationMessage('email'), 'This email is already used.');
287
    }
288
289
    /**
290
     * @Then there should still be only one customer with email :email
291
     */
292
    public function thereShouldStillBeOnlyOneCustomerWithEmail($email)
293
    {
294
        $this->indexPage->open();
295
296
        Assert::true($this->indexPage->isSingleResourceOnPage(['email' => $email]));
297
    }
298
299
    /**
300
     * @Given I want to enable :customer
301
     * @Given I want to disable :customer
302
     */
303
    public function iWantToChangeStatusOf(CustomerInterface $customer)
304
    {
305
        $this->updatePage->open(['id' => $customer->getId()]);
306
    }
307
308
    /**
309
     * @When I enable their account
310
     */
311
    public function iEnableIt()
312
    {
313
        $this->updatePage->enable();
314
    }
315
316
    /**
317
     * @When I disable their account
318
     */
319
    public function iDisableIt()
320
    {
321
        $this->updatePage->disable();
322
    }
323
324
    /**
325
     * @Then /^(this customer) should be enabled$/
326
     */
327
    public function thisCustomerShouldBeEnabled(CustomerInterface $customer)
328
    {
329
        $this->indexPage->open();
330
331
        Assert::eq($this->indexPage->getCustomerAccountStatus($customer), 'Enabled');
332
    }
333
334
    /**
335
     * @Then /^(this customer) should be disabled$/
336
     */
337
    public function thisCustomerShouldBeDisabled(CustomerInterface $customer)
338
    {
339
        $this->indexPage->open();
340
341
        Assert::eq($this->indexPage->getCustomerAccountStatus($customer), 'Disabled');
342
    }
343
344
    /**
345
     * @When I specify their password as :password
346
     */
347
    public function iSpecifyItsPasswordAs($password)
348
    {
349
        $this->createPage->specifyPassword($password);
350
    }
351
352
    /**
353
     * @When I choose create account option
354
     */
355
    public function iChooseCreateAccountOption()
356
    {
357
        $this->createPage->selectCreateAccount();
358
    }
359
360
    /**
361
     * @When I browse orders of a customer :customer
362
     */
363
    public function iBrowseOrdersOfACustomer(CustomerInterface $customer): void
364
    {
365
        $this->ordersIndexPage->open(['id' => $customer->getId()]);
366
    }
367
368
    /**
369
     * @When I sort them by :sortBy
370
     */
371
    public function iSortThemByChannel(string $sortBy): void
372
    {
373
        $this->ordersIndexPage->sort(ucfirst($sortBy));
374
    }
375
376
    /**
377
     * @Then the customer :customer should have an account created
378
     * @Then /^(this customer) should have an account created$/
379
     */
380
    public function theyShouldHaveAnAccountCreated(CustomerInterface $customer): void
381
    {
382
        Assert::notNull(
383
            $customer->getUser()->getPassword(),
384
            'Customer should have an account, but they do not.'
385
        );
386
    }
387
388
    /**
389
     * @When I view details of the customer :customer
390
     * @When /^I view (their) details$/
391
     */
392
    public function iViewDetailsOfTheCustomer(CustomerInterface $customer)
393
    {
394
        $this->showPage->open(['id' => $customer->getId()]);
395
    }
396
397
    /**
398
     * @Then his name should be :name
399
     */
400
    public function hisNameShouldBe($name)
401
    {
402
        Assert::same($this->showPage->getCustomerName(), $name);
403
    }
404
405
    /**
406
     * @Then he should be registered since :registrationDate
407
     */
408
    public function hisRegistrationDateShouldBe($registrationDate)
409
    {
410
        Assert::eq($this->showPage->getRegistrationDate(), new \DateTime($registrationDate));
411
    }
412
413
    /**
414
     * @Then his email should be :email
415
     */
416
    public function hisEmailShouldBe($email)
417
    {
418
        Assert::same($this->showPage->getCustomerEmail(), $email);
419
    }
420
421
    /**
422
     * @Then his phone number should be :phoneNumber
423
     */
424
    public function hisPhoneNumberShouldBe($phoneNumber)
425
    {
426
        Assert::same($this->showPage->getCustomerPhoneNumber(), $phoneNumber);
427
    }
428
429
    /**
430
     * @Then his default address should be :defaultAddress
431
     */
432
    public function hisShippingAddressShouldBe($defaultAddress)
433
    {
434
        Assert::same($this->showPage->getDefaultAddress(), str_replace(',', '', $defaultAddress));
435
    }
436
437
    /**
438
     * @Then I should see information about no existing account for this customer
439
     */
440
    public function iShouldSeeInformationAboutNoExistingAccountForThisCustomer()
441
    {
442
        Assert::true($this->showPage->hasAccount());
443
    }
444
445
    /**
446
     * @Then I should see that this customer is subscribed to the newsletter
447
     */
448
    public function iShouldSeeThatThisCustomerIsSubscribedToTheNewsletter()
449
    {
450
        Assert::true($this->showPage->isSubscribedToNewsletter());
451
    }
452
453
    /**
454
     * @Then I should not see information about email verification
455
     */
456
    public function iShouldSeeInformationAboutEmailVerification()
457
    {
458
        Assert::true($this->showPage->hasEmailVerificationInformation());
459
    }
460
461
    /**
462
     * @When I make them subscribed to the newsletter
463
     */
464
    public function iMakeThemSubscribedToTheNewsletter()
465
    {
466
        $this->updatePage->subscribeToTheNewsletter();
467
    }
468
469
    /**
470
     * @When I change the password of user :customer to :newPassword
471
     */
472
    public function iChangeThePasswordOfUserTo(CustomerInterface $customer, $newPassword)
473
    {
474
        $this->updatePage->open(['id' => $customer->getId()]);
475
        $this->updatePage->changePassword($newPassword);
476
        $this->updatePage->saveChanges();
477
    }
478
479
    /**
480
     * @Then this customer should be subscribed to the newsletter
481
     */
482
    public function thisCustomerShouldBeSubscribedToTheNewsletter()
483
    {
484
        Assert::true($this->updatePage->isSubscribedToTheNewsletter());
485
    }
486
487
    /**
488
     * @Then the province in the default address should be :provinceName
489
     */
490
    public function theProvinceInTheDefaultAddressShouldBe($provinceName)
491
    {
492
        Assert::true($this->showPage->hasDefaultAddressProvinceName($provinceName));
493
    }
494
495
    /**
496
     * @Then this customer should have :groupName as their group
497
     */
498
    public function thisCustomerShouldHaveAsTheirGroup($groupName)
499
    {
500
        /** @var UpdatePageInterface|ShowPageInterface $currentPage */
501
        $currentPage = $this->currentPageResolver->getCurrentPageWithForm([$this->updatePage, $this->showPage]);
502
503
        Assert::same($currentPage->getGroupName(), $groupName);
504
    }
505
506
    /**
507
     * @Then I should see that this customer has verified the email
508
     */
509
    public function iShouldSeeThatThisCustomerHasVerifiedTheEmail()
510
    {
511
        Assert::true($this->showPage->hasVerifiedEmail());
512
    }
513
514
    /**
515
     * @Then I should see a single order in the list
516
     */
517
    public function iShouldSeeASingleOrderInTheList()
518
    {
519
        Assert::same($this->ordersIndexPage->countItems(), 1);
520
    }
521
522
    /**
523
     * @Then I should see the order with number :orderNumber in the list
524
     */
525
    public function iShouldSeeASingleOrderFromCustomer($orderNumber)
526
    {
527
        Assert::true($this->indexPage->isSingleResourceOnPage(['number' => $orderNumber]));
528
    }
529
530
    /**
531
     * @Then I should not see the order with number :orderNumber in the list
532
     */
533
    public function iShouldNotSeeASingleOrderFromCustomer($orderNumber)
534
    {
535
        Assert::false($this->indexPage->isSingleResourceOnPage(['number' => $orderNumber]));
536
    }
537
538
    /**
539
     * @When I do not specify any information
540
     */
541
    public function iDoNotSpecifyAnyInformation()
542
    {
543
        // Intentionally left blank.
544
    }
545
546
    /**
547
     * @Then I should not be able to specify their password
548
     */
549
    public function iShouldNotBeAbleToSpecifyItPassword()
550
    {
551
        Assert::true($this->createPage->isUserFormHidden());
552
    }
553
554
    /**
555
     * @Then I should still be on the customer creation page
556
     */
557
    public function iShouldBeOnTheCustomerCreationPage()
558
    {
559
        $this->createPage->verify();
560
    }
561
562
    /**
563
     * @Then I should be able to select create account option
564
     */
565
    public function iShouldBeAbleToSelectCreateAccountOption()
566
    {
567
        Assert::false($this->createPage->hasCheckedCreateOption());
568
    }
569
570
    /**
571
     * @Then I should be able to specify their password
572
     */
573
    public function iShouldBeAbleToSpecifyItPassword()
574
    {
575
        Assert::true($this->createPage->hasPasswordField());
576
    }
577
578
    /**
579
     * @Then I should not be able to select create account option
580
     */
581
    public function iShouldNotBeAbleToSelectCreateAccountOption()
582
    {
583
        Assert::true($this->createPage->hasCheckedCreateOption());
584
    }
585
586
    /**
587
     * @When I do not choose create account option
588
     */
589
    public function iDoNotChooseCreateAccountOption()
590
    {
591
        // Intentionally left blank.
592
    }
593
594
    /**
595
     * @Then I should not see create account option
596
     */
597
    public function iShouldNotSeeCreateAccountOption()
598
    {
599
        Assert::false($this->createPage->hasCreateOption());
600
    }
601
602
    /**
603
     * @Then /^I should be notified that the password must be at least (\d+) characters long$/
604
     */
605
    public function iShouldBeNotifiedThatThePasswordMustBeAtLeastCharactersLong($amountOfCharacters)
606
    {
607
        Assert::same(
608
            $this->createPage->getValidationMessage('password'),
609
            sprintf('Password must be at least %d characters long.', $amountOfCharacters)
610
        );
611
    }
612
613
    /**
614
     * @Then I should see the customer has not placed any orders yet
615
     */
616
    public function iShouldSeeTheCustomerHasNotYetPlacedAnyOrders()
617
    {
618
        Assert::false($this->showPage->hasCustomerPlacedAnyOrders());
619
    }
620
621
    /**
622
     * @Then /^I should see that they have placed (\d+) orders? in the "([^"]+)" channel$/
623
     */
624
    public function iShouldSeeThatTheyHavePlacedOrdersInTheChannel($ordersCount, $channelName)
625
    {
626
        Assert::same($this->showPage->getOrdersCountInChannel($channelName), (int) $ordersCount);
627
    }
628
629
    /**
630
     * @Then /^I should see that the overall total value of all their orders in the "([^"]+)" channel is "([^"]+)"$/
631
     */
632
    public function iShouldSeeThatTheOverallTotalValueOfAllTheirOrdersInTheChannelIs($channelName, $ordersValue)
633
    {
634
        Assert::same($this->showPage->getOrdersTotalInChannel($channelName), $ordersValue);
635
    }
636
637
    /**
638
     * @Then /^I should see that the average total value of their order in the "([^"]+)" channel is "([^"]+)"$/
639
     */
640
    public function iShouldSeeThatTheAverageTotalValueOfTheirOrderInTheChannelIs($channelName, $ordersValue)
641
    {
642
        Assert::same($this->showPage->getOrdersTotalInChannel($channelName), $ordersValue);
643
    }
644
}
645