Completed
Push — master ( 61b8cb...f400ca )
by Kamil
08:30
created

getSelectedShippingMethodName()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 3
nop 0
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\Page\Shop\Checkout;
15
16
use Behat\Mink\Driver\Selenium2Driver;
17
use Behat\Mink\Element\NodeElement;
18
use Behat\Mink\Exception\ElementNotFoundException;
19
use Sylius\Behat\Page\SymfonyPage;
20
21
class SelectShippingPage extends SymfonyPage implements SelectShippingPageInterface
22
{
23
    /**
24
     * {@inheritdoc}
25
     */
26
    public function getRouteName()
27
    {
28
        return 'sylius_shop_checkout_select_shipping';
29
    }
30
31
    /**
32
     * {@inheritdoc}
33
     */
34
    public function selectShippingMethod($shippingMethod)
35
    {
36
        if ($this->getDriver() instanceof Selenium2Driver) {
37
            $this->getElement('shipping_method_select', ['%shipping_method%' => $shippingMethod])->click();
38
39
            return;
40
        }
41
42
        $shippingMethodOptionElement = $this->getElement('shipping_method_option', ['%shipping_method%' => $shippingMethod]);
43
        $shippingMethodOptionElement->selectOption($shippingMethodOptionElement->getAttribute('value'));
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49
    public function getShippingMethods()
50
    {
51
        $inputs = $this->getSession()->getPage()->findAll('css', '#sylius-shipping-methods .item .content label');
52
53
        $shippingMethods = [];
54
        foreach ($inputs as $input) {
55
            $shippingMethods[] = trim($input->getText());
56
        }
57
58
        return $shippingMethods;
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64
    public function getSelectedShippingMethodName(): ?string
65
    {
66
        $shippingMethods = $this->getSession()->getPage()->findAll('css', '#sylius-shipping-methods .item');
67
68
        /** @var NodeElement $shippingMethod */
69
        foreach ($shippingMethods as $shippingMethod) {
70
            if (null !== $shippingMethod->find('css', 'input:checked')) {
71
                return $shippingMethod->find('css', '.content label')->getText();
72
            }
73
        }
74
75
        return null;
76
    }
77
78
79
    /**
80
     * {@inheritdoc}
81
     */
82
    public function hasNoShippingMethodsMessage()
83
    {
84
        try {
85
            $this->getElement('order_cannot_be_shipped_message');
86
        } catch (ElementNotFoundException $exception) {
87
            return false;
88
        }
89
90
        return true;
91
    }
92
93
    /**
94
     * {@inheritdoc}
95
     */
96
    public function hasShippingMethodFee($shippingMethodName, $fee)
97
    {
98
        $feeElement = $this->getElement('shipping_method_fee', ['%shipping_method%' => $shippingMethodName])->getText();
99
100
        return false !== strpos($feeElement, $fee);
101
    }
102
103
    /**
104
     * {@inheritdoc}
105
     */
106
    public function getItemSubtotal($itemName)
107
    {
108
        $itemSlug = strtolower(str_replace('\"', '', str_replace(' ', '-', $itemName)));
109
110
        $subtotalTable = $this->getElement('checkout_subtotal');
111
112
        return $subtotalTable->find('css', sprintf('#sylius-item-%s-subtotal', $itemSlug))->getText();
113
    }
114
115
    public function nextStep()
116
    {
117
        $this->getElement('next_step')->press();
118
    }
119
120
    public function changeAddress()
121
    {
122
        $this->getDocument()->clickLink('Change address');
123
    }
124
125
    public function changeAddressByStepLabel()
126
    {
127
        $this->getElement('address')->click();
128
    }
129
130
    /**
131
     * {@inheritdoc}
132
     */
133
    public function getPurchaserEmail()
134
    {
135
        return $this->getElement('purchaser-email')->getText();
136
    }
137
138
    /**
139
     * {@inheritdoc}
140
     */
141
    public function getValidationMessageForShipment()
142
    {
143
        $foundElement = $this->getElement('shipment');
144
        if (null === $foundElement) {
145
            throw new ElementNotFoundException($this->getSession(), 'Items element');
146
        }
147
148
        $validationMessage = $foundElement->find('css', '.sylius-validation-error');
149
        if (null === $validationMessage) {
150
            throw new ElementNotFoundException($this->getSession(), 'Validation message', 'css', '.sylius-validation-error');
151
        }
152
153
        return $validationMessage->getText();
154
    }
155
156
    /**
157
     * {@inheritdoc}
158
     */
159
    public function hasNoAvailableShippingMethodsWarning()
160
    {
161
        return $this->hasElement('warning_no_shipping_methods');
162
    }
163
164
    /**
165
     * {@inheritdoc}
166
     */
167
    public function isNextStepButtonUnavailable()
168
    {
169
        return $this->getElement('next_step')->hasClass('disabled');
170
    }
171
172
    /**
173
     * {@inheritdoc}
174
     */
175
    public function hasShippingMethod($shippingMethodName)
176
    {
177
        $inputs = $this->getSession()->getPage()->findAll('css', '#sylius-shipping-methods .item .content label');
178
179
        $shippingMethods = [];
180
        foreach ($inputs as $input) {
181
            $shippingMethods[] = trim($input->getText());
182
        }
183
184
        return in_array($shippingMethodName, $shippingMethods);
185
    }
186
187
    /**
188
     * {@inheritdoc}
189
     */
190
    protected function getDefinedElements()
191
    {
192
        return array_merge(parent::getDefinedElements(), [
193
            'address' => '.steps a:contains("Address")',
194
            'checkout_subtotal' => '#sylius-checkout-subtotal',
195
            'next_step' => '#next-step',
196
            'order_cannot_be_shipped_message' => '#sylius-order-cannot-be-shipped',
197
            'purchaser-email' => '#purchaser-email',
198
            'shipment' => '.items',
199
            'shipping_method' => '[name="sylius_checkout_select_shipping[shipments][0][method]"]',
200
            'shipping_method_fee' => '.item:contains("%shipping_method%") .fee',
201
            'shipping_method_select' => '.item:contains("%shipping_method%") > .field > .ui.radio.checkbox',
202
            'shipping_method_option' => '.item:contains("%shipping_method%") input',
203
            'warning_no_shipping_methods' => '#sylius-order-cannot-be-shipped',
204
        ]);
205
    }
206
}
207