Completed
Push — master ( a818cb...b1c6ca )
by Kamil
07:13 queued 10s
created

ShowPage::resendShipmentConfirmationEmail()   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) {
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...
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
        $promotionTotalElement = $this->getElement('promotion_total');
192
193
        return trim(str_replace('Promotion total:', '', $promotionTotalElement->getText()));
194
    }
195
196
    public function hasPromotionDiscount(string $promotionName, string $promotionAmount): bool
197
    {
198
        $promotionDiscountsText = $this->getElement('promotion_discounts')->getText();
199
200
        return stripos($promotionDiscountsText, sprintf('%s %s', $promotionAmount, $promotionName)) !== false;
201
    }
202
203
    public function hasTax(string $tax): bool
204
    {
205
        $taxesText = $this->getElement('taxes')->getText();
206
207
        return stripos($taxesText, $tax) !== false;
208
    }
209
210
    public function getItemCode(string $itemName): string
211
    {
212
        return $this->getItemProperty($itemName, 'sylius-product-variant-code');
213
    }
214
215
    public function getItemUnitPrice(string $itemName): string
216
    {
217
        return $this->getRowWithItem($itemName)->find('css', '.unit-price')->getText();
218
    }
219
220
    public function getItemDiscountedUnitPrice(string $itemName): string
221
    {
222
        return $this->getRowWithItem($itemName)->find('css', '.discounted-unit-price')->getText();
223
    }
224
225
    public function getItemOrderDiscount(string $itemName): string
226
    {
227
        return $this->getRowWithItem($itemName)->find('css', '.unit-order-discount')->getText();
228
    }
229
230
    public function getItemQuantity(string $itemName): string
231
    {
232
        return $this->getItemProperty($itemName, 'quantity');
233
    }
234
235
    public function getItemSubtotal(string $itemName): string
236
    {
237
        return $this->getItemProperty($itemName, 'subtotal');
238
    }
239
240
    public function getItemDiscount(string $itemName): string
241
    {
242
        return $this->getItemProperty($itemName, 'unit-discount');
243
    }
244
245
    public function getItemTax(string $itemName): string
246
    {
247
        return $this->getRowWithItem($itemName)->find('css', '.tax-excluded')->getText();
248
    }
249
250
    public function getItemTaxIncludedInPrice(string $itemName): string
251
    {
252
        return $this->getRowWithItem($itemName)->find('css', '.tax-included')->getText();
253
    }
254
255
    public function getItemTotal(string $itemName): string
256
    {
257
        return $this->getItemProperty($itemName, 'total');
258
    }
259
260
    public function getPaymentAmount(): string
261
    {
262
        $paymentsPrice = $this->getElement('payments')->find('css', '.description');
263
264
        return $paymentsPrice->getText();
265
    }
266
267
    public function getPaymentsCount(): int
268
    {
269
        try {
270
            $payments = $this->getElement('payments')->findAll('css', '.item');
271
        } 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...
272
            return 0;
273
        }
274
275
        return count($payments);
276
    }
277
278
    public function getShipmentsCount(): int
279
    {
280
        try {
281
            $shipments = $this->getElement('shipments')->findAll('css', '.item');
282
        } 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...
283
            return 0;
284
        }
285
286
        return count($shipments);
287
    }
288
289
    public function hasCancelButton(): bool
290
    {
291
        return $this->getDocument()->hasButton('Cancel');
292
    }
293
294
    public function getOrderState(): string
295
    {
296
        return $this->getElement('order_state')->getText();
297
    }
298
299
    public function getPaymentState(): string
300
    {
301
        return $this->getElement('order_payment_state')->getText();
302
    }
303
304
    public function getShippingState(): string
305
    {
306
        return $this->getElement('order_shipping_state')->getText();
307
    }
308
309
    public function cancelOrder(): void
310
    {
311
        $this->getDocument()->pressButton('Cancel');
312
    }
313
314
    public function deleteOrder(): void
315
    {
316
        $this->getDocument()->pressButton('Delete');
317
    }
318
319
    public function hasNote(string $note): bool
320
    {
321
        $orderNotesElement = $this->getElement('order_notes');
322
323
        return $orderNotesElement->getText() === $note;
324
    }
325
326
    public function hasShippingProvinceName(string $provinceName): bool
327
    {
328
        $shippingAddressText = $this->getElement('shipping_address')->getText();
329
330
        return false !== stripos($shippingAddressText, $provinceName);
331
    }
332
333
    public function hasBillingProvinceName(string $provinceName): bool
334
    {
335
        $billingAddressText = $this->getElement('billing_address')->getText();
336
337
        return false !== stripos($billingAddressText, $provinceName);
338
    }
339
340
    public function getIpAddressAssigned(): string
341
    {
342
        return $this->getElement('ip_address')->getText();
343
    }
344
345
    public function getOrderCurrency(): string
346
    {
347
        return $this->getElement('currency')->getText();
348
    }
349
350
    public function hasRefundButton(): bool
351
    {
352
        return $this->getDocument()->hasButton('Refund');
353
    }
354
355
    public function getShippingPromotionData(): string
356
    {
357
        return $this->getElement('promotion_shipping_discounts')->getText();
358
    }
359
360
    public function getRouteName(): string
361
    {
362
        return 'sylius_admin_order_show';
363
    }
364
365
    public function hasInformationAboutNoPayment(): bool
366
    {
367
        return $this->getDocument()->has('css', '#no-payments:contains("Order without payments")');
368
    }
369
370
    public function resendOrderConfirmationEmail(): void
371
    {
372
        $this->getElement('resend_order_confirmation_email')->click();
373
    }
374
375
    public function resendShipmentConfirmationEmail(): void
376
    {
377
        $this->getElement('resend_shipment_confirmation_email')->click();
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
            'resend_order_confirmation_email' => '[data-test-resend-order-confirmation-email]',
397
            'resend_shipment_confirmation_email' => '[data-test-resend-shipment-confirmation-email]',
398
            'shipments' => '#sylius-shipments',
399
            'shipping_address' => '#shipping-address',
400
            'shipping_adjustment_name' => '#shipping-adjustment-label',
401
            'shipping_charges' => '#shipping-base-value',
402
            'shipping_total' => '#shipping-total',
403
            'table' => '.table',
404
            'tax_total' => '#tax-total',
405
            'taxes' => '#taxes',
406
            'total' => '#total',
407
        ]);
408
    }
409
410
    protected function getTableAccessor(): TableAccessorInterface
411
    {
412
        return $this->tableAccessor;
413
    }
414
415
    private function hasAddress(string $elementText, string $customerName, string $street, string $postcode, string $city, string $countryName): bool
416
    {
417
        return
418
            (stripos($elementText, $customerName) !== false) &&
419
            (stripos($elementText, $street) !== false) &&
420
            (stripos($elementText, $city) !== false) &&
421
            (stripos($elementText, $countryName . ' ' . $postcode) !== false)
422
        ;
423
    }
424
425
    private function getItemProperty(string $itemName, string $property): string
426
    {
427
        $rows = $this->tableAccessor->getRowsWithFields(
428
            $this->getElement('table'),
429
            ['item' => $itemName]
430
        );
431
432
        return $rows[0]->find('css', '.' . $property)->getText();
433
    }
434
435
    private function getRowWithItem(string $itemName): ?NodeElement
436
    {
437
        return $this->tableAccessor->getRowWithFields($this->getElement('table'), ['item' => $itemName]);
438
    }
439
440
    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...
441
    {
442
        $payment = $order->getPayments()->last();
443
444
        $paymentStateElements = $this->getElement('payments')->findAll('css', sprintf('span.ui.label:contains(\'%s\')', ucfirst($payment->getState())));
445
        $paymentStateElement = end($paymentStateElements);
446
447
        return $paymentStateElement->getParent()->getParent();
448
    }
449
450
    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...
451
    {
452
        $shipment = $order->getShipments()->last();
453
454
        $shipmentStateElements = $this->getElement('shipments')->findAll('css', sprintf('span.ui.label:contains(\'%s\')', ucfirst($shipment->getState())));
455
        $shipmentStateElement = end($shipmentStateElements);
456
457
        return $shipmentStateElement->getParent()->getParent();
458
    }
459
460
    private function getFormattedMoney(int $orderPromotionTotal): string
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
461
    {
462
        return $this->moneyFormatter->format($orderPromotionTotal, $this->getDocument()->find('css', '#sylius-order-currency')->getText());
463
    }
464
}
465