Completed
Push — checkout-context-cleanup ( 3b30b9...1e6a42 )
by Kamil
18:57
created

CheckoutContext::iProceedSelectingShippingMethod()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
nc 1
cc 1
eloc 2
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;
13
14
use Behat\Behat\Context\Context;
15
use Sylius\Behat\Context\Ui\Shop\Checkout\AddressingContext;
16
use Sylius\Behat\Context\Ui\Shop\Checkout\PaymentContext;
17
use Sylius\Behat\Context\Ui\Shop\Checkout\ShippingContext;
18
use Sylius\Behat\Page\Shop\Checkout\AddressPageInterface;
19
use Sylius\Behat\Page\Shop\Checkout\CompletePageInterface;
20
use Sylius\Behat\Page\Shop\Checkout\SelectPaymentPageInterface;
21
use Sylius\Behat\Page\Shop\Checkout\SelectShippingPageInterface;
22
use Sylius\Behat\Page\UnexpectedPageException;
23
use Sylius\Behat\Service\Resolver\CurrentPageResolverInterface;
24
use Sylius\Behat\Service\SharedStorageInterface;
25
use Sylius\Component\Addressing\Model\CountryInterface;
26
use Webmozart\Assert\Assert;
27
28
/**
29
 * @author Arkadiusz Krakowiak <[email protected]>
30
 */
31
final class CheckoutContext implements Context
32
{
33
    /**
34
     * @var AddressPageInterface
35
     */
36
    private $addressPage;
37
38
    /**
39
     * @var SelectPaymentPageInterface
40
     */
41
    private $selectPaymentPage;
42
43
    /**
44
     * @var SelectShippingPageInterface
45
     */
46
    private $selectShippingPage;
47
48
    /**
49
     * @var CompletePageInterface
50
     */
51
    private $completePage;
52
53
    /**
54
     * @var CurrentPageResolverInterface
55
     */
56
    private $currentPageResolver;
57
58
    /**
59
     * @var AddressingContext
60
     */
61
    private $addressingContext;
62
63
    /**
64
     * @var ShippingContext
65
     */
66
    private $shippingContext;
67
68
    /**
69
     * @var PaymentContext
70
     */
71
    private $paymentContext;
72
73
    /**
74
     * @param AddressPageInterface $addressPage
75
     * @param SelectPaymentPageInterface $selectPaymentPage
76
     * @param SelectShippingPageInterface $selectShippingPage
77
     * @param CompletePageInterface $completePage
78
     * @param CurrentPageResolverInterface $currentPageResolver
79
     * @param AddressingContext $addressingContext
80
     * @param ShippingContext $shippingContext
81
     * @param PaymentContext $paymentContext
82
     */
83
    public function __construct(
84
        AddressPageInterface $addressPage,
85
        SelectPaymentPageInterface $selectPaymentPage,
86
        SelectShippingPageInterface $selectShippingPage,
87
        CompletePageInterface $completePage,
88
        CurrentPageResolverInterface $currentPageResolver,
89
        AddressingContext $addressingContext,
90
        ShippingContext $shippingContext,
91
        PaymentContext $paymentContext
92
    ) {
93
        $this->addressPage = $addressPage;
94
        $this->selectPaymentPage = $selectPaymentPage;
95
        $this->selectShippingPage = $selectShippingPage;
96
        $this->completePage = $completePage;
97
        $this->currentPageResolver = $currentPageResolver;
98
        $this->addressingContext = $addressingContext;
99
        $this->shippingContext = $shippingContext;
100
        $this->paymentContext = $paymentContext;
101
    }
102
103
    /**
104
     * @Given I was at the checkout summary step
105
     */
106
    public function iWasAtTheCheckoutSummaryStep()
107
    {
108
        $this->addressingContext->iSpecifiedTheShippingAddress();
109
        $this->iProceedOrderWithShippingMethodAndPayment('Free', 'Offline');
110
    }
111
112
    /**
113
     * @When I go to the addressing step
114
     */
115
    public function iGoToTheAddressingStep()
116
    {
117
        if ($this->selectShippingPage->isOpen()) {
118
            $this->selectShippingPage->changeAddressByStepLabel();
119
120
            return;
121
        }
122
123
        if ($this->selectPaymentPage->isOpen()) {
124
            $this->selectPaymentPage->changeAddressByStepLabel();
125
126
            return;
127
        }
128
129
        if ($this->completePage->isOpen()) {
130
            $this->completePage->changeAddress();
131
132
            return;
133
        }
134
135
        throw new UnexpectedPageException('It is impossible to go to addressing step from current page.');
136
    }
137
138
    /**
139
     * @When I go to the shipping step
140
     */
141
    public function iGoToTheShippingStep()
142
    {
143
        if ($this->selectPaymentPage->isOpen()) {
144
            $this->selectPaymentPage->changeShippingMethodByStepLabel();
145
146
            return;
147
        }
148
149
        if ($this->completePage->isOpen()) {
150
            $this->completePage->changeShippingMethod();
151
152
            return;
153
        }
154
155
        throw new UnexpectedPageException('It is impossible to go to shipping step from current page.');
156
    }
157
158
    /**
159
     * @When /^I proceed selecting ("[^"]+" as shipping country) with "([^"]+)" method$/
160
     */
161
    public function iProceedSelectingShippingCountryAndShippingMethod(CountryInterface $shippingCountry = null, $shippingMethodName = null)
162
    {
163
        $this->addressingContext->iProceedSelectingShippingCountry($shippingCountry);
164
165
        $this->shippingContext->iHaveProceededSelectingShippingMethod($shippingMethodName ?: 'Free');
166
    }
167
168
    /**
169
     * @When /^I proceed selecting "([^"]+)" shipping method$/
170
     * @Given /^I chose "([^"]*)" shipping method$/
171
     */
172
    public function iProceedSelectingShippingMethod($shippingMethodName)
173
    {
174
        $this->iProceedSelectingShippingCountryAndShippingMethod(null, $shippingMethodName);
175
    }
176
177
    /**
178
     * @Given I have proceeded selecting :paymentMethodName payment method
179
     * @When /^I (?:proceed|proceeded) selecting "([^"]+)" payment method$/
180
     */
181
    public function iProceedSelectingPaymentMethod($paymentMethodName = 'Offline')
182
    {
183
        $this->iProceedSelectingShippingCountryAndShippingMethod();
184
        $this->paymentContext->iChoosePaymentMethod($paymentMethodName);
185
    }
186
187
    /**
188
     * @When /^I change shipping method to "([^"]*)"$/
189
     */
190
    public function iChangeShippingMethod($shippingMethodName)
191
    {
192
        $this->paymentContext->iDecideToChangeMyShippingMethod();
193
        $this->shippingContext->iHaveProceededSelectingShippingMethod($shippingMethodName);
194
    }
195
196
    /**
197
     * @When /^I do not modify anything$/
198
     */
199
    public function iDoNotModifyAnything()
200
    {
201
        // Intentionally left blank to fulfill context expectation
202
    }
203
204
    /**
205
     * @Given I have proceeded order with :shippingMethod shipping method and :paymentMethod payment
206
     * @When I proceed with :shippingMethod shipping method and :paymentMethod payment
207
     */
208
    public function iProceedOrderWithShippingMethodAndPayment($shippingMethod, $paymentMethod)
209
    {
210
        $this->iProceedOrderWithShippingMethod($shippingMethod);
211
        $this->paymentContext->iChoosePaymentMethod($paymentMethod);
212
    }
213
214
    /**
215
     * @When I proceed with :shippingMethod shipping method
216
     */
217
    public function iProceedOrderWithShippingMethod($shippingMethod)
218
    {
219
        $this->shippingContext->iHaveProceededSelectingShippingMethod($shippingMethod);
220
    }
221
222
    /**
223
     * @Then the subtotal of :item item should be :price
224
     */
225
    public function theSubtotalOfItemShouldBe($item, $price)
226
    {
227
        $currentPage = $this->resolveCurrentStepPage();
228
        $actualPrice = $currentPage->getItemSubtotal($item);
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...
229
230
        Assert::eq($actualPrice, $price, sprintf('The %s subtotal should be %s, but is %s', $item, $price, $actualPrice));
231
    }
232
233
    /**
234
     * @return AddressPageInterface|SelectPaymentPageInterface|SelectShippingPageInterface|CompletePageInterface
235
     */
236
    private function resolveCurrentStepPage()
237
    {
238
        return $this->currentPageResolver->getCurrentPageWithForm([
239
            $this->addressPage,
240
            $this->selectPaymentPage,
241
            $this->selectShippingPage,
242
            $this->completePage,
243
        ]);
244
    }
245
}
246