Completed
Push — master ( 094334...0b6e60 )
by Kamil
17:47
created

iProceedSelectingShippingCountryAndShippingMethod()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 2
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\Checkout\CheckoutAddressingStep;
16
use Sylius\Behat\Page\Checkout\CheckoutFinalizeStep;
17
use Sylius\Behat\Page\Checkout\CheckoutPaymentStep;
18
use Sylius\Behat\Page\Checkout\CheckoutShippingStep;
19
use Sylius\Behat\Page\Checkout\CheckoutThankYouPage;
20
use Sylius\Component\Core\Model\UserInterface;
21
use Sylius\Component\Core\Test\Services\SharedStorageInterface;
22
23
/**
24
 * @author Arkadiusz Krakowiak <[email protected]>
25
 */
26
final class CheckoutContext implements Context
27
{
28
    /**
29
     * @var SharedStorageInterface
30
     */
31
    private $sharedStorage;
32
33
    /**
34
     * @var CheckoutAddressingStep
35
     */
36
    private $checkoutAddressingStep;
37
38
    /**
39
     * @var CheckoutShippingStep
40
     */
41
    private $checkoutShippingStep;
42
43
    /**
44
     * @var CheckoutPaymentStep
45
     */
46
    private $checkoutPaymentStep;
47
48
    /**
49
     * @var CheckoutFinalizeStep
50
     */
51
    private $checkoutFinalizeStep;
52
53
    /**
54
     * @var CheckoutThankYouPage
55
     */
56
    private $checkoutThankYouPage;
57
58
    /**
59
     * @param SharedStorageInterface $sharedStorage
60
     * @param CheckoutAddressingStep $checkoutAddressingStep
61
     * @param CheckoutShippingStep $checkoutShippingStep
62
     * @param CheckoutPaymentStep $checkoutPaymentStep
63
     * @param CheckoutFinalizeStep $checkoutFinalizeStep
64
     * @param CheckoutThankYouPage $checkoutThankYouPage
65
     */
66
    public function __construct(
67
        SharedStorageInterface $sharedStorage,
68
        CheckoutAddressingStep $checkoutAddressingStep,
69
        CheckoutShippingStep $checkoutShippingStep,
70
        CheckoutPaymentStep $checkoutPaymentStep,
71
        CheckoutFinalizeStep $checkoutFinalizeStep,
72
        CheckoutThankYouPage $checkoutThankYouPage
73
    ) {
74
        $this->sharedStorage = $sharedStorage;
75
        $this->checkoutAddressingStep = $checkoutAddressingStep;
76
        $this->checkoutShippingStep = $checkoutShippingStep;
77
        $this->checkoutPaymentStep = $checkoutPaymentStep;
78
        $this->checkoutFinalizeStep = $checkoutFinalizeStep;
79
        $this->checkoutThankYouPage = $checkoutThankYouPage;
80
    }
81
82
    /**
83
     * @When I proceed selecting :paymentMethodName payment method
84
     */
85
    public function iProceedSelectingOfflinePaymentMethod($paymentMethodName)
86
    {
87
        $this->proceedOrder(null, null, $paymentMethodName);
88
    }
89
90
    /**
91
     * @When /^I proceed selecting "([^"]+)" shipping method$/
92
     * @Given /^I chose "([^"]*)" shipping method$/
93
     */
94
    public function iProceedSelectingShippingMethod($shippingMethodName)
95
    {
96
        $this->proceedOrder(null, $shippingMethodName);
97
    }
98
99
    /**
100
     * @When /^I proceed selecting "([^"]*)" as shipping country with "([^"]*)" method$/
101
     */
102
    public function iProceedSelectingShippingCountryAndShippingMethod($shippingCountry, $shippingMethodName)
103
    {
104
        $this->proceedOrder($shippingCountry, $shippingMethodName);
105
    }
106
107
    /**
108
     * @When /^I proceed selecting "([^"]*)" as shipping country with "([^"]*)" payment method$/
109
     */
110
    public function iProceedSelectingShippingCountryAndPaymentMethod($shippingCountry, $paymentMethodName)
111
    {
112
        $this->proceedOrder($shippingCountry, null, $paymentMethodName);
113
    }
114
115
    /**
116
     * @When /^I change shipping method to "([^"]*)"$/
117
     */
118
    public function iChangeShippingMethod($shippingMethodName)
119
    {
120
        $this->checkoutShippingStep->open();
121
        $this->checkoutShippingStep->selectShippingMethod($shippingMethodName);
122
        $this->checkoutShippingStep->continueCheckout();
123
    }
124
125
    /**
126
     * @When I confirm my order
127
     */
128
    public function iConfirmMyOrder()
129
    {
130
        $this->checkoutFinalizeStep->confirmOrder();
131
    }
132
133
    /**
134
     * @Then I should see the thank you page
135
     */
136
    public function iShouldSeeTheThankYouPage()
137
    {
138
        /** @var UserInterface $user */
139
        $user = $this->sharedStorage->getCurrentResource('user');
140
        $customer = $user->getCustomer();
141
142
        expect($this->checkoutThankYouPage->hasThankYouMessageFor($customer->getFullName()))->toBe(true);
143
    }
144
145
    /**
146
     * @param string|null $shippingCountry
147
     * @param string|null $shippingMethodName
148
     * @param string|null $paymentMethodName
149
     */
150
    private function proceedOrder($shippingCountry = null, $shippingMethodName = null, $paymentMethodName = null)
151
    {
152
        $this->checkoutAddressingStep->open();
153
        $this->checkoutAddressingStep->fillAddressingDetails([
154
            'firstName' => 'John',
155
            'lastName' => 'Doe',
156
            'country' => $shippingCountry ?: 'France',
157
            'street' => '0635 Myron Hollow Apt. 711',
158
            'city' => 'North Bridget',
159
            'postcode' => '93-554',
160
            'phoneNumber' => '321123456',
161
        ]);
162
        $this->checkoutAddressingStep->continueCheckout();
163
164
        $this->checkoutShippingStep->selectShippingMethod($shippingMethodName ?: 'Free');
165
        $this->checkoutShippingStep->continueCheckout();
166
167
        $this->checkoutPaymentStep->selectPaymentMethod($paymentMethodName ?: 'Offline');
168
        $this->checkoutPaymentStep->continueCheckout();
169
    }
170
}
171