Completed
Push — master ( 3e7fd7...789110 )
by Paweł
162:07 queued 147:09
created

SummaryPage   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 199
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 21
c 1
b 0
f 0
lcom 1
cbo 3
dl 0
loc 199
rs 10

18 Methods

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