Completed
Push — 1.0-symfony-3.3.13 ( 577127...a3487e )
by Kamil
50:10 queued 26:38
created

ShowPage::hasCustomer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 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\Admin\Order;
15
16
use Behat\Mink\Element\NodeElement;
17
use Behat\Mink\Exception\ElementNotFoundException;
18
use Behat\Mink\Session;
19
use Sylius\Behat\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
    /**
27
     * @var TableAccessorInterface
28
     */
29
    private $tableAccessor;
30
31
    /**
32
     * @param Session $session
33
     * @param array $parameters
34
     * @param RouterInterface $router
35
     * @param TableAccessorInterface $tableAccessor
36
     */
37
    public function __construct(
38
        Session $session,
39
        array $parameters,
40
        RouterInterface $router,
41
        TableAccessorInterface $tableAccessor
42
    ) {
43
        parent::__construct($session, $parameters, $router);
44
45
        $this->tableAccessor = $tableAccessor;
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51
    public function hasCustomer($customerName)
52
    {
53
        $customerText = $this->getElement('customer')->getText();
54
55
        return stripos($customerText, $customerName) !== false;
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61
    public function hasShippingAddress($customerName, $street, $postcode, $city, $countryName)
62
    {
63
        $shippingAddressText = $this->getElement('shipping_address')->getText();
64
65
        return $this->hasAddress($shippingAddressText, $customerName, $street, $postcode, $city, $countryName);
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71
    public function hasBillingAddress($customerName, $street, $postcode, $city, $countryName)
72
    {
73
        $billingAddressText = $this->getElement('billing_address')->getText();
74
75
        return $this->hasAddress($billingAddressText, $customerName, $street, $postcode, $city, $countryName);
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81
    public function hasShipment($shippingDetails)
82
    {
83
        $shipmentsText = $this->getElement('shipments')->getText();
84
85
        return stripos($shipmentsText, $shippingDetails) !== false;
86
    }
87
88
    public function specifyTrackingCode($code)
89
    {
90
        $this->getDocument()->fillField('sylius_shipment_ship_tracking', $code);
91
    }
92
93
    public function canShipOrder(OrderInterface $order)
94
    {
95
        return $this->getLastOrderShipmentElement($order)->hasButton('Ship');
96
    }
97
98
    public function shipOrder(OrderInterface $order)
99
    {
100
        $this->getLastOrderShipmentElement($order)->pressButton('Ship');
101
    }
102
103
    /**
104
     * {@inheritdoc}
105
     */
106
    public function hasPayment($paymentDetails)
107
    {
108
        $paymentsText = $this->getElement('payments')->getText();
109
110
        return stripos($paymentsText, $paymentDetails) !== false;
111
    }
112
113
    /**
114
     * {@inheritdoc}
115
     */
116
    public function canCompleteOrderLastPayment(OrderInterface $order)
117
    {
118
        return $this->getLastOrderPaymentElement($order)->hasButton('Complete');
119
    }
120
121
    /**
122
     * {@inheritdoc}
123
     */
124
    public function completeOrderLastPayment(OrderInterface $order)
125
    {
126
        $this->getLastOrderPaymentElement($order)->pressButton('Complete');
127
    }
128
129
    /**
130
     * {@inheritdoc}
131
     */
132
    public function refundOrderLastPayment(OrderInterface $order)
133
    {
134
        $this->getLastOrderPaymentElement($order)->pressButton('Refund');
135
    }
136
137
    /**
138
     * {@inheritdoc}
139
     */
140
    public function countItems()
141
    {
142
        return $this->tableAccessor->countTableBodyRows($this->getElement('table'));
143
    }
144
145
    /**
146
     * {@inheritdoc}
147
     */
148
    public function isProductInTheList($productName)
149
    {
150
        try {
151
            $rows = $this->tableAccessor->getRowsWithFields(
152
                $this->getElement('table'),
153
                ['item' => $productName]
154
            );
155
156
            return 1 === count($rows);
157
        } catch (\InvalidArgumentException $exception) {
158
            return false;
159
        }
160
    }
161
162
    /**
163
     * {@inheritdoc}
164
     */
165
    public function getItemsTotal()
166
    {
167
        $itemsTotalElement = $this->getElement('items_total');
168
169
        return trim(str_replace('Subtotal:', '', $itemsTotalElement->getText()));
170
    }
171
172
    /**
173
     * {@inheritdoc}
174
     */
175
    public function getTotal()
176
    {
177
        $totalElement = $this->getElement('total');
178
179
        return trim(str_replace('Total:', '', $totalElement->getText()));
180
    }
181
182
    /**
183
     * {@inheritdoc}
184
     */
185
    public function getShippingTotal()
186
    {
187
        $shippingTotalElement = $this->getElement('shipping_total');
188
189
        return trim(str_replace('Shipping total:', '', $shippingTotalElement->getText()));
190
    }
191
192
    /**
193
     * {@inheritdoc}
194
     */
195
    public function getTaxTotal()
196
    {
197
        $taxTotalElement = $this->getElement('tax_total');
198
199
        return trim(str_replace('Tax total:', '', $taxTotalElement->getText()));
200
    }
201
202
    /**
203
     * {@inheritdoc}
204
     */
205
    public function hasShippingCharge($shippingCharge)
206
    {
207
        $shippingChargesText = $this->getElement('shipping_charges')->getText();
208
209
        return stripos($shippingChargesText, $shippingCharge) !== false;
210
    }
211
212
    /**
213
     * {@inheritdoc}
214
     */
215
    public function getPromotionTotal()
216
    {
217
        $promotionTotalElement = $this->getElement('promotion_total');
218
219
        return trim(str_replace('Promotion total:', '', $promotionTotalElement->getText()));
220
    }
221
222
    /**
223
     * {@inheritdoc}
224
     */
225
    public function hasPromotionDiscount($promotionDiscount)
226
    {
227
        $promotionDiscountsText = $this->getElement('promotion_discounts')->getText();
228
229
        return stripos($promotionDiscountsText, $promotionDiscount) !== false;
230
    }
231
232
    /**
233
     * {@inheritdoc}
234
     */
235
    public function hasShippingPromotion($promotionName)
236
    {
237
        return $this->getElement('promotion_shipping_discounts')->getText();
238
    }
239
240
    /**
241
     * {@inheritdoc}
242
     */
243
    public function hasTax($tax)
244
    {
245
        $taxesText = $this->getElement('taxes')->getText();
246
247
        return stripos($taxesText, $tax) !== false;
248
    }
249
250
    /**
251
     * {@inheritdoc}
252
     */
253
    public function getItemCode($itemName)
254
    {
255
        return $this->getItemProperty($itemName, 'sylius-product-variant-code');
256
    }
257
258
    /**
259
     * {@inheritdoc}
260
     */
261
    public function getItemUnitPrice($itemName)
262
    {
263
        return $this->getItemProperty($itemName, 'unit-price');
264
    }
265
266
    /**
267
     * {@inheritdoc}
268
     */
269
    public function getItemDiscountedUnitPrice($itemName)
270
    {
271
        return $this->getItemProperty($itemName, 'discounted-unit-price');
272
    }
273
274
    /**
275
     * {@inheritdoc}
276
     */
277
    public function getItemQuantity($itemName)
278
    {
279
        return $this->getItemProperty($itemName, 'quantity');
280
    }
281
282
    /**
283
     * {@inheritdoc}
284
     */
285
    public function getItemSubtotal($itemName)
286
    {
287
        return $this->getItemProperty($itemName, 'subtotal');
288
    }
289
290
    /**
291
     * {@inheritdoc}
292
     */
293
    public function getItemDiscount($itemName)
294
    {
295
        return $this->getItemProperty($itemName, 'discount');
296
    }
297
298
    /**
299
     * {@inheritdoc}
300
     */
301
    public function getItemTax($itemName)
302
    {
303
        return $this->getItemProperty($itemName, 'tax');
304
    }
305
306
    /**
307
     * {@inheritdoc}
308
     */
309
    public function getItemTotal($itemName)
310
    {
311
        return $this->getItemProperty($itemName, 'total');
312
    }
313
314
    /**
315
     * {@inheritdoc}
316
     */
317
    public function getPaymentAmount()
318
    {
319
        $paymentsPrice = $this->getElement('payments')->find('css', '.description');
320
321
        return $paymentsPrice->getText();
322
    }
323
324
    /**
325
     * {@inheritdoc}
326
     */
327
    public function getPaymentsCount()
328
    {
329
        try {
330
            $payments = $this->getElement('payments')->findAll('css', '.item');
331
        } catch (ElementNotFoundException $exception) {
332
            return 0;
333
        }
334
335
        return count($payments);
336
    }
337
338
    /**
339
     * {@inheritdoc}
340
     */
341
    public function getShipmentsCount(): int
342
    {
343
        try {
344
            $shipments = $this->getElement('shipments')->findAll('css', '.item');
345
        } catch (ElementNotFoundException $exception) {
346
            return 0;
347
        }
348
349
        return count($shipments);
350
    }
351
352
    /**
353
     * {@inheritdoc}
354
     */
355
    public function hasCancelButton()
356
    {
357
        return $this->getDocument()->hasButton('Cancel');
358
    }
359
360
    /**
361
     * {@inheritdoc}
362
     */
363
    public function getOrderState()
364
    {
365
        return $this->getElement('order_state')->getText();
366
    }
367
368
    /**
369
     * {@inheritdoc}
370
     */
371
    public function getPaymentState()
372
    {
373
        return $this->getElement('order_payment_state')->getText();
374
    }
375
376
    /**
377
     * {@inheritdoc}
378
     */
379
    public function getShippingState()
380
    {
381
        return $this->getElement('order_shipping_state')->getText();
382
    }
383
384
    public function cancelOrder()
385
    {
386
        $this->getDocument()->pressButton('Cancel');
387
    }
388
389
    public function deleteOrder()
390
    {
391
        $this->getDocument()->pressButton('Delete');
392
    }
393
394
    /**
395
     * {@inheritdoc}
396
     */
397
    public function hasNote($note)
398
    {
399
        $orderNotesElement = $this->getElement('order_notes');
400
401
        return $orderNotesElement->getText() === $note;
402
    }
403
404
    /**
405
     * {@inheritdoc}
406
     */
407
    public function hasShippingProvinceName($provinceName)
408
    {
409
        $shippingAddressText = $this->getElement('shipping_address')->getText();
410
411
        return false !== stripos($shippingAddressText, $provinceName);
412
    }
413
414
    /**
415
     * {@inheritdoc}
416
     */
417
    public function hasBillingProvinceName($provinceName)
418
    {
419
        $billingAddressText = $this->getElement('billing_address')->getText();
420
421
        return false !== stripos($billingAddressText, $provinceName);
422
    }
423
424
    /**
425
     * {@inheritdoc}
426
     */
427
    public function getIpAddressAssigned()
428
    {
429
        return $this->getElement('ip_address')->getText();
430
    }
431
432
    /**
433
     * {@inheritdoc}
434
     */
435
    public function getOrderCurrency()
436
    {
437
        return $this->getElement('currency')->getText();
438
    }
439
440
    /**
441
     * {@inheritdoc}
442
     */
443
    public function hasRefundButton()
444
    {
445
        return $this->getDocument()->hasButton('Refund');
446
    }
447
448
    /**
449
     * {@inheritdoc}
450
     */
451
    public function getShippingPromotionData()
452
    {
453
        return $this->getElement('promotion_shipping_discounts')->getText();
454
    }
455
456
    /**
457
     * {@inheritdoc}
458
     */
459
    public function getRouteName()
460
    {
461
        return 'sylius_admin_order_show';
462
    }
463
464
    /**
465
     * {@inheritdoc}
466
     */
467
    protected function getDefinedElements()
468
    {
469
        return array_merge(parent::getDefinedElements(), [
470
            'billing_address' => '#billing-address',
471
            'currency' => '#sylius-order-currency',
472
            'customer' => '#customer',
473
            'ip_address' => '#ipAddress',
474
            'items_total' => '#items-total',
475
            'order_notes' => '#sylius-order-notes',
476
            'order_payment_state' => '#payment-state > span',
477
            'order_shipping_state' => '#shipping-state > span',
478
            'order_state' => '#sylius-order-state',
479
            'payments' => '#sylius-payments',
480
            'promotion_discounts' => '#promotion-discounts',
481
            'promotion_shipping_discounts' => '#promotion-shipping-discounts',
482
            'promotion_total' => '#promotion-total',
483
            'shipments' => '#sylius-shipments',
484
            'shipping_address' => '#shipping-address',
485
            'shipping_charges' => '#shipping-charges',
486
            'shipping_total' => '#shipping-total',
487
            'table' => '.table',
488
            'tax_total' => '#tax-total',
489
            'taxes' => '#taxes',
490
            'total' => '#total',
491
        ]);
492
    }
493
494
    /**
495
     * @return TableAccessorInterface
496
     */
497
    protected function getTableAccessor()
498
    {
499
        return $this->tableAccessor;
500
    }
501
502
    /**
503
     * @param string $elementText
504
     * @param string $customerName
505
     * @param string $street
506
     * @param string $postcode
507
     * @param string $city
508
     * @param string $countryName
509
     *
510
     * @return bool
511
     */
512
    private function hasAddress($elementText, $customerName, $street, $postcode, $city, $countryName)
513
    {
514
        return
515
            (stripos($elementText, $customerName) !== false) &&
516
            (stripos($elementText, $street) !== false) &&
517
            (stripos($elementText, $city) !== false) &&
518
            (stripos($elementText, $countryName . ' ' . $postcode) !== false)
519
        ;
520
    }
521
522
    /**
523
     * @param string $itemName
524
     * @param string $property
525
     *
526
     * @return string
527
     */
528
    private function getItemProperty($itemName, $property)
529
    {
530
        $rows = $this->tableAccessor->getRowsWithFields(
531
            $this->getElement('table'),
532
            ['item' => $itemName]
533
        );
534
535
        return $rows[0]->find('css', '.' . $property)->getText();
536
    }
537
538
    /**
539
     * @param OrderInterface $order
540
     *
541
     * @return NodeElement|null
542
     */
543
    private function getLastOrderPaymentElement(OrderInterface $order)
544
    {
545
        $payment = $order->getPayments()->last();
546
547
        $paymentStateElements = $this->getElement('payments')->findAll('css', sprintf('span.ui.label:contains(\'%s\')', ucfirst($payment->getState())));
548
        $paymentStateElement = end($paymentStateElements);
549
550
        return $paymentStateElement->getParent()->getParent();
551
    }
552
553
    /**
554
     * @param OrderInterface $order
555
     *
556
     * @return NodeElement|null
557
     */
558
    private function getLastOrderShipmentElement(OrderInterface $order)
559
    {
560
        $shipment = $order->getShipments()->last();
561
562
        $shipmentStateElements = $this->getElement('shipments')->findAll('css', sprintf('span.ui.label:contains(\'%s\')', ucfirst($shipment->getState())));
563
        $shipmentStateElement = end($shipmentStateElements);
564
565
        return $shipmentStateElement->getParent()->getParent();
566
    }
567
}
568