Completed
Push — symfony3-wololo-packages ( fecf70...6bf04d )
by Kamil
28:46 queued 11:35
created

SummaryPage::getDefinedElements()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

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