Completed
Push — master ( 2f9340...094334 )
by Kamil
17:24
created

CheckoutContext::proceedOrder()   A

Complexity

Conditions 4
Paths 1

Size

Total Lines 20
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 20
rs 9.2
cc 4
eloc 15
nc 1
nop 3
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 change shipping method to "([^"]*)"$/
109
     */
110
    public function iChangeShippingMethod($shippingMethodName)
111
    {
112
        $this->checkoutShippingStep->open();
113
        $this->checkoutShippingStep->selectShippingMethod($shippingMethodName);
114
        $this->checkoutShippingStep->continueCheckout();
115
    }
116
117
    /**
118
     * @When I confirm my order
119
     */
120
    public function iConfirmMyOrder()
121
    {
122
        $this->checkoutFinalizeStep->confirmOrder();
123
    }
124
125
    /**
126
     * @Then I should see the thank you page
127
     */
128
    public function iShouldSeeTheThankYouPage()
129
    {
130
        /** @var UserInterface $user */
131
        $user = $this->sharedStorage->getCurrentResource('user');
132
        $customer = $user->getCustomer();
133
134
        expect($this->checkoutThankYouPage->hasThankYouMessageFor($customer->getFullName()))->toBe(true);
135
    }
136
137
    /**
138
     * @param string|null $shippingCountry
139
     * @param string|null $shippingMethodName
140
     * @param string|null $paymentMethodName
141
     */
142
    private function proceedOrder($shippingCountry = null, $shippingMethodName = null, $paymentMethodName = null)
143
    {
144
        $this->checkoutAddressingStep->open();
145
        $this->checkoutAddressingStep->fillAddressingDetails([
146
            'firstName' => 'John',
147
            'lastName' => 'Doe',
148
            'country' => $shippingCountry ?: 'France',
149
            'street' => '0635 Myron Hollow Apt. 711',
150
            'city' => 'North Bridget',
151
            'postcode' => '93-554',
152
            'phoneNumber' => '321123456',
153
        ]);
154
        $this->checkoutAddressingStep->continueCheckout();
155
156
        $this->checkoutShippingStep->selectShippingMethod($shippingMethodName ?: 'Free');
157
        $this->checkoutShippingStep->continueCheckout();
158
159
        $this->checkoutPaymentStep->selectPaymentMethod($paymentMethodName ?: 'Offline');
160
        $this->checkoutPaymentStep->continueCheckout();
161
    }
162
}
163