Completed
Push — master ( 284884...b70f30 )
by Kamil
20:25
created

CheckoutContext::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 2 Features 0
Metric Value
c 4
b 2
f 0
dl 0
loc 21
rs 9.3142
cc 1
eloc 19
nc 1
nop 9

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
namespace Sylius\Behat\Context\Ui;
13
14
use Behat\Behat\Context\Context;
15
use Sylius\Behat\Page\Shop\Order\OrderPaymentsPageInterface;
16
use Sylius\Behat\Page\Shop\Checkout\AddressingStepInterface;
17
use Sylius\Behat\Page\Shop\Checkout\FinalizeStepInterface;
18
use Sylius\Behat\Page\Shop\Checkout\PaymentStepInterface;
19
use Sylius\Behat\Page\Shop\Checkout\SecurityStepInterface;
20
use Sylius\Behat\Page\Shop\Checkout\ShippingStepInterface;
21
use Sylius\Behat\Page\Shop\Checkout\ThankYouPageInterface;
22
use Sylius\Component\Core\Model\OrderInterface;
23
use Sylius\Component\Core\Model\UserInterface;
24
use Sylius\Component\Core\Test\Services\SharedStorageInterface;
25
use Sylius\Component\Payment\Model\PaymentInterface;
26
use Sylius\Component\Resource\Repository\RepositoryInterface;
27
28
/**
29
 * @author Arkadiusz Krakowiak <[email protected]>
30
 */
31
final class CheckoutContext implements Context
32
{
33
    /**
34
     * @var SharedStorageInterface
35
     */
36
    private $sharedStorage;
37
38
    /**
39
     * @var SecurityStepInterface
40
     */
41
    private $checkoutSecurityStep;
42
43
    /**
44
     * @var AddressingStepInterface
45
     */
46
    private $checkoutAddressingStep;
47
48
    /**
49
     * @var ShippingStepInterface
50
     */
51
    private $checkoutShippingStep;
52
53
    /**
54
     * @var PaymentStepInterface
55
     */
56
    private $checkoutPaymentStep;
57
58
    /**
59
     * @var FinalizeStepInterface
60
     */
61
    private $checkoutFinalizeStep;
62
63
    /**
64
     * @var ThankYouPageInterface
65
     */
66
    private $checkoutThankYouPage;
67
68
    /**
69
     * @var OrderPaymentsPageInterface
70
     */
71
    private $orderPaymentsPage;
72
73
    /**
74
     * @var RepositoryInterface
75
     */
76
    private $orderRepository;
77
78
    /**
79
     * @param SharedStorageInterface $sharedStorage
80
     * @param SecurityStepInterface $checkoutSecurityStep
81
     * @param AddressingStepInterface $checkoutAddressingStep
82
     * @param ShippingStepInterface $checkoutShippingStep
83
     * @param PaymentStepInterface $checkoutPaymentStep
84
     * @param FinalizeStepInterface $checkoutFinalizeStep
85
     * @param ThankYouPageInterface $checkoutThankYouPage
86
     * @param OrderPaymentsPageInterface $orderPaymentsPage
87
     * @param RepositoryInterface $orderRepository
88
     */
89
    public function __construct(
90
        SharedStorageInterface $sharedStorage,
91
        SecurityStepInterface $checkoutSecurityStep,
92
        AddressingStepInterface $checkoutAddressingStep,
93
        ShippingStepInterface $checkoutShippingStep,
94
        PaymentStepInterface $checkoutPaymentStep,
95
        FinalizeStepInterface $checkoutFinalizeStep,
96
        ThankYouPageInterface $checkoutThankYouPage,
97
        OrderPaymentsPageInterface $orderPaymentsPage,
98
        RepositoryInterface $orderRepository
99
    ) {
100
        $this->sharedStorage = $sharedStorage;
101
        $this->checkoutSecurityStep = $checkoutSecurityStep;
102
        $this->checkoutAddressingStep = $checkoutAddressingStep;
103
        $this->checkoutShippingStep = $checkoutShippingStep;
104
        $this->checkoutPaymentStep = $checkoutPaymentStep;
105
        $this->checkoutFinalizeStep = $checkoutFinalizeStep;
106
        $this->checkoutThankYouPage = $checkoutThankYouPage;
107
        $this->orderPaymentsPage = $orderPaymentsPage;
108
        $this->orderRepository = $orderRepository;
109
    }
110
111
    /**
112
     * @Given /^I proceed without selecting shipping address$/
113
     */
114
    public function iProceedWithoutSelectingShippingAddress()
115
    {
116
        $this->checkoutAddressingStep->open();
117
        $this->checkoutAddressingStep->continueCheckout();
118
    }
119
120
    /**
121
     * @When /^I proceed selecting "([^"]*)" as shipping country$/
122
     */
123
    public function iProceedSelectingShippingCountry($shippingCountry)
124
    {
125
        $this->checkoutAddressingStep->open();
126
        $this->checkoutAddressingStep->fillAddressingDetails([
127
            'firstName' => 'John',
128
            'lastName' => 'Doe',
129
            'country' => $shippingCountry ?: 'France',
130
            'street' => '0635 Myron Hollow Apt. 711',
131
            'city' => 'North Bridget',
132
            'postcode' => '93-554',
133
            'phoneNumber' => '321123456',
134
        ]);
135
        $this->checkoutAddressingStep->continueCheckout();
136
    }
137
138
    /**
139
     * @When /^I proceed selecting "([^"]*)" as shipping country with "([^"]*)" method$/
140
     */
141
    public function iProceedSelectingShippingCountryAndShippingMethod($shippingCountry, $shippingMethodName)
142
    {
143
        $this->iProceedSelectingShippingCountry($shippingCountry);
144
145
        $this->checkoutShippingStep->selectShippingMethod($shippingMethodName ?: 'Free');
146
        $this->checkoutShippingStep->continueCheckout();
147
    }
148
149
    /**
150
     * @When /^I proceed selecting "([^"]+)" shipping method$/
151
     * @Given /^I chose "([^"]*)" shipping method$/
152
     */
153
    public function iProceedSelectingShippingMethod($shippingMethodName)
154
    {
155
        $this->iProceedSelectingShippingCountryAndShippingMethod(null, $shippingMethodName);
156
    }
157
158
    /**
159
     * @When /^I choose "([^"]*)" payment method$/
160
     */
161
    public function iChoosePaymentMethod($paymentMethodName)
162
    {
163
        $this->checkoutPaymentStep->verify([]);
164
        $this->checkoutPaymentStep->selectPaymentMethod($paymentMethodName ?: 'Offline');
165
        $this->checkoutPaymentStep->continueCheckout();
166
    }
167
168
    /**
169
     * @When /^I proceed selecting "([^"]*)" as shipping country with "([^"]*)" payment method$/
170
     */
171
    public function iProceedSelectingShippingCountryAndPaymentMethod($shippingCountry, $paymentMethodName)
172
    {
173
        $this->iProceedSelectingShippingCountryAndShippingMethod($shippingCountry, null);
174
175
        $this->iChoosePaymentMethod($paymentMethodName);
176
    }
177
178
    /**
179
     * @When I proceed selecting :paymentMethodName payment method
180
     */
181
    public function iProceedSelectingOfflinePaymentMethod($paymentMethodName)
182
    {
183
        $this->iProceedSelectingShippingCountryAndPaymentMethod(null, $paymentMethodName);
184
    }
185
186
    /**
187
     * @When /^I change shipping method to "([^"]*)"$/
188
     */
189
    public function iChangeShippingMethod($shippingMethodName)
190
    {
191
        $this->checkoutShippingStep->open();
192
        $this->checkoutShippingStep->selectShippingMethod($shippingMethodName);
193
        $this->checkoutShippingStep->continueCheckout();
194
    }
195
196
    /**
197
     * @Given /^I proceed logging as "([^"]*)" with "([^"]*)" password$/
198
     */
199
    public function iProceedLoggingAs($login, $password)
200
    {
201
        $this->checkoutSecurityStep->open();
202
        $this->checkoutSecurityStep->logInAsExistingUser($login, $password);
203
204
        $this->checkoutAddressingStep->continueCheckout();
205
    }
206
207
    /**
208
     * @When /^I proceed as guest "([^"]*)" with "([^"]*)" as shipping country$/
209
     */
210
    public function iProceedLoggingAsGuestWithAsShippingCountry($email, $shippingCountry)
211
    {
212
        $this->checkoutSecurityStep->open();
213
        $this->checkoutSecurityStep->proceedAsGuest($email);
214
215
        $this->iProceedSelectingShippingCountry($shippingCountry);
216
    }
217
218
    /**
219
     * @When I confirm my order
220
     */
221
    public function iConfirmMyOrder()
222
    {
223
        $this->checkoutFinalizeStep->confirmOrder();
224
    }
225
226
    /**
227
     * @Then I should see the thank you page
228
     */
229
    public function iShouldSeeTheThankYouPage()
230
    {
231
        /** @var UserInterface $user */
232
        $user = $this->sharedStorage->get('user');
233
        $customer = $user->getCustomer();
234
235
        expect($this->checkoutThankYouPage->hasThankYouMessageFor($customer->getFullName()))->toBe(true);
236
    }
237
238
    /**
239
     * @Then I should be redirected back to the thank you page
240
     */
241
    public function iShouldBeRedirectedBackToTheThankYouPage()
242
    {
243
        $this->checkoutThankYouPage->waitForResponse(5);
244
245
        expect($this->checkoutThankYouPage->isOpen())->toBe(true);
246
    }
247
248
    /**
249
     * @Then I should be redirected back to the order payment page
250
     */
251
    public function iShouldBeRedirectedBackToTheOrderPaymentPage()
252
    {
253
        $this->orderPaymentsPage->waitForResponse(5, ['number' => $this->getLastOrder()->getNumber()]);
254
255
        expect($this->orderPaymentsPage->isOpen(['number' => $this->getLastOrder()->getNumber()]))->toBe(true);
256
    }
257
258
    /**
259
     * @Then I should see two cancelled payments and new one ready to be paid
260
     */
261
    public function iShouldSeeTwoCancelledPaymentsAndNewOneReadyToBePaid()
262
    {
263
        expect($this->orderPaymentsPage->countPaymentWithSpecificState(PaymentInterface::STATE_CANCELLED))->toBe(2);
264
        expect($this->orderPaymentsPage->countPaymentWithSpecificState(PaymentInterface::STATE_NEW))->toBe(1);
265
    }
266
267
    /**
268
     * @return OrderInterface
269
     *
270
     * @throws \RuntimeException
271
     */
272
    private function getLastOrder()
273
    {
274
        $customer = $this->sharedStorage->get('user')->getCustomer();
275
        $orders = $this->orderRepository->findByCustomer($customer);
276
        $lastOrder = end($orders);
277
278
        if (false === $lastOrder) {
279
            throw new \RuntimeException(sprintf('There is no last order for %s', $customer->getFullName()));
280
        }
281
282
        return $lastOrder;
283
    }
284
}
285