Completed
Push — master ( 85508a...fbb5f8 )
by Kamil
24:40
created

dBe()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
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\Admin;
13
14
use Behat\Behat\Context\Context;
15
use Sylius\Behat\Page\Admin\Crud\IndexPageInterface;
16
use Sylius\Behat\Page\Admin\Customer\CreatePageInterface;
17
use Sylius\Behat\Page\Admin\Customer\ShowPageInterface;
18
use Sylius\Behat\Page\Admin\Customer\UpdatePageInterface;
19
use Sylius\Behat\Service\SharedStorageInterface;
20
use Sylius\Component\Core\Model\Customer;
21
use Sylius\Component\Customer\Model\CustomerInterface;
22
use Webmozart\Assert\Assert;
23
24
/**
25
 * @author Anna Walasek <[email protected]>
26
 */
27
final class ManagingCustomersContext implements Context
28
{
29
    /**
30
     * @var SharedStorageInterface
31
     */
32
    private $sharedStorage;
33
34
    /**
35
     * @var IndexPageInterface
36
     */
37
    private $indexPage;
38
39
    /**
40
     * @var CreatePageInterface
41
     */
42
    private $createPage;
43
44
    /**
45
     * @var UpdatePageInterface
46
     */
47
    private $updatePage;
48
49
    /**
50
     * @var ShowPageInterface
51
     */
52
    private $showPage;
53
54
    /**
55
     * @param SharedStorageInterface $sharedStorage
56
     * @param CreatePageInterface $createPage
57
     * @param IndexPageInterface $indexPage
58
     * @param UpdatePageInterface $updatePage
59
     * @param ShowPageInterface $showPage
60
     */
61
    public function __construct(
62
        SharedStorageInterface $sharedStorage,
63
        CreatePageInterface $createPage,
64
        IndexPageInterface $indexPage,
65
        UpdatePageInterface $updatePage,
66
        ShowPageInterface $showPage
67
    ) {
68
        $this->sharedStorage = $sharedStorage;
69
        $this->createPage = $createPage;
70
        $this->indexPage = $indexPage;
71
        $this->updatePage = $updatePage;
72
        $this->showPage = $showPage;
73
    }
74
75
    /**
76
     * @Given I want to create a new customer
77
     * @Given I want to create a new customer account
78
     */
79
    public function iWantToCreateANewCustomer()
80
    {
81
        $this->createPage->open();
82
    }
83
84
    /**
85
     * @When /^I specify (?:their|his) first name as "([^"]*)"$/
86
     */
87
    public function iSpecifyItsFirstNameAs($name)
88
    {
89
        $this->createPage->specifyFirstName($name);
90
    }
91
92
    /**
93
     * @When /^I specify (?:their|his) last name as "([^"]*)"$/
94
     */
95
    public function iSpecifyItsLastNameAs($name)
96
    {
97
        $this->createPage->specifyLastName($name);
98
    }
99
100
    /**
101
     * @When I specify their email as :name
102
     */
103
    public function iSpecifyItsEmailAs($email)
104
    {
105
        $this->createPage->specifyEmail($email);
106
    }
107
108
    /**
109
     * @When I add them
110
     * @When I try to add it
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(
126
            $this->indexPage->isSingleResourceOnPage(['email' => $customer->getEmail()]),
127
            sprintf('Customer with email %s should exist but it does not.', $customer->getEmail())
128
        );
129
    }
130
131
    /**
132
     * @When I select :gender as its gender
133
     */
134
    public function iSelectGender($gender)
135
    {
136
        $this->createPage->chooseGender($gender);
137
    }
138
139
    /**
140
     * @When I select :group as their group
141
     */
142
    public function iSelectGroup($group)
143
    {
144
        $this->createPage->chooseGroup($group);
145
    }
146
147
    /**
148
     * @When I specify its birthday as :birthday
149
     */
150
    public function iSpecifyItsBirthdayAs($birthday)
151
    {
152
        $this->createPage->specifyBirthday($birthday);
153
    }
154
155
    /**
156
     * @Given /^I want to edit (this customer)$/
157
     * @Given I want to edit the customer :customer
158
     */
159
    public function iWantToEditThisCustomer(CustomerInterface $customer)
160
    {
161
        $this->updatePage->open(['id' => $customer->getId()]);
162
    }
163
164
    /**
165
     * @Given I want to change my password
166
     */
167
    public function iWantToChangeMyPassword()
168
    {
169
        $customer = $this->sharedStorage->get('customer');
170
171
        $this->updatePage->open(['id' => $customer->getId()]);
172
    }
173
174
    /**
175
     * @When I save my changes
176
     * @When I try to save my changes
177
     */
178
    public function iSaveMyChanges()
179
    {
180
        $this->updatePage->saveChanges();
181
    }
182
183
    /**
184
     * @Then /^(this customer) with name "([^"]*)" should appear in the store$/
185
     */
186
    public function theCustomerWithNameShouldAppearInTheRegistry(CustomerInterface $customer, $name)
187
    {
188
        $this->updatePage->open(['id' => $customer->getId()]);
189
190
        Assert::eq(
191
            $name,
192
            $this->updatePage->getFullName(),
193
            sprintf('Customer should have name %s, but they have %s.', $name, $this->updatePage->getFullName())
194
        );
195
    }
196
197
    /**
198
     * @When I want to see all customers in store
199
     */
200
    public function iWantToSeeAllCustomersInStore()
201
    {
202
        $this->indexPage->open();
203
    }
204
205
    /**
206
     * @Then /^I should see (\d+) customers in the list$/
207
     */
208
    public function iShouldSeeCustomersInTheList($amountOfCustomers)
209
    {
210
        Assert::same(
211
            (int) $amountOfCustomers,
212
            $this->indexPage->countItems(),
213
            sprintf('Amount of customers should be equal %s, but is not.', $amountOfCustomers)
214
        );
215
    }
216
217
    /**
218
     * @Then I should see the customer :email in the list
219
     */
220
    public function iShouldSeeTheCustomerInTheList($email)
221
    {
222
        Assert::true(
223
            $this->indexPage->isSingleResourceOnPage(['email' => $email]),
224
            sprintf('Customer with email %s should exist but it does not.', $email)
225
        );
226
    }
227
228
    /**
229
     * @Then /^I should be notified that ([^"]+) is required$/
230
     */
231
    public function iShouldBeNotifiedThatFirstNameIsRequired($elementName)
232
    {
233
        Assert::same($this->createPage->getValidationMessage($elementName), sprintf('Please enter your %s.', $elementName));
234
    }
235
236
    /**
237
     * @Then /^I should be notified that ([^"]+) should be ([^"]+)$/
238
     */
239
    public function iShouldBeNotifiedThatTheElementShouldBe($elementName, $validationMessage)
240
    {
241
        Assert::same(
242
            $this->updatePage->getValidationMessage($elementName),
243
            sprintf('%s must be %s.', ucfirst($elementName), $validationMessage)
244
        );
245
    }
246
247
    /**
248
     * @Then the customer with email :email should not appear in the store
249
     */
250
    public function theCustomerShouldNotAppearInTheStore($email)
251
    {
252
        $this->indexPage->open();
253
254
        Assert::false(
255
            $this->indexPage->isSingleResourceOnPage(['email' => $email]),
256
            sprintf('Customer with email %s was created, but it should not.', $email)
257
        );
258
    }
259
260
    /**
261
     * @When I remove its first name
262
     */
263
    public function iRemoveItsFirstName()
264
    {
265
        $this->updatePage->changeFirstName('');
266
    }
267
268
    /**
269
     * @Then /^(this customer) should have an empty first name$/
270
     * @Then the customer :customer should still have an empty first name
271
     */
272
    public function theCustomerShouldStillHaveAnEmptyFirstName(CustomerInterface $customer)
273
    {
274
        $this->updatePage->open(['id' => $customer->getId()]);
275
276
        Assert::eq(
277
            '',
278
            $this->updatePage->getFirstName(),
279
            'Customer should have an empty first name, but it does not.'
280
        );
281
    }
282
283
    /**
284
     * @When I remove its last name
285
     */
286
    public function iRemoveItsLastName()
287
    {
288
        $this->updatePage->changeLastName('');
289
    }
290
291
    /**
292
     * @Then /^(this customer) should have an empty last name$/
293
     * @Then the customer :customer should still have an empty last name
294
     */
295
    public function theCustomerShouldStillHaveAnEmptyLastName(CustomerInterface $customer)
296
    {
297
        $this->updatePage->open(['id' => $customer->getId()]);
298
299
        Assert::eq(
300
            '',
301
            $this->updatePage->getLastName(),
302
            'Customer should have an empty last name, but it does not.'
303
        );
304
    }
305
306
    /**
307
     * @When I remove its email
308
     */
309
    public function iRemoveItsEmail()
310
    {
311
        $this->updatePage->changeEmail('');
312
    }
313
314
    /**
315
     * @Then I should be notified that email is not valid
316
     */
317
    public function iShouldBeNotifiedThatEmailIsNotValid()
318
    {
319
        Assert::same($this->createPage->getValidationMessage('email'), 'This email is invalid.');
320
    }
321
322
    /**
323
     * @Then I should be notified that email must be unique
324
     */
325
    public function iShouldBeNotifiedThatEmailMustBeUnique()
326
    {
327
        Assert::same($this->createPage->getValidationMessage('email'), 'This email is already used.');
328
    }
329
330
    /**
331
     * @Then there should still be only one customer with email :email
332
     */
333
    public function thereShouldStillBeOnlyOneCustomerWithEmail($email)
334
    {
335
        $this->indexPage->open();
336
337
        Assert::true(
338
            $this->indexPage->isSingleResourceOnPage(['email' => $email]),
339
            sprintf('Customer with email %s cannot be found.', $email)
340
        );
341
    }
342
343
    /**
344
     * @Given I want to enable :customer
345
     * @Given I want to disable :customer
346
     */
347
    public function iWantToChangeStatusOf(CustomerInterface $customer)
348
    {
349
        $this->updatePage->open(['id' => $customer->getId()]);
350
    }
351
352
    /**
353
     * @When I enable their account
354
     */
355
    public function iEnableIt()
356
    {
357
        $this->updatePage->enable();
358
    }
359
360
    /**
361
     * @When I disable their account
362
     */
363
    public function iDisableIt()
364
    {
365
        $this->updatePage->disable();
366
    }
367
368
    /**
369
     * @Then /^(this customer) should be enabled$/
370
     */
371
    public function thisCustomerShouldBeEnabled(CustomerInterface $customer)
372
    {
373
        $this->indexPage->open();
374
375
        Assert::eq(
376
            'Enabled',
377
            $this->indexPage->getCustomerAccountStatus($customer),
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Sylius\Behat\Page\Admin\Crud\IndexPageInterface as the method getCustomerAccountStatus() does only exist in the following implementations of said interface: Sylius\Behat\Page\Admin\Customer\IndexPage.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
378
            'Customer account should be enabled, but it does not.'
379
        );
380
    }
381
382
    /**
383
     * @Then /^(this customer) should be disabled$/
384
     */
385
    public function thisCustomerShouldBeDisabled(CustomerInterface $customer)
386
    {
387
        $this->indexPage->open();
388
389
        Assert::eq(
390
            'Disabled',
391
            $this->indexPage->getCustomerAccountStatus($customer),
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Sylius\Behat\Page\Admin\Crud\IndexPageInterface as the method getCustomerAccountStatus() does only exist in the following implementations of said interface: Sylius\Behat\Page\Admin\Customer\IndexPage.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
392
            'Customer account should be disabled, but it does not.'
393
        );
394
    }
395
396
    /**
397
     * @When I specify its password as :password
398
     */
399
    public function iSpecifyItsPasswordAs($password)
400
    {
401
        $this->createPage->specifyPassword($password);
402
    }
403
404
    /**
405
     * @When I change my password to :password
406
     */
407
    public function iSpecifyMyPasswordAs($password)
408
    {
409
        $this->updatePage->changePassword($password);
410
    }
411
412
    /**
413
     * @When I choose create account option
414
     */
415
    public function iChooseCreateAccountOption()
416
    {
417
        $this->createPage->selectCreateAccount();
418
    }
419
420
    /**
421
     * @Then the customer :customer should have an account created
422
     * @Then /^(this customer) should have an account created$/
423
     */
424
    public function theyShouldHaveAnAccountCreated(CustomerInterface $customer)
425
    {
426
        Assert::notNull(
427
            $customer->getUser()->getPassword(),
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Sylius\Component\Customer\Model\CustomerInterface as the method getUser() does only exist in the following implementations of said interface: Sylius\Component\Core\Model\Customer.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
428
            'Customer should have an account, but they do not.'
429
        );
430
    }
431
432
    /**
433
     * @When I view details of the customer :customer
434
     */
435
    public function iViewDetailsOfTheCustomer(CustomerInterface $customer)
436
    {
437
        $this->showPage->open(['id' => $customer->getId()]);
438
    }
439
440
    /**
441
     * @Then his name should be :name
442
     */
443
    public function hisNameShouldBe($name)
444
    {
445
        Assert::same(
446
            $name,
447
            $this->showPage->getCustomerName(),
448
            'Customer name should be "%s", but it is not.'
449
        );
450
    }
451
452
    /**
453
     * @Given he should be registered since :registrationDate
454
     */
455
    public function hisRegistrationDateShouldBe($registrationDate)
456
    {
457
        Assert::eq(
458
            new \DateTime($registrationDate),
459
            $this->showPage->getRegistrationDate(),
460
            'Customer registration date should be "%s", but it is not.'
461
        );
462
    }
463
464
    /**
465
     * @Given his email should be :email
466
     */
467
    public function hisEmailShouldBe($email)
468
    {
469
        Assert::same(
470
            $email,
471
            $this->showPage->getCustomerEmail(),
472
            'Customer email should be "%s", but it is not'
473
        );
474
    }
475
476
    /**
477
     * @Then his default address should be :defaultAddress
478
     */
479
    public function hisShippingAddressShouldBe($defaultAddress)
480
    {
481
        Assert::same(
482
            str_replace(',', '', $defaultAddress),
483
            $this->showPage->getDefaultAddress(),
484
            'Customer\'s default address should be "%s", but it is not.'
485
        );
486
    }
487
488
    /**
489
     * @Then I should see information about no existing account for this customer
490
     */
491
    public function iShouldSeeInformationAboutNoExistingAccountForThisCustomer()
492
    {
493
        Assert::true(
494
            $this->showPage->hasAccount(),
495
            'There should be information about no account, but there is none.'
496
        );
497
    }
498
499
    /**
500
     * @Then I should see that this customer is subscribed to the newsletter
501
     */
502
    public function iShouldSeeThatThisCustomerIsSubscribedToTheNewsletter()
503
    {
504
        Assert::true(
505
            $this->showPage->isSubscribedToNewsletter(),
506
            'There should be information that this customer is subscribed to the newsletter.'
507
        );
508
    }
509
510
    /**
511
     * @When I make them subscribed to the newsletter
512
     */
513
    public function iMakeThemSubscribedToTheNewsletter()
514
    {
515
        $this->updatePage->subscribeToTheNewsletter();
516
    }
517
518
    /**
519
     * @Then this customer should be subscribed to the newsletter
520
     */
521
    public function thisCustomerShouldBeSubscribedToTheNewsletter()
522
    {
523
        Assert::true(
524
            $this->updatePage->isSubscribedToTheNewsletter(),
525
            'This customer should subscribe to the newsletter.'
526
        );
527
    }
528
529
    /**
530
     * @Then the province in the default address should be :provinceName
531
     */
532
    public function theProvinceInTheDefaultAddressShouldBe($provinceName)
533
    {
534
        Assert::true(
535
            $this->showPage->hasDefaultAddressProvinceName($provinceName),
536
            sprintf('Cannot find shipping address with province %s', $provinceName)
537
        );
538
    }
539
540
    /**
541
     * @Then /^(this customer) should have "([^"]+)" as their group$/
542
     */
543
    public function thisCustomerShouldHaveAsTheirGroup(CustomerInterface $customer, $groupName)
544
    {
545
        $this->updatePage->open(['id' => $customer->getId()]);
546
547
        Assert::same(
548
            $groupName,
549
            $this->updatePage->getGroupName(),
550
            sprintf('Customer should have %s as group, but it does not', $groupName)
551
        );
552
    }
553
}
554