Completed
Push — symfony3-wololo-packages ( fecf70...6bf04d )
by Kamil
28:46 queued 11:35
created

ShowPage::getDefinedElements()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 8.9713
c 0
b 0
f 0
cc 1
eloc 21
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
namespace Sylius\Behat\Page\Admin\Order;
13
14
use Behat\Mink\Element\NodeElement;
15
use Behat\Mink\Session;
16
use Sylius\Behat\Page\SymfonyPage;
17
use Sylius\Behat\Service\Accessor\TableAccessorInterface;
18
use Sylius\Component\Core\Model\OrderInterface;
19
use Symfony\Component\Routing\RouterInterface;
20
21
/**
22
 * @author Łukasz Chruściel <[email protected]>
23
 * @author Grzegorz Sadowski <[email protected]>
24
 */
25
class ShowPage extends SymfonyPage implements ShowPageInterface
26
{
27
    /**
28
     * @var TableAccessorInterface
29
     */
30
    private $tableAccessor;
31
32
    /**
33
     * @param Session $session
34
     * @param array $parameters
35
     * @param RouterInterface $router
36
     * @param TableAccessorInterface $tableAccessor
37
     */
38
    public function __construct(
39
        Session $session,
40
        array $parameters,
41
        RouterInterface $router,
42
        TableAccessorInterface $tableAccessor
43
    ) {
44
        parent::__construct($session, $parameters, $router);
45
46
        $this->tableAccessor = $tableAccessor;
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52
    public function hasCustomer($customerName)
53
    {
54
        $customerText = $this->getElement('customer')->getText();
55
56
        return stripos($customerText, $customerName) !== false;
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62
    public function hasShippingAddress($customerName, $street, $postcode, $city, $countryName)
63
    {
64
        $shippingAddressText = $this->getElement('shipping_address')->getText();
65
66
        return $this->hasAddress($shippingAddressText, $customerName, $street, $postcode, $city, $countryName);
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72
    public function hasBillingAddress($customerName, $street, $postcode, $city, $countryName)
73
    {
74
        $billingAddressText = $this->getElement('billing_address')->getText();
75
76
        return $this->hasAddress($billingAddressText, $customerName, $street, $postcode, $city, $countryName);
77
    }
78
79
    /**
80
     * {@inheritdoc}
81
     */
82
    public function hasShipment($shippingDetails)
83
    {
84
        $shipmentsText = $this->getElement('shipments')->getText();
85
86
        return stripos($shipmentsText, $shippingDetails) !== false;
87
    }
88
89
    public function specifyTrackingCode($code)
90
    {
91
        $this->getDocument()->fillField('sylius_shipment_ship_tracking', $code);
92
    }
93
94
    public function canShipOrder(OrderInterface $order)
95
    {
96
        return $this->getLastOrderShipmentElement($order)->hasButton('Ship');
97
    }
98
99
    public function shipOrder(OrderInterface $order)
100
    {
101
        $this->getLastOrderShipmentElement($order)->pressButton('Ship');
102
    }
103
104
    /**
105
     * {@inheritdoc}
106
     */
107
    public function hasPayment($paymentDetails)
108
    {
109
        $paymentsText = $this->getElement('payments')->getText();
110
111
        return stripos($paymentsText, $paymentDetails) !== false;
112
    }
113
114
    /**
115
     * {@inheritdoc}
116
     */
117
    public function canCompleteOrderLastPayment(OrderInterface $order)
118
    {
119
        return $this->getLastOrderPaymentElement($order)->hasButton('Complete');
120
    }
121
122
    /**
123
     * {@inheritdoc}
124
     */
125
    public function completeOrderLastPayment(OrderInterface $order)
126
    {
127
        $this->getLastOrderPaymentElement($order)->pressButton('Complete');
128
    }
129
130
    /**
131
     * {@inheritdoc}
132
     */
133
    public function refundOrderLastPayment(OrderInterface $order)
134
    {
135
        $this->getLastOrderPaymentElement($order)->pressButton('Refund');
136
    }
137
138
    /**
139
     * {@inheritdoc}
140
     */
141
    public function countItems()
142
    {
143
        return $this->tableAccessor->countTableBodyRows($this->getElement('table'));
144
    }
145
146
    /**
147
     * {@inheritdoc}
148
     */
149
    public function isProductInTheList($productName)
150
    {
151
        try {
152
            $rows = $this->tableAccessor->getRowsWithFields(
153
                $this->getElement('table'),
154
                ['item' => $productName]
155
            );
156
157
            return 1 === count($rows);
158
159
        } catch (\InvalidArgumentException $exception) {
160
            return false;
161
        }
162
    }
163
164
    /**
165
     * {@inheritdoc}
166
     */
167
    public function getItemsTotal()
168
    {
169
        $itemsTotalElement = $this->getElement('items_total');
170
171
        return trim(str_replace('Subtotal:', '', $itemsTotalElement->getText()));
172
    }
173
174
    /**
175
     * {@inheritdoc}
176
     */
177
    public function getTotal()
178
    {
179
        $totalElement = $this->getElement('total');
180
181
        return trim(str_replace('Total:', '', $totalElement->getText()));
182
    }
183
184
    /**
185
     * {@inheritdoc}
186
     */
187
    public function getShippingTotal()
188
    {
189
        $shippingTotalElement = $this->getElement('shipping_total');
190
191
        return trim(str_replace('Shipping total:', '', $shippingTotalElement->getText()));
192
    }
193
194
    /**
195
     * {@inheritdoc}
196
     */
197
    public function getTaxTotal()
198
    {
199
        $taxTotalElement = $this->getElement('tax_total');
200
201
        return trim(str_replace('Tax total:', '', $taxTotalElement->getText()));
202
    }
203
204
    /**
205
     * {@inheritdoc}
206
     */
207
    public function hasShippingCharge($shippingCharge)
208
    {
209
        $shippingChargesText = $this->getElement('shipping_charges')->getText();
210
211
        return stripos($shippingChargesText, $shippingCharge) !== false;
212
    }
213
214
    /**
215
     * {@inheritdoc}
216
     */
217
    public function getPromotionTotal()
218
    {
219
        $promotionTotalElement = $this->getElement('promotion_total');
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $promotionTotalElement exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
220
221
        return trim(str_replace('Promotion total:', '', $promotionTotalElement->getText()));
222
    }
223
224
    /**
225
     * {@inheritdoc}
226
     */
227
    public function hasPromotionDiscount($promotionDiscount)
228
    {
229
        $promotionDiscountsText = $this->getElement('promotion_discounts')->getText();
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $promotionDiscountsText exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
230
231
        return stripos($promotionDiscountsText, $promotionDiscount) !== false;
232
    }
233
234
    /**
235
     * {@inheritdoc}
236
     */
237
    public function hasTax($tax)
238
    {
239
        $taxesText = $this->getElement('taxes')->getText();
240
241
        return stripos($taxesText, $tax) !== false;
242
    }
243
244
    /**
245
     * {@inheritdoc}
246
     */
247
    public function getItemCode($itemName)
248
    {
249
        return $this->getItemProperty($itemName, 'sylius-product-variant-code');
250
    }
251
252
    /**
253
     * {@inheritdoc}
254
     */
255
    public function getItemUnitPrice($itemName)
256
    {
257
        return $this->getItemProperty($itemName, 'unit-price');
258
    }
259
260
    /**
261
     * {@inheritdoc}
262
     */
263
    public function getItemDiscountedUnitPrice($itemName)
264
    {
265
        return $this->getItemProperty($itemName, 'discounted-unit-price');
266
    }
267
268
    /**
269
     * {@inheritdoc}
270
     */
271
    public function getItemQuantity($itemName)
272
    {
273
        return $this->getItemProperty($itemName, 'quantity');
274
    }
275
276
    /**
277
     * {@inheritdoc}
278
     */
279
    public function getItemSubtotal($itemName)
280
    {
281
        return $this->getItemProperty($itemName, 'subtotal');
282
    }
283
284
    /**
285
     * {@inheritdoc}
286
     */
287
    public function getItemDiscount($itemName)
288
    {
289
        return $this->getItemProperty($itemName, 'discount');
290
    }
291
292
    /**
293
     * {@inheritdoc}
294
     */
295
    public function getItemTax($itemName)
296
    {
297
        return $this->getItemProperty($itemName, 'tax');
298
    }
299
300
    /**
301
     * {@inheritdoc}
302
     */
303
    public function getItemTotal($itemName)
304
    {
305
        return $this->getItemProperty($itemName, 'total');
306
    }
307
308
    /**
309
     * {@inheritdoc}
310
     */
311
    public function getPaymentAmount()
312
    {
313
        $paymentsPrice = $this->getElement('payments')->find('css', '.description');
314
315
        return $paymentsPrice->getText();
316
    }
317
318
    /**
319
     * {@inheritdoc}
320
     */
321
    public function getPaymentsCount()
322
    {
323
        $payments = $this->getElement('payments')->findAll('css', '.item');
324
325
        return count($payments);
326
    }
327
328
    /**
329
     * {@inheritdoc}
330
     */
331
    public function hasCancelButton()
332
    {
333
        return $this->getDocument()->hasButton('Cancel');
334
    }
335
336
    /**
337
     * {@inheritdoc}
338
     */
339
    public function getOrderState()
340
    {
341
        return $this->getElement('order_state')->getText();
342
    }
343
344
    /**
345
     * {@inheritdoc}
346
     */
347
    public function getPaymentState()
348
    {
349
        return $this->getElement('order_payment_state')->getText();
350
    }
351
352
    public function cancelOrder()
353
    {
354
        $this->getDocument()->pressButton('Cancel');
355
    }
356
357
    public function deleteOrder()
358
    {
359
        $this->getDocument()->pressButton('Delete');
360
    }
361
362
    /**
363
     * {@inheritdoc}
364
     */
365
    public function hasNote($note)
366
    {
367
        $orderNotesElement = $this->getElement('order_notes');
368
369
        return $orderNotesElement->getText() === $note;
370
    }
371
372
    /**
373
     * {@inheritdoc}
374
     */
375
    public function hasShippingProvinceName($provinceName)
376
    {
377
        $shippingAddressText = $this->getElement('shipping_address')->getText();
378
379
        return false !== stripos($shippingAddressText, $provinceName);
380
    }
381
382
    /**
383
     * {@inheritdoc}
384
     */
385
    public function hasBillingProvinceName($provinceName)
386
    {
387
        $billingAddressText = $this->getElement('billing_address')->getText();
388
389
        return false !== stripos($billingAddressText, $provinceName);
390
    }
391
392
    /**
393
     * {@inheritdoc}
394
     */
395
    public function getIpAddressAssigned()
396
    {
397
        return $this->getElement('ip_address')->getText();
398
    }
399
400
    /**
401
     * {@inheritdoc}
402
     */
403
    public function getOrderCurrency()
404
    {
405
        return $this->getElement('currency')->getText();
406
    }
407
408
    /**
409
     * {@inheritdoc}
410
     */
411
    public function getRouteName()
412
    {
413
        return 'sylius_admin_order_show';
414
    }
415
416
    /**
417
     * {@inheritdoc}
418
     */
419
    protected function getDefinedElements()
420
    {
421
        return array_merge(parent::getDefinedElements(), [
422
            'billing_address' => '#billing-address',
423
            'currency' => '#sylius-order-currency',
424
            'customer' => '#customer',
425
            'ip_address' => '#ipAddress',
426
            'items_total' => '#items-total',
427
            'order_notes' => '#sylius-order-notes',
428
            'order_payment_state' => '#payment-state > span',
429
            'order_state' => '#sylius-order-state',
430
            'payments' => '#payments',
431
            'promotion_discounts' => '#promotion-discounts',
432
            'promotion_total' => '#promotion-total',
433
            'shipments' => '#shipments',
434
            'shipping_address' => '#shipping-address',
435
            'shipping_charges' => '#shipping-charges',
436
            'shipping_total' => '#shipping-total',
437
            'table' => '.table',
438
            'tax_total' => '#tax-total',
439
            'taxes' => '#taxes',
440
            'total' => '#total',
441
        ]);
442
    }
443
444
    /**
445
     * @return TableAccessorInterface
446
     */
447
    protected function getTableAccessor()
448
    {
449
        return $this->tableAccessor;
450
    }
451
452
    /**
453
     * @param string $elementText
454
     * @param string $customerName
455
     * @param string $street
456
     * @param string $postcode
457
     * @param string $city
458
     * @param string $countryName
459
     *
460
     * @return bool
461
     */
462
    private function hasAddress($elementText, $customerName, $street, $postcode, $city, $countryName)
463
    {
464
        return
465
            (stripos($elementText, $customerName) !== false) &&
466
            (stripos($elementText, $street) !== false) &&
467
            (stripos($elementText, $city) !== false) &&
468
            (stripos($elementText, $countryName.' '.$postcode) !== false)
469
        ;
470
    }
471
472
    /**
473
     * @param string $itemName
474
     * @param string $property
475
     *
476
     * @return string
477
     */
478
    private function getItemProperty($itemName, $property)
479
    {
480
        $rows = $this->tableAccessor->getRowsWithFields(
481
            $this->getElement('table'),
482
            ['item' => $itemName]
483
        );
484
485
        return $rows[0]->find('css', '.'.$property)->getText();
486
    }
487
488
    /**
489
     * @param OrderInterface $order
490
     *
491
     * @return NodeElement|null
492
     */
493
    private function getLastOrderPaymentElement(OrderInterface $order)
494
    {
495
        $payment = $order->getPayments()->last();
496
497
        $paymentStateElements = $this->getElement('payments')->findAll('css', sprintf('span.ui.label:contains(\'%s\')', ucfirst($payment->getState())));
498
        $paymentStateElement = end($paymentStateElements);
499
500
        return $paymentStateElement->getParent()->getParent();
501
    }
502
503
    /**
504
     * @param OrderInterface $order
505
     *
506
     * @return NodeElement|null
507
     */
508
    private function getLastOrderShipmentElement(OrderInterface $order)
509
    {
510
        $shipment = $order->getShipments()->last();
511
512
        $shipmentStateElements = $this->getElement('shipments')->findAll('css', sprintf('span.ui.label:contains(\'%s\')', ucfirst($shipment->getState())));
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $shipmentStateElements exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
513
        $shipmentStateElement = end($shipmentStateElements);
514
515
        return $shipmentStateElement->getParent()->getParent();
516
    }
517
}
518