Completed
Push — move-guzzle ( fd46e9 )
by Kamil
21:01
created

CheckoutContext::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 9.552
c 0
b 0
f 0
cc 1
nc 1
nop 10

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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 FriendsOfBehat\PageObjectExtension\Page\UnexpectedPageException;
18
use Sylius\Behat\Context\Ui\Shop\Checkout\CheckoutAddressingContext;
19
use Sylius\Behat\Context\Ui\Shop\Checkout\CheckoutPaymentContext;
20
use Sylius\Behat\Context\Ui\Shop\Checkout\CheckoutShippingContext;
21
use Sylius\Behat\Element\Shop\Account\RegisterElementInterface;
22
use Sylius\Behat\Page\Shop\Account\RegisterPageInterface;
23
use Sylius\Behat\Page\Shop\Checkout\AddressPageInterface;
24
use Sylius\Behat\Page\Shop\Checkout\CompletePageInterface;
25
use Sylius\Behat\Page\Shop\Checkout\SelectPaymentPageInterface;
26
use Sylius\Behat\Page\Shop\Checkout\SelectShippingPageInterface;
27
use Sylius\Behat\Service\Resolver\CurrentPageResolverInterface;
28
use Sylius\Component\Addressing\Model\CountryInterface;
29
use Webmozart\Assert\Assert;
30
31
final class CheckoutContext implements Context
32
{
33
    /** @var AddressPageInterface */
34
    private $addressPage;
35
36
    /** @var SelectPaymentPageInterface */
37
    private $selectPaymentPage;
38
39
    /** @var SelectShippingPageInterface */
40
    private $selectShippingPage;
41
42
    /** @var CompletePageInterface */
43
    private $completePage;
44
45
    /** @var RegisterPageInterface */
46
    private $registerPage;
47
48
    /** @var RegisterElementInterface */
49
    private $registerElement;
50
51
    /** @var CurrentPageResolverInterface */
52
    private $currentPageResolver;
53
54
    /** @var CheckoutAddressingContext */
55
    private $addressingContext;
56
57
    /** @var CheckoutShippingContext */
58
    private $shippingContext;
59
60
    /** @var CheckoutPaymentContext */
61
    private $paymentContext;
62
63
    public function __construct(
64
        AddressPageInterface $addressPage,
65
        SelectPaymentPageInterface $selectPaymentPage,
66
        SelectShippingPageInterface $selectShippingPage,
67
        CompletePageInterface $completePage,
68
        RegisterPageInterface $registerPage,
69
        RegisterElementInterface $registerElement,
70
        CurrentPageResolverInterface $currentPageResolver,
71
        CheckoutAddressingContext $addressingContext,
72
        CheckoutShippingContext $shippingContext,
73
        CheckoutPaymentContext $paymentContext
74
    ) {
75
        $this->addressPage = $addressPage;
76
        $this->selectPaymentPage = $selectPaymentPage;
77
        $this->selectShippingPage = $selectShippingPage;
78
        $this->completePage = $completePage;
79
        $this->registerPage = $registerPage;
80
        $this->registerElement = $registerElement;
81
        $this->currentPageResolver = $currentPageResolver;
82
        $this->addressingContext = $addressingContext;
83
        $this->shippingContext = $shippingContext;
84
        $this->paymentContext = $paymentContext;
85
    }
86
87
    /**
88
     * @Given I was at the checkout summary step
89
     */
90
    public function iWasAtTheCheckoutSummaryStep()
91
    {
92
        $this->addressingContext->iSpecifiedTheBillingAddress();
93
        $this->iProceedOrderWithShippingMethodAndPayment('Free', 'Offline');
94
    }
95
96
    /**
97
     * @Given I chose :shippingMethodName shipping method
98
     * @When I proceed selecting :shippingMethodName shipping method
99
     */
100
    public function iProceedSelectingShippingMethod($shippingMethodName)
101
    {
102
        $this->iProceedSelectingBillingCountryAndShippingMethod(null, $shippingMethodName);
103
    }
104
105
    /**
106
     * @Given I have proceeded selecting :paymentMethodName payment method
107
     * @When I proceed selecting :paymentMethodName payment method
108
     */
109
    public function iProceedSelectingPaymentMethod($paymentMethodName)
110
    {
111
        $this->iProceedSelectingBillingCountryAndShippingMethod();
112
        $this->paymentContext->iChoosePaymentMethod($paymentMethodName);
113
    }
114
115
    /**
116
     * @Given I have proceeded order with :shippingMethodName shipping method and :paymentMethodName payment
117
     * @When I proceed with :shippingMethodName shipping method and :paymentMethodName payment
118
     */
119
    public function iProceedOrderWithShippingMethodAndPayment($shippingMethodName, $paymentMethodName)
120
    {
121
        $this->shippingContext->iHaveProceededSelectingShippingMethod($shippingMethodName);
122
        $this->paymentContext->iChoosePaymentMethod($paymentMethodName);
123
    }
124
125
    /**
126
     * @Given I have proceeded through checkout process in the :localeCode locale with email :email
127
     * @When I proceed through checkout process
128
     * @When I proceed through checkout process in the :localeCode locale
129
     * @When I proceed through checkout process in the :localeCode locale with email :email
130
     */
131
    public function iProceedThroughCheckoutProcess(string $localeCode = 'en_US', ?string $email = null): void
132
    {
133
        $this->addressingContext->iProceedSelectingBillingCountry(null, $localeCode, $email);
134
        $this->shippingContext->iCompleteTheShippingStep();
135
        $this->paymentContext->iCompleteThePaymentStep();
136
    }
137
138
    /**
139
     * @When /^I proceed selecting ("[^"]+" as billing country) with "([^"]+)" method$/
140
     */
141
    public function iProceedSelectingBillingCountryAndShippingMethod(CountryInterface $shippingCountry = null, $shippingMethodName = null)
142
    {
143
        $this->addressingContext->iProceedSelectingBillingCountry($shippingCountry);
144
        $this->shippingContext->iHaveProceededSelectingShippingMethod($shippingMethodName ?: 'Free');
145
    }
146
147
    /**
148
     * @When /^I change shipping method to "([^"]*)"$/
149
     */
150
    public function iChangeShippingMethod($shippingMethodName)
151
    {
152
        $this->paymentContext->iDecideToChangeMyShippingMethod();
153
        $this->shippingContext->iHaveProceededSelectingShippingMethod($shippingMethodName);
154
    }
155
156
    /**
157
     * @When I go to the addressing step
158
     */
159
    public function iGoToTheAddressingStep()
160
    {
161
        if ($this->selectShippingPage->isOpen()) {
162
            $this->selectShippingPage->changeAddressByStepLabel();
163
164
            return;
165
        }
166
167
        if ($this->selectPaymentPage->isOpen()) {
168
            $this->selectPaymentPage->changeAddressByStepLabel();
169
170
            return;
171
        }
172
173
        if ($this->completePage->isOpen()) {
174
            $this->completePage->changeAddress();
175
176
            return;
177
        }
178
179
        throw new UnexpectedPageException('It is impossible to go to addressing step from current page.');
180
    }
181
182
    /**
183
     * @When I go to the shipping step
184
     */
185
    public function iGoToTheShippingStep()
186
    {
187
        if ($this->selectPaymentPage->isOpen()) {
188
            $this->selectPaymentPage->changeShippingMethodByStepLabel();
189
190
            return;
191
        }
192
193
        if ($this->completePage->isOpen()) {
194
            $this->completePage->changeShippingMethod();
195
196
            return;
197
        }
198
199
        throw new UnexpectedPageException('It is impossible to go to shipping step from current page.');
200
    }
201
202
    /**
203
     * @Then the subtotal of :item item should be :price
204
     */
205
    public function theSubtotalOfItemShouldBe($item, $price)
206
    {
207
        /** @var AddressPageInterface|SelectPaymentPageInterface|SelectShippingPageInterface|CompletePageInterface $currentPage */
208
        $currentPage = $this->currentPageResolver->getCurrentPageWithForm([
209
            $this->addressPage,
210
            $this->selectPaymentPage,
211
            $this->selectShippingPage,
212
            $this->completePage,
213
        ]);
214
215
        Assert::eq($currentPage->getItemSubtotal($item), $price);
0 ignored issues
show
Bug introduced by
The method getItemSubtotal does only exist in Sylius\Behat\Page\Shop\C...ctShippingPageInterface, but not in Sylius\Behat\Page\Shop\C...t\CompletePageInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
216
    }
217
218
    /**
219
     * @When I register with previously used :email email and :password password
220
     */
221
    public function iRegisterWithPreviouslyUsedEmailAndPassword(string $email, string $password): void
222
    {
223
        $this->registerPage->open();
224
        $this->registerElement->specifyEmail($email);
225
        $this->registerElement->specifyPassword($password);
226
        $this->registerElement->verifyPassword($password);
227
        $this->registerElement->specifyFirstName('Carrot');
228
        $this->registerElement->specifyLastName('Ironfoundersson');
229
        $this->registerElement->register();
230
    }
231
}
232