Completed
Push — 1.1-phpstan-level-2 ( 261c61 )
by Kamil
195:32 queued 171:07
created

AccountContext   A

Complexity

Total Complexity 42

Size/Duplication

Total Lines 421
Duplicated Lines 0 %

Coupling/Cohesion

Components 7
Dependencies 10

Importance

Changes 0
Metric Value
wmc 42
lcom 7
cbo 10
dl 0
loc 421
rs 9.0399
c 0
b 0
f 0

42 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 17 1
A iWantToModifyMyProfile() 0 4 1
A iSpecifyTheFirstName() 0 4 1
A iSpecifyTheLastName() 0 4 1
A iSpecifyCustomerTheEmail() 0 4 1
A iSaveMyChanges() 0 4 1
A iShouldBeNotifiedThatItHasBeenSuccessfullyEdited() 0 4 1
A myNameShouldBe() 0 6 1
A myEmailShouldBe() 0 6 1
A iShouldBeNotifiedThatElementIsRequired() 0 7 1
A iShouldBeNotifiedThatElementIsInvalid() 0 7 1
A iShouldBeNotifiedThatTheEmailIsAlreadyUsed() 0 4 1
A iWantToChangeMyPassword() 0 4 1
A iChangePasswordTo() 0 6 1
A iShouldBeNotifiedThatMyPasswordHasBeenSuccessfullyChanged() 0 4 1
A iSpecifyTheCurrentPasswordAs() 0 4 1
A iSpecifyTheNewPasswordAs() 0 4 1
A iSpecifyTheConfirmationPasswordAs() 0 4 1
A iShouldBeNotifiedThatProvidedPasswordIsDifferentThanTheCurrentOne() 0 7 1
A iShouldBeNotifiedThatTheEnteredPasswordsDoNotMatch() 0 7 1
A iShouldBeNotifiedThatThePasswordShouldBeAtLeastCharactersLong() 0 7 1
A iBrowseMyOrders() 0 4 1
A iShouldSeeASingleOrderInTheList() 0 4 1
A thisOrderShouldHaveNumber() 0 4 1
A iViewTheSummaryOfTheOrder() 0 4 1
A iViewingTheSummaryOfMyLastOrder() 0 5 1
A itShouldHasNumber() 0 4 1
A iShouldSeeAsShippingAddress() 0 4 1
A itShouldBeShippedTo() 0 4 1
A iShouldSeeAsOrderSTotal() 0 4 1
A iShouldSeeAsOrderSSubtotal() 0 4 1
A iShouldSeeIHaveToPayForThisOrder() 0 4 1
A iShouldSeeItemsInTheList() 0 4 1
A theProductShouldBeInTheItemsList() 0 4 1
A iShouldSeeAsItemPrice() 0 4 1
A iSubscribeToTheNewsletter() 0 4 1
A iShouldBeSubscribedToTheNewsletter() 0 4 1
A iShouldSeeAsProvinceInTheShippingAddress() 0 4 1
A iShouldSeeAsProvinceInTheBillingAddress() 0 4 1
A iShouldBeAbleToChangePaymentMethodForThisOrder() 0 4 1
A iShouldBeRedirectedToMyAccountDashboard() 0 4 1
A iWantToLogIn() 0 4 1

How to fix   Complexity   

Complex Class

Complex classes like AccountContext often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use AccountContext, and based on these observations, apply Extract Interface, too.

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\PageInterface;
19
use Sylius\Behat\Page\Shop\Account\ChangePasswordPageInterface;
20
use Sylius\Behat\Page\Shop\Account\DashboardPageInterface;
21
use Sylius\Behat\Page\Shop\Account\LoginPageInterface;
22
use Sylius\Behat\Page\Shop\Account\Order\IndexPageInterface;
23
use Sylius\Behat\Page\Shop\Account\Order\ShowPageInterface;
24
use Sylius\Behat\Page\Shop\Account\ProfileUpdatePageInterface;
25
use Sylius\Behat\Service\NotificationCheckerInterface;
26
use Sylius\Component\Core\Formatter\StringInflector;
27
use Sylius\Component\Core\Model\OrderInterface;
28
use Webmozart\Assert\Assert;
29
30
final class AccountContext implements Context
31
{
32
    /**
33
     * @var DashboardPageInterface
34
     */
35
    private $dashboardPage;
36
37
    /**
38
     * @var ProfileUpdatePageInterface
39
     */
40
    private $profileUpdatePage;
41
42
    /**
43
     * @var ChangePasswordPageInterface
44
     */
45
    private $changePasswordPage;
46
47
    /**
48
     * @var IndexPageInterface
49
     */
50
    private $orderIndexPage;
51
52
    /**
53
     * @var ShowPageInterface
54
     */
55
    private $orderShowPage;
56
57
    /**
58
     * @var LoginPageInterface
59
     */
60
    private $loginPage;
61
62
    /**
63
     * @var NotificationCheckerInterface
64
     */
65
    private $notificationChecker;
66
67
    /**
68
     * @param DashboardPageInterface $dashboardPage
69
     * @param ProfileUpdatePageInterface $profileUpdatePage
70
     * @param ChangePasswordPageInterface $changePasswordPage
71
     * @param IndexPageInterface $orderIndexPage
72
     * @param ShowPageInterface $orderShowPage
73
     * @param LoginPageInterface $loginPage
74
     * @param NotificationCheckerInterface $notificationChecker
75
     */
76
    public function __construct(
77
        DashboardPageInterface $dashboardPage,
78
        ProfileUpdatePageInterface $profileUpdatePage,
79
        ChangePasswordPageInterface $changePasswordPage,
80
        IndexPageInterface $orderIndexPage,
81
        ShowPageInterface $orderShowPage,
82
        LoginPageInterface $loginPage,
83
        NotificationCheckerInterface $notificationChecker
84
    ) {
85
        $this->dashboardPage = $dashboardPage;
86
        $this->profileUpdatePage = $profileUpdatePage;
87
        $this->changePasswordPage = $changePasswordPage;
88
        $this->orderIndexPage = $orderIndexPage;
89
        $this->orderShowPage = $orderShowPage;
90
        $this->loginPage = $loginPage;
91
        $this->notificationChecker = $notificationChecker;
92
    }
93
94
    /**
95
     * @When I want to modify my profile
96
     */
97
    public function iWantToModifyMyProfile()
98
    {
99
        $this->profileUpdatePage->open();
100
    }
101
102
    /**
103
     * @When I specify the first name as :firstName
104
     * @When I remove the first name
105
     */
106
    public function iSpecifyTheFirstName($firstName = null)
107
    {
108
        $this->profileUpdatePage->specifyFirstName($firstName);
109
    }
110
111
    /**
112
     * @When I specify the last name as :lastName
113
     * @When I remove the last name
114
     */
115
    public function iSpecifyTheLastName($lastName = null)
116
    {
117
        $this->profileUpdatePage->specifyLastName($lastName);
118
    }
119
120
    /**
121
     * @When I specify the customer email as :email
122
     * @When I remove the customer email
123
     */
124
    public function iSpecifyCustomerTheEmail($email = null)
125
    {
126
        $this->profileUpdatePage->specifyEmail($email);
127
    }
128
129
    /**
130
     * @When I save my changes
131
     * @When I try to save my changes
132
     */
133
    public function iSaveMyChanges()
134
    {
135
        $this->profileUpdatePage->saveChanges();
136
    }
137
138
    /**
139
     * @Then I should be notified that it has been successfully edited
140
     */
141
    public function iShouldBeNotifiedThatItHasBeenSuccessfullyEdited()
142
    {
143
        $this->notificationChecker->checkNotification('has been successfully updated.', NotificationType::success());
144
    }
145
146
    /**
147
     * @Then my name should be :name
148
     * @Then my name should still be :name
149
     */
150
    public function myNameShouldBe($name)
151
    {
152
        $this->dashboardPage->open();
153
154
        Assert::true($this->dashboardPage->hasCustomerName($name));
155
    }
156
157
    /**
158
     * @Then my email should be :email
159
     * @Then my email should still be :email
160
     */
161
    public function myEmailShouldBe($email)
162
    {
163
        $this->dashboardPage->open();
164
165
        Assert::true($this->dashboardPage->hasCustomerEmail($email));
166
    }
167
168
    /**
169
     * @Then /^I should be notified that the (email|password|city|street|first name|last name) is required$/
170
     */
171
    public function iShouldBeNotifiedThatElementIsRequired($element)
172
    {
173
        Assert::true($this->profileUpdatePage->checkValidationMessageFor(
174
            StringInflector::nameToCode($element),
175
            sprintf('Please enter your %s.', $element)
176
        ));
177
    }
178
179
    /**
180
     * @Then /^I should be notified that the (email) is invalid$/
181
     */
182
    public function iShouldBeNotifiedThatElementIsInvalid($element)
183
    {
184
        Assert::true($this->profileUpdatePage->checkValidationMessageFor(
185
            StringInflector::nameToCode($element),
186
            sprintf('This %s is invalid.', $element)
187
        ));
188
    }
189
190
    /**
191
     * @Then I should be notified that the email is already used
192
     */
193
    public function iShouldBeNotifiedThatTheEmailIsAlreadyUsed()
194
    {
195
        Assert::true($this->profileUpdatePage->checkValidationMessageFor('email', 'This email is already used.'));
196
    }
197
198
    /**
199
     * @Given /^I want to change my password$/
200
     */
201
    public function iWantToChangeMyPassword()
202
    {
203
        $this->changePasswordPage->open();
204
    }
205
206
    /**
207
     * @Given I change password from :oldPassword to :newPassword
208
     */
209
    public function iChangePasswordTo($oldPassword, $newPassword)
210
    {
211
        $this->iSpecifyTheCurrentPasswordAs($oldPassword);
212
        $this->iSpecifyTheNewPasswordAs($newPassword);
213
        $this->iSpecifyTheConfirmationPasswordAs($newPassword);
214
    }
215
216
    /**
217
     * @Then I should be notified that my password has been successfully changed
218
     */
219
    public function iShouldBeNotifiedThatMyPasswordHasBeenSuccessfullyChanged()
220
    {
221
        $this->notificationChecker->checkNotification('has been changed successfully!', NotificationType::success());
222
    }
223
224
    /**
225
     * @Given I specify the current password as :password
226
     */
227
    public function iSpecifyTheCurrentPasswordAs($password)
228
    {
229
        $this->changePasswordPage->specifyCurrentPassword($password);
230
    }
231
232
    /**
233
     * @Given I specify the new password as :password
234
     */
235
    public function iSpecifyTheNewPasswordAs($password)
236
    {
237
        $this->changePasswordPage->specifyNewPassword($password);
238
    }
239
240
    /**
241
     * @Given I confirm this password as :password
242
     */
243
    public function iSpecifyTheConfirmationPasswordAs($password)
244
    {
245
        $this->changePasswordPage->specifyConfirmationPassword($password);
246
    }
247
248
    /**
249
     * @Then I should be notified that provided password is different than the current one
250
     */
251
    public function iShouldBeNotifiedThatProvidedPasswordIsDifferentThanTheCurrentOne()
252
    {
253
        Assert::true($this->changePasswordPage->checkValidationMessageFor(
254
            'current_password',
255
            'Provided password is different than the current one.'
256
        ));
257
    }
258
259
    /**
260
     * @Then I should be notified that the entered passwords do not match
261
     */
262
    public function iShouldBeNotifiedThatTheEnteredPasswordsDoNotMatch()
263
    {
264
        Assert::true($this->changePasswordPage->checkValidationMessageFor(
265
            'new_password',
266
            'The entered passwords don\'t match'
267
        ));
268
    }
269
270
    /**
271
     * @Then I should be notified that the password should be at least 4 characters long
272
     */
273
    public function iShouldBeNotifiedThatThePasswordShouldBeAtLeastCharactersLong()
274
    {
275
        Assert::true($this->changePasswordPage->checkValidationMessageFor(
276
            'new_password',
277
            'Password must be at least 4 characters long.'
278
        ));
279
    }
280
281
    /**
282
     * @When I browse my orders
283
     */
284
    public function iBrowseMyOrders()
285
    {
286
        $this->orderIndexPage->open();
287
    }
288
289
    /**
290
     * @Then I should see a single order in the list
291
     */
292
    public function iShouldSeeASingleOrderInTheList()
293
    {
294
        Assert::same($this->orderIndexPage->countOrders(), 1);
295
    }
296
297
    /**
298
     * @Then this order should have :order number
299
     */
300
    public function thisOrderShouldHaveNumber(OrderInterface $order)
301
    {
302
        Assert::true($this->orderIndexPage->isOrderWithNumberInTheList($order->getNumber()));
303
    }
304
305
    /**
306
     * @When I view the summary of the order :order
307
     */
308
    public function iViewTheSummaryOfTheOrder(OrderInterface $order)
309
    {
310
        $this->orderShowPage->open(['number' => $order->getNumber()]);
311
    }
312
313
    /**
314
     * @When I am viewing the summary of my last order
315
     */
316
    public function iViewingTheSummaryOfMyLastOrder()
317
    {
318
        $this->orderIndexPage->open();
319
        $this->orderIndexPage->openLastOrderPage();
320
    }
321
322
    /**
323
     * @Then it should has number :orderNumber
324
     */
325
    public function itShouldHasNumber($orderNumber)
326
    {
327
        Assert::same($this->orderShowPage->getNumber(), $orderNumber);
328
    }
329
330
    /**
331
     * @Then I should see :customerName, :street, :postcode, :city, :countryName as shipping address
332
     */
333
    public function iShouldSeeAsShippingAddress($customerName, $street, $postcode, $city, $countryName)
334
    {
335
        Assert::true($this->orderShowPage->hasShippingAddress($customerName, $street, $postcode, $city, $countryName));
336
    }
337
338
    /**
339
     * @Then I should see :customerName, :street, :postcode, :city, :countryName as billing address
340
     */
341
    public function itShouldBeShippedTo($customerName, $street, $postcode, $city, $countryName)
342
    {
343
        Assert::true($this->orderShowPage->hasBillingAddress($customerName, $street, $postcode, $city, $countryName));
344
    }
345
346
    /**
347
     * @Then I should see :total as order's total
348
     */
349
    public function iShouldSeeAsOrderSTotal($total)
350
    {
351
        Assert::same($this->orderShowPage->getTotal(), $total);
352
    }
353
354
    /**
355
     * @Then I should see :itemsTotal as order's subtotal
356
     */
357
    public function iShouldSeeAsOrderSSubtotal($subtotal)
358
    {
359
        Assert::same($this->orderShowPage->getSubtotal(), $subtotal);
360
    }
361
362
    /**
363
     * @Then I should see that I have to pay :paymentAmount for this order
364
     * @Then I should see :paymentTotal as payment total
365
     */
366
    public function iShouldSeeIHaveToPayForThisOrder($paymentAmount)
367
    {
368
        Assert::same($this->orderShowPage->getPaymentPrice(), $paymentAmount);
369
    }
370
371
    /**
372
     * @Then I should see :numberOfItems items in the list
373
     */
374
    public function iShouldSeeItemsInTheList($numberOfItems)
375
    {
376
        Assert::same($this->orderShowPage->countItems(), (int) $numberOfItems);
377
    }
378
379
    /**
380
     * @Then the product named :productName should be in the items list
381
     */
382
    public function theProductShouldBeInTheItemsList($productName)
383
    {
384
        Assert::true($this->orderShowPage->isProductInTheList($productName));
385
    }
386
387
    /**
388
     * @Then I should see :itemPrice as item price
389
     */
390
    public function iShouldSeeAsItemPrice($itemPrice)
391
    {
392
        Assert::same($this->orderShowPage->getItemPrice(), $itemPrice);
393
    }
394
395
    /**
396
     * @When I subscribe to the newsletter
397
     */
398
    public function iSubscribeToTheNewsletter()
399
    {
400
        $this->profileUpdatePage->subscribeToTheNewsletter();
401
    }
402
403
    /**
404
     * @Then I should be subscribed to the newsletter
405
     */
406
    public function iShouldBeSubscribedToTheNewsletter()
407
    {
408
        Assert::true($this->profileUpdatePage->isSubscribedToTheNewsletter());
409
    }
410
411
    /**
412
     * @Then I should see :provinceName as province in the shipping address
413
     */
414
    public function iShouldSeeAsProvinceInTheShippingAddress($provinceName)
415
    {
416
        Assert::true($this->orderShowPage->hasShippingProvinceName($provinceName));
417
    }
418
419
    /**
420
     * @Then I should see :provinceName as province in the billing address
421
     */
422
    public function iShouldSeeAsProvinceInTheBillingAddress($provinceName)
423
    {
424
        Assert::true($this->orderShowPage->hasBillingProvinceName($provinceName));
425
    }
426
427
    /**
428
     * @Then /^I should be able to change payment method for (this order)$/
429
     */
430
    public function iShouldBeAbleToChangePaymentMethodForThisOrder(OrderInterface $order)
431
    {
432
        Assert::true($this->orderIndexPage->isItPossibleToChangePaymentMethodForOrder($order));
433
    }
434
435
    /**
436
     * @Then I should be redirected to my account dashboard
437
     */
438
    public function iShouldBeRedirectedToMyAccountDashboard()
439
    {
440
        Assert::true($this->dashboardPage->isOpen(), 'User should be on the account panel dashboard page but they are not.');
441
    }
442
443
    /**
444
     * @When I want to log in
445
     */
446
    public function iWantToLogIn()
447
    {
448
        $this->loginPage->tryToOpen();
449
    }
450
}
451