SummaryPage   A
last analyzed

Complexity

Total Complexity 34

Size/Duplication

Total Lines 306
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Importance

Changes 0
Metric Value
wmc 34
lcom 2
cbo 2
dl 0
loc 306
rs 9.68
c 0
b 0
f 0

29 Methods

Rating   Name   Duplication   Size   Complexity  
A getRouteName() 0 4 1
A getGrandTotal() 0 6 1
A getBaseGrandTotal() 0 6 1
A getTaxTotal() 0 6 1
A getShippingTotal() 0 6 1
A getPromotionTotal() 0 6 1
A getItemTotal() 0 6 1
A getItemUnitRegularPrice() 0 6 1
A getItemUnitPrice() 0 6 1
A isItemDiscounted() 0 4 1
A removeProduct() 0 5 1
A applyCoupon() 0 5 1
A changeQuantity() 0 7 1
A isSingleItemOnPage() 0 6 1
A hasItemNamed() 0 4 1
A hasItemWithVariantNamed() 0 4 1
A hasItemWithOptionValue() 0 13 2
A hasItemWithCode() 0 4 1
A hasProductOutOfStockValidationMessage() 0 10 2
A isEmpty() 0 4 1
A getQuantity() 0 6 1
A getCartTotal() 0 10 2
A clearCart() 0 4 1
A updateCart() 0 4 1
A waitForRedirect() 0 6 1
A getPromotionCouponValidationMessage() 0 4 1
A getDefinedElements() 0 24 1
A hasItemWith() 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
declare(strict_types=1);
13
14
namespace Sylius\Behat\Page\Shop\Cart;
15
16
use Behat\Mink\Exception\ElementNotFoundException;
17
use Sylius\Behat\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()
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
    /**
51
     * {@inheritdoc}
52
     */
53
    public function getTaxTotal()
54
    {
55
        $taxTotalElement = $this->getElement('tax_total');
56
57
        return $taxTotalElement->getText();
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63
    public function getShippingTotal()
64
    {
65
        $shippingTotalElement = $this->getElement('shipping_total');
66
67
        return $shippingTotalElement->getText();
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73
    public function getPromotionTotal()
74
    {
75
        $shippingTotalElement = $this->getElement('promotion_total');
76
77
        return $shippingTotalElement->getText();
78
    }
79
80
    /**
81
     * {@inheritdoc}
82
     */
83
    public function getItemTotal($productName)
84
    {
85
        $itemTotalElement = $this->getElement('product_total', ['%name%' => $productName]);
86
87
        return  $itemTotalElement->getText();
88
    }
89
90
    /**
91
     * {@inheritdoc}
92
     */
93
    public function getItemUnitRegularPrice($productName)
94
    {
95
        $regularUnitPrice = $this->getElement('product_unit_regular_price', ['%name%' => $productName]);
96
97
        return $this->getPriceFromString(trim($regularUnitPrice->getText()));
98
    }
99
100
    /**
101
     * {@inheritdoc}
102
     */
103
    public function getItemUnitPrice($productName)
104
    {
105
        $unitPrice = $this->getElement('product_unit_price', ['%name%' => $productName]);
106
107
        return $this->getPriceFromString(trim($unitPrice->getText()));
108
    }
109
110
    /**
111
     * {@inheritdoc}
112
     */
113
    public function isItemDiscounted($productName)
114
    {
115
        return $this->hasElement('product_unit_regular_price', ['%name%' => $productName]);
116
    }
117
118
    /**
119
     * {@inheritdoc}
120
     */
121
    public function removeProduct($productName)
122
    {
123
        $itemElement = $this->getElement('product_row', ['%name%' => $productName]);
124
        $itemElement->find('css', 'button.sylius-cart-remove-button')->press();
125
    }
126
127
    /**
128
     * {@inheritdoc}
129
     */
130
    public function applyCoupon($couponCode)
131
    {
132
        $this->getElement('coupon_field')->setValue($couponCode);
133
        $this->getElement('apply_coupon_button')->press();
134
    }
135
136
    /**
137
     * {@inheritdoc}
138
     */
139
    public function changeQuantity($productName, $quantity)
140
    {
141
        $itemElement = $this->getElement('product_row', ['%name%' => $productName]);
142
        $itemElement->find('css', 'input[type=number]')->setValue($quantity);
143
144
        $this->getElement('save_button')->click();
145
    }
146
147
    /**
148
     * {@inheritdoc}
149
     */
150
    public function isSingleItemOnPage()
151
    {
152
        $items = $this->getElement('cart_items')->findAll('css', 'tbody > tr');
153
154
        return 1 === count($items);
155
    }
156
157
    /**
158
     * {@inheritdoc}
159
     */
160
    public function hasItemNamed($name)
161
    {
162
        return $this->hasItemWith($name, '.sylius-product-name');
163
    }
164
165
    /**
166
     * {@inheritdoc}
167
     */
168
    public function hasItemWithVariantNamed($variantName)
169
    {
170
        return $this->hasItemWith($variantName, '.sylius-product-variant-name');
171
    }
172
173
    /**
174
     * {@inheritdoc}
175
     */
176
    public function hasItemWithOptionValue($productName, $optionName, $optionValue)
177
    {
178
        $itemElement = $this->getElement('product_row', ['%name%' => $productName]);
179
180
        $selector = sprintf('.sylius-product-options > .item[data-sylius-option-name="%s"]', $optionName);
181
        $optionValueElement = $itemElement->find('css', $selector);
182
183
        if (null === $optionValueElement) {
184
            throw new ElementNotFoundException($this->getSession(), sprintf('ProductOption value of "%s"', $optionName), 'css', $selector);
185
        }
186
187
        return $optionValue === $optionValueElement->getText();
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $optionValue === ...alueElement->getText(); (boolean) is incompatible with the return type declared by the interface Sylius\Behat\Page\Shop\C...:hasItemWithOptionValue of type string.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
188
    }
189
190
    /**
191
     * {@inheritdoc}
192
     */
193
    public function hasItemWithCode($code)
194
    {
195
        return $this->hasItemWith($code, '.sylius-product-variant-code');
196
    }
197
198
    /**
199
     * {@inheritdoc}
200
     */
201
    public function hasProductOutOfStockValidationMessage(ProductInterface $product)
202
    {
203
        $message = sprintf('%s does not have sufficient stock.', $product->getName());
204
205
        try {
206
            return $this->getElement('validation_errors')->getText() === $message;
207
        } catch (ElementNotFoundException $exception) {
208
            return false;
209
        }
210
    }
211
212
    /**
213
     * {@inheritdoc}
214
     */
215
    public function isEmpty()
216
    {
217
        return false !== strpos($this->getDocument()->find('css', '.message')->getText(), 'Your cart is empty');
218
    }
219
220
    /**
221
     * {@inheritdoc}
222
     */
223
    public function getQuantity($productName)
224
    {
225
        $itemElement = $this->getElement('product_row', ['%name%' => $productName]);
226
227
        return (int) $itemElement->find('css', 'input[type=number]')->getValue();
228
    }
229
230
    /**
231
     * {@inheritdoc}
232
     */
233
    public function getCartTotal()
234
    {
235
        $cartTotalText = $this->getElement('cart_total')->getText();
236
237
        if (strpos($cartTotalText, ',') !== false) {
238
            return strstr($cartTotalText, ',', true);
239
        }
240
241
        return trim($cartTotalText);
242
    }
243
244
    public function clearCart()
245
    {
246
        $this->getElement('clear_button')->click();
247
    }
248
249
    public function updateCart()
250
    {
251
        $this->getElement('update_button')->click();
252
    }
253
254
    /**
255
     * {@inheritdoc}
256
     */
257
    public function waitForRedirect($timeout)
258
    {
259
        $this->getDocument()->waitFor($timeout, function () {
260
            return $this->isOpen();
261
        });
262
    }
263
264
    /**
265
     * {@inheritdoc}
266
     */
267
    public function getPromotionCouponValidationMessage()
268
    {
269
        return $this->getElement('promotion_coupon_validation_message')->getText();
270
    }
271
272
    /**
273
     * {@inheritdoc}
274
     */
275
    protected function getDefinedElements()
276
    {
277
        return array_merge(parent::getDefinedElements(), [
278
            'apply_coupon_button' => 'button:contains("Apply coupon")',
279
            'cart_items' => '#sylius-cart-items',
280
            'cart_total' => '#sylius-cart-total',
281
            'clear_button' => '#sylius-cart-clear',
282
            'coupon_field' => '#sylius_cart_promotionCoupon',
283
            'grand_total' => '#sylius-cart-grand-total',
284
            'base_grand_total' => '#sylius-cart-base-grand-total',
285
            'product_discounted_total' => '#sylius-cart-items tr:contains("%name%") .sylius-discounted-total',
286
            'product_row' => '#sylius-cart-items tbody tr:contains("%name%")',
287
            'product_total' => '#sylius-cart-items tr:contains("%name%") .sylius-total',
288
            'product_unit_price' => '#sylius-cart-items tr:contains("%name%") .sylius-unit-price',
289
            'product_unit_regular_price' => '#sylius-cart-items tr:contains("%name%") .sylius-regular-unit-price',
290
            'promotion_coupon_validation_message' => '#sylius-coupon .sylius-validation-error',
291
            'promotion_total' => '#sylius-cart-promotion-total',
292
            'save_button' => '#sylius-save',
293
            'shipping_total' => '#sylius-cart-shipping-total',
294
            'tax_total' => '#sylius-cart-tax-total',
295
            'update_button' => '#sylius-cart-update',
296
            'validation_errors' => '.sylius-validation-error',
297
        ]);
298
    }
299
300
    /**
301
     * @param string $attributeName
302
     * @param string|array $selector
303
     *
304
     * @return bool
305
     *
306
     * @throws ElementNotFoundException
307
     */
308
    private function hasItemWith($attributeName, $selector)
309
    {
310
        $itemsAttributes = $this->getElement('cart_items')->findAll('css', $selector);
311
312
        foreach ($itemsAttributes as $itemAttribute) {
313
            if ($attributeName === $itemAttribute->getText()) {
314
                return true;
315
            }
316
        }
317
318
        return false;
319
    }
320
321
    private function getPriceFromString(string $price): int
322
    {
323
        return (int) round((float) str_replace(['€', '£', '$'], '', $price) * 100, 2);
324
    }
325
}
326