Completed
Push — 1.3 ( 97f34f...f86edd )
by Kamil
10:36
created

ShowPage::hasShippingAddressVisible()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
cc 2
nc 2
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\Admin\Order;
15
16
use Behat\Mink\Element\NodeElement;
17
use Behat\Mink\Exception\ElementNotFoundException;
18
use Behat\Mink\Session;
19
use FriendsOfBehat\PageObjectExtension\Page\SymfonyPage;
20
use Sylius\Behat\Service\Accessor\TableAccessorInterface;
21
use Sylius\Component\Core\Model\OrderInterface;
22
use Symfony\Component\Routing\RouterInterface;
23
24
class ShowPage extends SymfonyPage implements ShowPageInterface
25
{
26
    /** @var TableAccessorInterface */
27
    private $tableAccessor;
28
29
    public function __construct(
30
        Session $session,
31
        array $parameters,
32
        RouterInterface $router,
33
        TableAccessorInterface $tableAccessor
34
    ) {
35
        parent::__construct($session, $parameters, $router);
36
37
        $this->tableAccessor = $tableAccessor;
38
    }
39
40
    public function hasCustomer(string $customerName): bool
41
    {
42
        $customerText = $this->getElement('customer')->getText();
43
44
        return stripos($customerText, $customerName) !== false;
45
    }
46
47
    public function hasShippingAddress(string $customerName, string $street, string $postcode, string $city, string $countryName): bool
48
    {
49
        $shippingAddressText = $this->getElement('shipping_address')->getText();
50
51
        return $this->hasAddress($shippingAddressText, $customerName, $street, $postcode, $city, $countryName);
52
    }
53
54
    public function hasShippingAddressVisible(): bool
55
    {
56
        try {
57
            $this->getElement('shipping_address');
58
        } catch (ElementNotFoundException $exception) {
0 ignored issues
show
Bug introduced by
The class Behat\Mink\Exception\ElementNotFoundException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
59
            return false;
60
        }
61
62
        return true;
63
    }
64
65
    public function hasBillingAddress(string $customerName, string $street, string $postcode, string $city, string $countryName): bool
66
    {
67
        $billingAddressText = $this->getElement('billing_address')->getText();
68
69
        return $this->hasAddress($billingAddressText, $customerName, $street, $postcode, $city, $countryName);
70
    }
71
72
    public function hasShipment(string $shippingDetails): bool
73
    {
74
        $shipmentsText = $this->getElement('shipments')->getText();
75
76
        return stripos($shipmentsText, $shippingDetails) !== false;
77
    }
78
79
    public function specifyTrackingCode(string $code): void
80
    {
81
        $this->getDocument()->fillField('sylius_shipment_ship_tracking', $code);
82
    }
83
84
    public function canShipOrder(OrderInterface $order): bool
85
    {
86
        return $this->getLastOrderShipmentElement($order)->hasButton('Ship');
87
    }
88
89
    public function shipOrder(OrderInterface $order): void
90
    {
91
        $this->getLastOrderShipmentElement($order)->pressButton('Ship');
92
    }
93
94
    public function hasPayment(string $paymentDetails): bool
95
    {
96
        $paymentsText = $this->getElement('payments')->getText();
97
98
        return stripos($paymentsText, $paymentDetails) !== false;
99
    }
100
101
    public function canCompleteOrderLastPayment(OrderInterface $order): bool
102
    {
103
        return $this->getLastOrderPaymentElement($order)->hasButton('Complete');
104
    }
105
106
    public function completeOrderLastPayment(OrderInterface $order): void
107
    {
108
        $this->getLastOrderPaymentElement($order)->pressButton('Complete');
109
    }
110
111
    public function refundOrderLastPayment(OrderInterface $order): void
112
    {
113
        $this->getLastOrderPaymentElement($order)->pressButton('Refund');
114
    }
115
116
    public function countItems(): int
117
    {
118
        return $this->tableAccessor->countTableBodyRows($this->getElement('table'));
119
    }
120
121
    public function isProductInTheList(string $productName): bool
122
    {
123
        try {
124
            $table = $this->getElement('table');
125
            $rows = $this->tableAccessor->getRowsWithFields(
126
                $table,
127
                ['item' => $productName]
128
            );
129
130
            foreach ($rows as $row) {
131
                $field = $this->tableAccessor->getFieldFromRow($table, $row, 'item');
132
                $name = $field->find('css', '.sylius-product-name');
133
                if (null !== $name && $name->getText() === $productName) {
134
                    return true;
135
                }
136
            }
137
138
            return false;
139
        } catch (\InvalidArgumentException $exception) {
140
            return false;
141
        }
142
    }
143
144
    public function getItemsTotal(): string
145
    {
146
        $itemsTotalElement = $this->getElement('items_total');
147
148
        return trim(str_replace('Subtotal:', '', $itemsTotalElement->getText()));
149
    }
150
151
    public function getTotal(): string
152
    {
153
        $totalElement = $this->getElement('total');
154
155
        return trim(str_replace('Total:', '', $totalElement->getText()));
156
    }
157
158
    public function getShippingTotal(): string
159
    {
160
        $shippingTotalElement = $this->getElement('shipping_total');
161
162
        return trim(str_replace('Shipping total:', '', $shippingTotalElement->getText()));
163
    }
164
165
    public function getTaxTotal(): string
166
    {
167
        $taxTotalElement = $this->getElement('tax_total');
168
169
        return trim(str_replace('Tax total:', '', $taxTotalElement->getText()));
170
    }
171
172
    public function hasShippingCharge(string $shippingCharge): bool
173
    {
174
        $shippingChargesText = $this->getElement('shipping_charges')->getText();
175
176
        return stripos($shippingChargesText, $shippingCharge) !== false;
177
    }
178
179
    public function getPromotionTotal(): string
180
    {
181
        $promotionTotalElement = $this->getElement('promotion_total');
182
183
        return trim(str_replace('Promotion total:', '', $promotionTotalElement->getText()));
184
    }
185
186
    public function hasPromotionDiscount(string $promotionDiscount): bool
187
    {
188
        $promotionDiscountsText = $this->getElement('promotion_discounts')->getText();
189
190
        return stripos($promotionDiscountsText, $promotionDiscount) !== false;
191
    }
192
193
    public function hasTax(string $tax): bool
194
    {
195
        $taxesText = $this->getElement('taxes')->getText();
196
197
        return stripos($taxesText, $tax) !== false;
198
    }
199
200
    public function getItemCode(string $itemName): string
201
    {
202
        return $this->getItemProperty($itemName, 'sylius-product-variant-code');
203
    }
204
205
    public function getItemUnitPrice(string $itemName): string
206
    {
207
        return $this->getItemProperty($itemName, 'unit-price');
208
    }
209
210
    public function getItemDiscountedUnitPrice(string $itemName): string
211
    {
212
        return $this->getItemProperty($itemName, 'discounted-unit-price');
213
    }
214
215
    public function getItemQuantity(string $itemName): string
216
    {
217
        return $this->getItemProperty($itemName, 'quantity');
218
    }
219
220
    public function getItemSubtotal(string $itemName): string
221
    {
222
        return $this->getItemProperty($itemName, 'subtotal');
223
    }
224
225
    public function getItemDiscount(string $itemName): string
226
    {
227
        return $this->getItemProperty($itemName, 'discount');
228
    }
229
230
    public function getItemTax(string $itemName): string
231
    {
232
        return $this->getItemProperty($itemName, 'tax');
233
    }
234
235
    public function getItemTotal(string $itemName): string
236
    {
237
        return $this->getItemProperty($itemName, 'total');
238
    }
239
240
    public function getPaymentAmount(): string
241
    {
242
        $paymentsPrice = $this->getElement('payments')->find('css', '.description');
243
244
        return $paymentsPrice->getText();
245
    }
246
247
    public function getPaymentsCount(): int
248
    {
249
        try {
250
            $payments = $this->getElement('payments')->findAll('css', '.item');
251
        } catch (ElementNotFoundException $exception) {
0 ignored issues
show
Bug introduced by
The class Behat\Mink\Exception\ElementNotFoundException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
252
            return 0;
253
        }
254
255
        return count($payments);
256
    }
257
258
    public function getShipmentsCount(): int
259
    {
260
        try {
261
            $shipments = $this->getElement('shipments')->findAll('css', '.item');
262
        } catch (ElementNotFoundException $exception) {
0 ignored issues
show
Bug introduced by
The class Behat\Mink\Exception\ElementNotFoundException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
263
            return 0;
264
        }
265
266
        return count($shipments);
267
    }
268
269
    public function hasCancelButton(): bool
270
    {
271
        return $this->getDocument()->hasButton('Cancel');
272
    }
273
274
    public function getOrderState(): string
275
    {
276
        return $this->getElement('order_state')->getText();
277
    }
278
279
    public function getPaymentState(): string
280
    {
281
        return $this->getElement('order_payment_state')->getText();
282
    }
283
284
    public function getShippingState(): string
285
    {
286
        return $this->getElement('order_shipping_state')->getText();
287
    }
288
289
    public function cancelOrder(): void
290
    {
291
        $this->getDocument()->pressButton('Cancel');
292
    }
293
294
    public function deleteOrder(): void
295
    {
296
        $this->getDocument()->pressButton('Delete');
297
    }
298
299
    public function hasNote(string $note): bool
300
    {
301
        $orderNotesElement = $this->getElement('order_notes');
302
303
        return $orderNotesElement->getText() === $note;
304
    }
305
306
    public function hasShippingProvinceName(string $provinceName): bool
307
    {
308
        $shippingAddressText = $this->getElement('shipping_address')->getText();
309
310
        return false !== stripos($shippingAddressText, $provinceName);
311
    }
312
313
    public function hasBillingProvinceName(string $provinceName): bool
314
    {
315
        $billingAddressText = $this->getElement('billing_address')->getText();
316
317
        return false !== stripos($billingAddressText, $provinceName);
318
    }
319
320
    public function getIpAddressAssigned(): string
321
    {
322
        return $this->getElement('ip_address')->getText();
323
    }
324
325
    public function getOrderCurrency(): string
326
    {
327
        return $this->getElement('currency')->getText();
328
    }
329
330
    public function hasRefundButton(): bool
331
    {
332
        return $this->getDocument()->hasButton('Refund');
333
    }
334
335
    public function getShippingPromotionData(): string
336
    {
337
        return $this->getElement('promotion_shipping_discounts')->getText();
338
    }
339
340
    public function getRouteName(): string
341
    {
342
        return 'sylius_admin_order_show';
343
    }
344
345
    protected function getDefinedElements(): array
346
    {
347
        return array_merge(parent::getDefinedElements(), [
348
            'billing_address' => '#billing-address',
349
            'currency' => '#sylius-order-currency',
350
            'customer' => '#customer',
351
            'ip_address' => '#ipAddress',
352
            'items_total' => '#items-total',
353
            'order_notes' => '#sylius-order-notes',
354
            'order_payment_state' => '#payment-state > span',
355
            'order_shipping_state' => '#shipping-state > span',
356
            'order_state' => '#sylius-order-state',
357
            'payments' => '#sylius-payments',
358
            'promotion_discounts' => '#promotion-discounts',
359
            'promotion_shipping_discounts' => '#promotion-shipping-discounts',
360
            'promotion_total' => '#promotion-total',
361
            'shipments' => '#sylius-shipments',
362
            'shipping_address' => '#shipping-address',
363
            'shipping_charges' => '#shipping-charges',
364
            'shipping_total' => '#shipping-total',
365
            'table' => '.table',
366
            'tax_total' => '#tax-total',
367
            'taxes' => '#taxes',
368
            'total' => '#total',
369
        ]);
370
    }
371
372
    protected function getTableAccessor(): TableAccessorInterface
373
    {
374
        return $this->tableAccessor;
375
    }
376
377
    private function hasAddress(string $elementText, string $customerName, string $street, string $postcode, string $city, string $countryName): bool
378
    {
379
        return
380
            (stripos($elementText, $customerName) !== false) &&
381
            (stripos($elementText, $street) !== false) &&
382
            (stripos($elementText, $city) !== false) &&
383
            (stripos($elementText, $countryName . ' ' . $postcode) !== false)
384
        ;
385
    }
386
387
    private function getItemProperty(string $itemName, string $property): string
388
    {
389
        $rows = $this->tableAccessor->getRowsWithFields(
390
            $this->getElement('table'),
391
            ['item' => $itemName]
392
        );
393
394
        return $rows[0]->find('css', '.' . $property)->getText();
395
    }
396
397
    private function getLastOrderPaymentElement(OrderInterface $order): ?NodeElement
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
398
    {
399
        $payment = $order->getPayments()->last();
400
401
        $paymentStateElements = $this->getElement('payments')->findAll('css', sprintf('span.ui.label:contains(\'%s\')', ucfirst($payment->getState())));
402
        $paymentStateElement = end($paymentStateElements);
403
404
        return $paymentStateElement->getParent()->getParent();
405
    }
406
407
    private function getLastOrderShipmentElement(OrderInterface $order): ?NodeElement
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
408
    {
409
        $shipment = $order->getShipments()->last();
410
411
        $shipmentStateElements = $this->getElement('shipments')->findAll('css', sprintf('span.ui.label:contains(\'%s\')', ucfirst($shipment->getState())));
412
        $shipmentStateElement = end($shipmentStateElements);
413
414
        return $shipmentStateElement->getParent()->getParent();
415
    }
416
}
417