Completed
Push — master ( a9aa0c...602f1d )
by Kamil
26:22 queued 09:35
created

ShowPage::hasInformationAboutNoPayment()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
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
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\Bundle\MoneyBundle\Formatter\MoneyFormatterInterface;
22
use Sylius\Component\Core\Model\OrderInterface;
23
use Symfony\Component\Routing\RouterInterface;
24
25
class ShowPage extends SymfonyPage implements ShowPageInterface
26
{
27
    /** @var TableAccessorInterface */
28
    private $tableAccessor;
29
30
    /** @var MoneyFormatterInterface */
31
    private $moneyFormatter;
32
33
    public function __construct(
34
        Session $session,
35
        $minkParameters,
36
        RouterInterface $router,
37
        TableAccessorInterface $tableAccessor,
38
        MoneyFormatterInterface $moneyFormatter
39
    ) {
40
        parent::__construct($session, $minkParameters, $router);
41
42
        $this->tableAccessor = $tableAccessor;
43
        $this->moneyFormatter = $moneyFormatter;
44
    }
45
46
    public function hasCustomer(string $customerName): bool
47
    {
48
        $customerText = $this->getElement('customer')->getText();
49
50
        return stripos($customerText, $customerName) !== false;
51
    }
52
53
    public function hasShippingAddress(string $customerName, string $street, string $postcode, string $city, string $countryName): bool
54
    {
55
        $shippingAddressText = $this->getElement('shipping_address')->getText();
56
57
        return $this->hasAddress($shippingAddressText, $customerName, $street, $postcode, $city, $countryName);
58
    }
59
60
    public function hasShippingAddressVisible(): bool
61
    {
62
        try {
63
            $this->getElement('shipping_address');
64
        } catch (ElementNotFoundException $exception) {
65
            return false;
66
        }
67
68
        return true;
69
    }
70
71
    public function hasBillingAddress(string $customerName, string $street, string $postcode, string $city, string $countryName): bool
72
    {
73
        $billingAddressText = $this->getElement('billing_address')->getText();
74
75
        return $this->hasAddress($billingAddressText, $customerName, $street, $postcode, $city, $countryName);
76
    }
77
78
    public function hasShipment(string $shippingDetails): bool
79
    {
80
        $shipmentsText = $this->getElement('shipments')->getText();
81
82
        return stripos($shipmentsText, $shippingDetails) !== false;
83
    }
84
85
    public function specifyTrackingCode(string $code): void
86
    {
87
        $this->getDocument()->fillField('sylius_shipment_ship_tracking', $code);
88
    }
89
90
    public function canShipOrder(OrderInterface $order): bool
91
    {
92
        return $this->getLastOrderShipmentElement($order)->hasButton('Ship');
93
    }
94
95
    public function shipOrder(OrderInterface $order): void
96
    {
97
        $this->getLastOrderShipmentElement($order)->pressButton('Ship');
98
    }
99
100
    public function hasPayment(string $paymentDetails): bool
101
    {
102
        $paymentsText = $this->getElement('payments')->getText();
103
104
        return stripos($paymentsText, $paymentDetails) !== false;
105
    }
106
107
    public function canCompleteOrderLastPayment(OrderInterface $order): bool
108
    {
109
        return $this->getLastOrderPaymentElement($order)->hasButton('Complete');
110
    }
111
112
    public function completeOrderLastPayment(OrderInterface $order): void
113
    {
114
        $this->getLastOrderPaymentElement($order)->pressButton('Complete');
115
    }
116
117
    public function refundOrderLastPayment(OrderInterface $order): void
118
    {
119
        $this->getLastOrderPaymentElement($order)->pressButton('Refund');
120
    }
121
122
    public function countItems(): int
123
    {
124
        return $this->tableAccessor->countTableBodyRows($this->getElement('table'));
125
    }
126
127
    public function isProductInTheList(string $productName): bool
128
    {
129
        try {
130
            $table = $this->getElement('table');
131
            $rows = $this->tableAccessor->getRowsWithFields(
132
                $table,
133
                ['item' => $productName]
134
            );
135
136
            foreach ($rows as $row) {
137
                $field = $this->tableAccessor->getFieldFromRow($table, $row, 'item');
138
                $name = $field->find('css', '.sylius-product-name');
139
                if (null !== $name && $name->getText() === $productName) {
140
                    return true;
141
                }
142
            }
143
144
            return false;
145
        } catch (\InvalidArgumentException $exception) {
146
            return false;
147
        }
148
    }
149
150
    public function getItemsTotal(): string
151
    {
152
        $itemsTotalElement = $this->getElement('items_total');
153
154
        return trim(str_replace('Items total:', '', $itemsTotalElement->getText()));
155
    }
156
157
    public function getTotal(): string
158
    {
159
        $totalElement = $this->getElement('total');
160
161
        return trim(str_replace('Order total:', '', $totalElement->getText()));
162
    }
163
164
    public function getShippingTotal(): string
165
    {
166
        $shippingTotalElement = $this->getElement('shipping_total');
167
168
        return trim(str_replace('Shipping total:', '', $shippingTotalElement->getText()));
169
    }
170
171
    public function getTaxTotal(): string
172
    {
173
        $taxTotalElement = $this->getElement('tax_total');
174
175
        return trim(str_replace('Tax total:', '', $taxTotalElement->getText()));
176
    }
177
178
    public function hasShippingCharge(string $shippingCharge): bool
179
    {
180
        $shippingChargesText = sprintf(
181
            '%s %s',
182
            substr($this->getElement('shipping_adjustment_name')->getText(), 0, -1),
183
            $this->getElement('shipping_charges')->getText()
184
        );
185
186
        return stripos($shippingChargesText, $shippingCharge) !== false;
187
    }
188
189
    public function getOrderPromotionTotal(): string
190
    {
191
        /** @var NodeElement[] $rows */
192
        $rows = $this->getElement('table')->findAll('css', 'tbody tr');
193
194
        $orderPromotionTotal = 0;
195
196
        foreach ($rows as $row) {
197
            $unitOrderPromotion = $row->find('css', 'td:nth-child(4)')->getText();
198
            $quantity = $row->find('css', 'td:nth-child(6)')->getText();
199
            $itemOrderPromotion = (float) trim(str_replace('-$', '', $unitOrderPromotion)) * $quantity;
200
            $orderPromotionTotal += (int) ($itemOrderPromotion * 100);
201
        }
202
203
        return $this->getFormattedMoney($orderPromotionTotal > 0 ? -1 * $orderPromotionTotal : $orderPromotionTotal);
204
    }
205
206
    public function hasPromotionDiscount(string $promotionDiscount): bool
207
    {
208
        $promotionDiscountsText = $this->getElement('promotion_discounts')->getText();
209
210
        return stripos($promotionDiscountsText, $promotionDiscount) !== false;
211
    }
212
213
    public function hasTax(string $tax): bool
214
    {
215
        $taxesText = $this->getElement('taxes')->getText();
216
217
        return stripos($taxesText, $tax) !== false;
218
    }
219
220
    public function getItemCode(string $itemName): string
221
    {
222
        return $this->getItemProperty($itemName, 'sylius-product-variant-code');
223
    }
224
225
    public function getItemUnitPrice(string $itemName): string
226
    {
227
        return $this->getRowWithItem($itemName)->find('css', '.unit-price')->getText();
228
    }
229
230
    public function getItemDiscountedUnitPrice(string $itemName): string
231
    {
232
        return $this->getRowWithItem($itemName)->find('css', '.discounted-unit-price')->getText();
233
    }
234
235
    public function getItemOrderDiscount(string $itemName): string
236
    {
237
        return $this->getRowWithItem($itemName)->find('css', '.unit-order-discount')->getText();
238
    }
239
240
    public function getItemQuantity(string $itemName): string
241
    {
242
        return $this->getItemProperty($itemName, 'quantity');
243
    }
244
245
    public function getItemSubtotal(string $itemName): string
246
    {
247
        return $this->getItemProperty($itemName, 'subtotal');
248
    }
249
250
    public function getItemDiscount(string $itemName): string
251
    {
252
        return $this->getItemProperty($itemName, 'unit-discount');
253
    }
254
255
    public function getItemTax(string $itemName): string
256
    {
257
        return $this->getRowWithItem($itemName)->find('css', '.tax-excluded')->getText();
258
    }
259
260
    public function getItemTaxIncludedInPrice(string $itemName): string
261
    {
262
        return $this->getRowWithItem($itemName)->find('css', '.tax-included')->getText();
263
    }
264
265
    public function getItemTotal(string $itemName): string
266
    {
267
        return $this->getItemProperty($itemName, 'total');
268
    }
269
270
    public function getPaymentAmount(): string
271
    {
272
        $paymentsPrice = $this->getElement('payments')->find('css', '.description');
273
274
        return $paymentsPrice->getText();
275
    }
276
277
    public function getPaymentsCount(): int
278
    {
279
        try {
280
            $payments = $this->getElement('payments')->findAll('css', '.item');
281
        } catch (ElementNotFoundException $exception) {
282
            return 0;
283
        }
284
285
        return count($payments);
286
    }
287
288
    public function getShipmentsCount(): int
289
    {
290
        try {
291
            $shipments = $this->getElement('shipments')->findAll('css', '.item');
292
        } catch (ElementNotFoundException $exception) {
293
            return 0;
294
        }
295
296
        return count($shipments);
297
    }
298
299
    public function hasCancelButton(): bool
300
    {
301
        return $this->getDocument()->hasButton('Cancel');
302
    }
303
304
    public function getOrderState(): string
305
    {
306
        return $this->getElement('order_state')->getText();
307
    }
308
309
    public function getPaymentState(): string
310
    {
311
        return $this->getElement('order_payment_state')->getText();
312
    }
313
314
    public function getShippingState(): string
315
    {
316
        return $this->getElement('order_shipping_state')->getText();
317
    }
318
319
    public function cancelOrder(): void
320
    {
321
        $this->getDocument()->pressButton('Cancel');
322
    }
323
324
    public function deleteOrder(): void
325
    {
326
        $this->getDocument()->pressButton('Delete');
327
    }
328
329
    public function hasNote(string $note): bool
330
    {
331
        $orderNotesElement = $this->getElement('order_notes');
332
333
        return $orderNotesElement->getText() === $note;
334
    }
335
336
    public function hasShippingProvinceName(string $provinceName): bool
337
    {
338
        $shippingAddressText = $this->getElement('shipping_address')->getText();
339
340
        return false !== stripos($shippingAddressText, $provinceName);
341
    }
342
343
    public function hasBillingProvinceName(string $provinceName): bool
344
    {
345
        $billingAddressText = $this->getElement('billing_address')->getText();
346
347
        return false !== stripos($billingAddressText, $provinceName);
348
    }
349
350
    public function getIpAddressAssigned(): string
351
    {
352
        return $this->getElement('ip_address')->getText();
353
    }
354
355
    public function getOrderCurrency(): string
356
    {
357
        return $this->getElement('currency')->getText();
358
    }
359
360
    public function hasRefundButton(): bool
361
    {
362
        return $this->getDocument()->hasButton('Refund');
363
    }
364
365
    public function getShippingPromotionData(): string
366
    {
367
        return $this->getElement('promotion_shipping_discounts')->getText();
368
    }
369
370
    public function getRouteName(): string
371
    {
372
        return 'sylius_admin_order_show';
373
    }
374
375
    public function hasInformationAboutNoPayment(): bool
376
    {
377
        return $this->getDocument()->has('css', '#no-payments:contains("Order without payments")');
378
    }
379
380
    protected function getDefinedElements(): array
381
    {
382
        return array_merge(parent::getDefinedElements(), [
383
            'billing_address' => '#billing-address',
384
            'currency' => '#sylius-order-currency',
385
            'customer' => '#customer',
386
            'ip_address' => '#ipAddress',
387
            'items_total' => '#items-total',
388
            'order_notes' => '#sylius-order-notes',
389
            'order_payment_state' => '#payment-state > span',
390
            'order_shipping_state' => '#shipping-state > span',
391
            'order_state' => '#sylius-order-state',
392
            'payments' => '#sylius-payments',
393
            'promotion_discounts' => '#promotion-discounts',
394
            'promotion_shipping_discounts' => '#shipping-discount-value',
395
            'promotion_total' => '#promotion-total',
396
            'shipments' => '#sylius-shipments',
397
            'shipping_address' => '#shipping-address',
398
            'shipping_adjustment_name' => '#shipping-adjustment-label',
399
            'shipping_charges' => '#shipping-base-value',
400
            'shipping_total' => '#shipping-total',
401
            'table' => '.table',
402
            'tax_total' => '#tax-total',
403
            'taxes' => '#taxes',
404
            'total' => '#total',
405
        ]);
406
    }
407
408
    protected function getTableAccessor(): TableAccessorInterface
409
    {
410
        return $this->tableAccessor;
411
    }
412
413
    private function hasAddress(string $elementText, string $customerName, string $street, string $postcode, string $city, string $countryName): bool
414
    {
415
        return
416
            (stripos($elementText, $customerName) !== false) &&
417
            (stripos($elementText, $street) !== false) &&
418
            (stripos($elementText, $city) !== false) &&
419
            (stripos($elementText, $countryName . ' ' . $postcode) !== false)
420
        ;
421
    }
422
423
    private function getItemProperty(string $itemName, string $property): string
424
    {
425
        $rows = $this->tableAccessor->getRowsWithFields(
426
            $this->getElement('table'),
427
            ['item' => $itemName]
428
        );
429
430
        return $rows[0]->find('css', '.' . $property)->getText();
431
    }
432
433
    private function getRowWithItem(string $itemName): ?NodeElement
434
    {
435
        return $this->tableAccessor->getRowWithFields($this->getElement('table'), ['item' => $itemName]);
436
    }
437
438
    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...
439
    {
440
        $payment = $order->getPayments()->last();
441
442
        $paymentStateElements = $this->getElement('payments')->findAll('css', sprintf('span.ui.label:contains(\'%s\')', ucfirst($payment->getState())));
443
        $paymentStateElement = end($paymentStateElements);
444
445
        return $paymentStateElement->getParent()->getParent();
446
    }
447
448
    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...
449
    {
450
        $shipment = $order->getShipments()->last();
451
452
        $shipmentStateElements = $this->getElement('shipments')->findAll('css', sprintf('span.ui.label:contains(\'%s\')', ucfirst($shipment->getState())));
453
        $shipmentStateElement = end($shipmentStateElements);
454
455
        return $shipmentStateElement->getParent()->getParent();
456
    }
457
458
    private function getFormattedMoney(int $orderPromotionTotal): string
459
    {
460
        return $this->moneyFormatter->format($orderPromotionTotal, $this->getDocument()->find('css', '#sylius-order-currency')->getText());
461
    }
462
}
463