Completed
Push — master ( 2ec63c...d54afb )
by Kamil
21:51
created

SummaryPage::getItemRegularPrice()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
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 Symfony\Component\Routing\RouterInterface;
17
18
/**
19
 * @author Mateusz Zalewski <[email protected]>
20
 * @author Anna Walasek <[email protected]>
21
 */
22
class SummaryPage extends SymfonyPage implements SummaryPageInterface
23
{
24
    /**
25
     * {@inheritdoc}
26
     */
27
    public function getGrandTotal()
28
    {
29
        $grandTotalElement = $this->getElement('grand_total');
30
31
        return trim(str_replace('Grand total:', '', $grandTotalElement->getText()));
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37
    public function getTaxTotal()
38
    {
39
        $taxTotalElement = $this->getElement('tax_total');
40
41
        return trim(str_replace('Tax total:', '', $taxTotalElement->getText()));
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47
    public function getShippingTotal()
48
    {
49
        $shippingTotalElement = $this->getElement('shipping_total');
50
51
        return trim(str_replace('Shipping total:', '', $shippingTotalElement->getText()));
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57
    public function getPromotionTotal()
58
    {
59
        $shippingTotalElement = $this->getElement('promotion_total');
60
61
        return trim(str_replace('Promotion total:', '', $shippingTotalElement->getText()));
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67
    public function getItemRegularPrice($productName)
68
    {
69
        $regularPriceElement = $this->getElement('product_regular_price', ['%name%' => $productName]);
70
71
        return $this->getPriceFromString(trim($regularPriceElement->getText()));
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77
    public function getItemDiscountPrice($productName)
78
    {
79
        $discountPriceElement = $this->getElement('product_discount_price', ['%name%' => $productName]);
80
81
        return $this->getPriceFromString(trim($discountPriceElement->getText()));
82
    }
83
84
    /**
85
     * {@inheritdoc}
86
     */
87
    public function isItemDiscounted($productName)
88
    {
89
        return $this->hasElement('product_discount_price', ['%name%' => $productName]);
90
    }
91
92
    /**
93
     * {@inheritdoc}
94
     */
95
    public function removeProduct($productName)
96
    {
97
        $itemElement = $this->getElement('product_row', ['%name%' => $productName]);
98
        $itemElement->find('css', 'a#remove-button')->click();
99
    }
100
101
    /**
102
     * {@inheritdoc}
103
     */
104
    public function changeQuantity($productName, $quantity)
105
    {
106
        $itemElement = $this->getElement('product_row', ['%name%' => $productName]);
107
        $itemElement->find('css', 'input[type=number]')->setValue($quantity);
108
109
        $this->getDocument()->pressButton('Save');
110
    }
111
112
    /**
113
     * {@inheritdoc}
114
     */
115
    public function isSingleItemOnPage()
116
    {
117
        $items = $this->getElement('cart_items')->findAll('css', 'tbody > tr');
118
119
        return 1 === count($items);
120
    }
121
122
    /**
123
     * {@inheritdoc}
124
     */
125
    public function isItemWithName($name)
126
    {
127
       return $this->findItemWith($name, 'tbody  tr > td > div > a > strong');
128
    }
129
130
    /**
131
     * {@inheritdoc}
132
     */
133
    public function isItemWithVariant($variantName)
134
    {
135
       return $this->findItemWith($variantName, 'tbody  tr > td > strong');
136
    }
137
138
    /**
139
     * {@inheritdoc}
140
     */
141
    public function getProductOption($productName, $optionName)
142
    {
143
        $itemElement = $this->getElement('product_row', ['%name%' => $productName]);
144
145
        return $itemElement->find('css', sprintf('li:contains("%s")', ucfirst($optionName)))->getText();
146
    }
147
148
    /**
149
     * {@inheritdoc}
150
     */
151
    public function isEmpty()
152
    {
153
        $isEmpty = strpos($this->getDocument()->find('css', '.message')->getText(), 'Your cart is empty');
154
        if (false === $isEmpty ) {
155
            return false;
156
        }
157
158
        return true;
159
    }
160
161
    /**
162
     * {@inheritdoc}
163
     */
164
    public function getRouteName()
165
    {
166
        return 'sylius_shop_cart_summary';
167
    }
168
169
    /**
170
     * {@inheritdoc}
171
     */
172
    public function getQuantity($productName)
173
    {
174
        $itemElement = $this->getElement('product_row', ['%name%' => $productName]);
175
176
        return (int) $itemElement->find('css', 'input[type=number]')->getValue();
177
    }
178
179
    public function clearCart()
180
    {
181
        $this->getElement('clear_button')->click();
182
    }
183
184
    /**
185
     * {@inheritdoc}
186
     */
187
    protected function getDefinedElements()
188
    {
189
        return array_merge(parent::getDefinedElements(), [
190
            'grand_total' => '#cart-summary td:contains("Grand total")',
191
            'promotion_total' => '#cart-summary td:contains("Promotion total")',
192
            'shipping_total' => '#cart-summary td:contains("Shipping total")',
193
            'tax_total' => '#cart-summary td:contains("Tax total")',
194
            'product_row' => '#cart-items tbody tr:contains("%name%")',
195
            'product_regular_price' => '#cart-items tr:contains("%name%") .regular-price',
196
            'product_discount_price' => '#cart-items tr:contains("%name%") .discount-price',
197
            'total' => '.total',
198
            'quantity' => '#sylius_cart_items_%number%_quantity',
199
            'unit_price' => '.unit-price',
200
            'cart_items' => '#cart-items',
201
            'cart_summary' => '#cart-summary',
202
            'clear_button' => 'a[class*="clear"]',
203
        ]);
204
    }
205
206
    /**
207
     * @param $attributeName
208
     * @param $selector
209
     *
210
     * @return bool
211
     *
212
     * @throws ElementNotFoundException
213
     */
214
    private function findItemWith($attributeName, $selector)
215
    {
216
        $itemsAttributes = $this->getElement('cart_items')->findAll('css', $selector);
217
218
        foreach($itemsAttributes as $itemAttribute) {
219
            if($attributeName === $itemAttribute->getText()) {
220
                return true;
221
            }
222
        }
223
224
        return false;
225
    }
226
227
    /**
228
     * @param string $price
229
     *
230
     * @return int
231
     */
232
    private function getPriceFromString($price)
233
    {
234
        return (int) round((str_replace(['€', '£', '$'], '', $price) * 100), 2);
235
    }
236
}
237