Completed
Push — checkout-consistent-prices ( af49ca )
by Kamil
13:24
created

SummaryPage::getTaxTotal()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
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\Cart;
15
16
use Behat\Mink\Exception\ElementNotFoundException;
17
use FriendsOfBehat\PageObjectExtension\Page\SymfonyPage;
18
use Sylius\Component\Core\Model\ProductInterface;
19
20
class SummaryPage extends SymfonyPage implements SummaryPageInterface
21
{
22
    /**
23
     * {@inheritdoc}
24
     */
25
    public function getRouteName(): string
26
    {
27
        return 'sylius_shop_cart_summary';
28
    }
29
30
    /**
31
     * {@inheritdoc}
32
     */
33
    public function getGrandTotal()
34
    {
35
        $totalElement = $this->getElement('grand_total');
36
37
        return $totalElement->getText();
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    public function getBaseGrandTotal()
44
    {
45
        $totalElement = $this->getElement('base_grand_total');
46
47
        return $totalElement->getText();
48
    }
49
50
    public function getIncludedTaxTotal(): string
51
    {
52
        $includedTaxTotalElement = $this->getElement('tax_included');
53
54
        return $includedTaxTotalElement->getText();
55
    }
56
57
    public function getExcludedTaxTotal(): string
58
    {
59
        $excludedTaxTotalElement = $this->getElement('tax_excluded');
60
61
        return $excludedTaxTotalElement->getText();
62
    }
63
64
    public function areTaxesCharged(): bool
65
    {
66
        try {
67
            $this->getElement('no_taxes');
68
        } catch (ElementNotFoundException $exception) {
69
            return true;
70
        }
71
72
        return false;
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78
    public function getShippingTotal()
79
    {
80
        $shippingTotalElement = $this->getElement('shipping_total');
81
82
        return $shippingTotalElement->getText();
83
    }
84
85
    /**
86
     * {@inheritdoc}
87
     */
88
    public function getPromotionTotal()
89
    {
90
        $shippingTotalElement = $this->getElement('promotion_total');
91
92
        return $shippingTotalElement->getText();
93
    }
94
95
    /**
96
     * {@inheritdoc}
97
     */
98
    public function getItemTotal($productName)
99
    {
100
        $itemTotalElement = $this->getElement('product_total', ['%name%' => $productName]);
101
102
        return $itemTotalElement->getText();
103
    }
104
105
    /**
106
     * {@inheritdoc}
107
     */
108
    public function getItemUnitRegularPrice($productName)
109
    {
110
        $regularUnitPrice = $this->getElement('product_unit_regular_price', ['%name%' => $productName]);
111
112
        return $this->getPriceFromString(trim($regularUnitPrice->getText()));
113
    }
114
115
    /**
116
     * {@inheritdoc}
117
     */
118
    public function getItemUnitPrice($productName)
119
    {
120
        $unitPrice = $this->getElement('product_unit_price', ['%name%' => $productName]);
121
122
        return $this->getPriceFromString(trim($unitPrice->getText()));
123
    }
124
125
    public function getItemImage(int $itemNumber): string
126
    {
127
        $itemImage = $this->getElement('item_image', ['%number%' => $itemNumber]);
128
129
        return $itemImage->getAttribute('src');
130
    }
131
132
    /**
133
     * {@inheritdoc}
134
     */
135
    public function isItemDiscounted($productName)
136
    {
137
        return $this->hasElement('product_unit_regular_price', ['%name%' => $productName]);
138
    }
139
140
    /**
141
     * {@inheritdoc}
142
     */
143
    public function removeProduct($productName)
144
    {
145
        $itemElement = $this->getElement('product_row', ['%name%' => $productName]);
146
        $itemElement->find('css', 'button.sylius-cart-remove-button')->press();
147
    }
148
149
    /**
150
     * {@inheritdoc}
151
     */
152
    public function applyCoupon($couponCode)
153
    {
154
        $this->getElement('coupon_field')->setValue($couponCode);
155
        $this->getElement('apply_coupon_button')->press();
156
    }
157
158
    /**
159
     * {@inheritdoc}
160
     */
161
    public function changeQuantity($productName, $quantity)
162
    {
163
        $itemElement = $this->getElement('product_row', ['%name%' => $productName]);
164
        $itemElement->find('css', 'input[type=number]')->setValue($quantity);
165
166
        $this->getElement('save_button')->click();
167
    }
168
169
    /**
170
     * {@inheritdoc}
171
     */
172
    public function isSingleItemOnPage()
173
    {
174
        $items = $this->getElement('cart_items')->findAll('css', 'tbody > tr');
175
176
        return 1 === count($items);
177
    }
178
179
    /**
180
     * {@inheritdoc}
181
     */
182
    public function hasItemNamed($name)
183
    {
184
        return $this->hasItemWith($name, '.sylius-product-name');
185
    }
186
187
    /**
188
     * {@inheritdoc}
189
     */
190
    public function hasItemWithVariantNamed($variantName)
191
    {
192
        return $this->hasItemWith($variantName, '.sylius-product-variant-name');
193
    }
194
195
    /**
196
     * {@inheritdoc}
197
     */
198
    public function hasItemWithOptionValue($productName, $optionName, $optionValue)
199
    {
200
        $itemElement = $this->getElement('product_row', ['%name%' => $productName]);
201
202
        $selector = sprintf('.sylius-product-options > .item[data-sylius-option-name="%s"]', $optionName);
203
        $optionValueElement = $itemElement->find('css', $selector);
204
205
        if (null === $optionValueElement) {
206
            throw new ElementNotFoundException($this->getSession(), sprintf('ProductOption value of "%s"', $optionName), 'css', $selector);
207
        }
208
209
        return $optionValue === $optionValueElement->getText();
210
    }
211
212
    /**
213
     * {@inheritdoc}
214
     */
215
    public function hasItemWithCode($code)
216
    {
217
        return $this->hasItemWith($code, '.sylius-product-variant-code');
218
    }
219
220
    /**
221
     * {@inheritdoc}
222
     */
223
    public function hasProductOutOfStockValidationMessage(ProductInterface $product)
224
    {
225
        $message = sprintf('%s does not have sufficient stock.', $product->getName());
226
227
        try {
228
            return $this->getElement('validation_errors')->getText() === $message;
229
        } catch (ElementNotFoundException $exception) {
230
            return false;
231
        }
232
    }
233
234
    /**
235
     * {@inheritdoc}
236
     */
237
    public function isEmpty()
238
    {
239
        return false !== strpos($this->getDocument()->find('css', '.message')->getText(), 'Your cart is empty');
240
    }
241
242
    /**
243
     * {@inheritdoc}
244
     */
245
    public function getQuantity($productName)
246
    {
247
        $itemElement = $this->getElement('product_row', ['%name%' => $productName]);
248
249
        return (int) $itemElement->find('css', 'input[type=number]')->getValue();
250
    }
251
252
    /**
253
     * {@inheritdoc}
254
     */
255
    public function getCartTotal()
256
    {
257
        $cartTotalText = $this->getElement('cart_total')->getText();
258
259
        if (strpos($cartTotalText, ',') !== false) {
260
            return strstr($cartTotalText, ',', true);
261
        }
262
263
        return trim($cartTotalText);
264
    }
265
266
    public function clearCart()
267
    {
268
        $this->getElement('clear_button')->click();
269
    }
270
271
    public function updateCart()
272
    {
273
        $this->getElement('update_button')->click();
274
    }
275
276
    /**
277
     * {@inheritdoc}
278
     */
279
    public function waitForRedirect($timeout)
280
    {
281
        $this->getDocument()->waitFor($timeout, function () {
282
            return $this->isOpen();
283
        });
284
    }
285
286
    /**
287
     * {@inheritdoc}
288
     */
289
    public function getPromotionCouponValidationMessage()
290
    {
291
        return $this->getElement('promotion_coupon_validation_message')->getText();
292
    }
293
294
    /**
295
     * {@inheritdoc}
296
     */
297
    protected function getDefinedElements(): array
298
    {
299
        return array_merge(parent::getDefinedElements(), [
300
            'apply_coupon_button' => 'button:contains("Apply coupon")',
301
            'base_grand_total' => '#sylius-cart-base-grand-total',
302
            'cart_items' => '#sylius-cart-items',
303
            'cart_total' => '#sylius-cart-total',
304
            'clear_button' => '#sylius-cart-clear',
305
            'coupon_field' => '#sylius_cart_promotionCoupon',
306
            'grand_total' => '#sylius-cart-grand-total',
307
            'item_image' => '#sylius-cart-items tbody tr:nth-child(%number%) img',
308
            'no_taxes' => '#sylius-cart-tax-none',
309
            'product_discounted_total' => '#sylius-cart-items tr:contains("%name%") .sylius-discounted-total',
310
            'product_row' => '#sylius-cart-items tbody tr:contains("%name%")',
311
            'product_total' => '#sylius-cart-items tr:contains("%name%") .sylius-total',
312
            'product_unit_price' => '#sylius-cart-items tr:contains("%name%") .sylius-unit-price',
313
            'product_unit_regular_price' => '#sylius-cart-items tr:contains("%name%") .sylius-regular-unit-price',
314
            'promotion_coupon_validation_message' => '#sylius-coupon .sylius-validation-error',
315
            'promotion_total' => '#sylius-cart-promotion-total',
316
            'save_button' => '#sylius-save',
317
            'shipping_total' => '#sylius-cart-shipping-total',
318
            'tax_excluded' => '#sylius-cart-tax-excluded',
319
            'tax_included' => '#sylius-cart-tax-included',
320
            'update_button' => '#sylius-cart-update',
321
            'validation_errors' => '.sylius-validation-error',
322
        ]);
323
    }
324
325
    /**
326
     * @param string $attributeName
327
     * @param string|array $selector
328
     *
329
     * @return bool
330
     *
331
     * @throws ElementNotFoundException
332
     */
333
    private function hasItemWith($attributeName, $selector)
334
    {
335
        $itemsAttributes = $this->getElement('cart_items')->findAll('css', $selector);
336
337
        foreach ($itemsAttributes as $itemAttribute) {
338
            if ($attributeName === $itemAttribute->getText()) {
339
                return true;
340
            }
341
        }
342
343
        return false;
344
    }
345
346
    private function getPriceFromString(string $price): int
347
    {
348
        return (int) round((float) str_replace(['€', '£', '$'], '', $price) * 100, 2);
349
    }
350
}
351