Completed
Push — master ( 429264...faaec3 )
by Paweł
25s
created

ShowPage::hasNote()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 1
Metric Value
c 2
b 1
f 1
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
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 countItems()
134
    {
135
        return $this->tableAccessor->countTableBodyRows($this->getElement('table'));
136
    }
137
138
    /**
139
     * {@inheritdoc}
140
     */
141
    public function isProductInTheList($productName)
142
    {
143
        try {
144
            $rows = $this->tableAccessor->getRowsWithFields(
145
                $this->getElement('table'),
146
                ['item' => $productName]
147
            );
148
149
            return 1 === count($rows);
150
151
        } catch (\InvalidArgumentException $exception) {
152
            return false;
153
        }
154
    }
155
156
    /**
157
     * {@inheritdoc}
158
     */
159
    public function getItemsTotal()
160
    {
161
        $itemsTotalElement = $this->getElement('items_total');
162
163
        return trim(str_replace('Subtotal:', '', $itemsTotalElement->getText()));
164
    }
165
166
    /**
167
     * {@inheritdoc}
168
     */
169
    public function getTotal()
170
    {
171
        $totalElement = $this->getElement('total');
172
173
        return trim(str_replace('Total:', '', $totalElement->getText()));
174
    }
175
176
    /**
177
     * {@inheritdoc}
178
     */
179
    public function getShippingTotal()
180
    {
181
        $shippingTotalElement = $this->getElement('shipping_total');
182
183
        return trim(str_replace('Shipping total:', '', $shippingTotalElement->getText()));
184
    }
185
186
    /**
187
     * {@inheritdoc}
188
     */
189
    public function getTaxTotal()
190
    {
191
        $taxTotalElement = $this->getElement('tax_total');
192
193
        return trim(str_replace('Tax total:', '', $taxTotalElement->getText()));
194
    }
195
196
    /**
197
     * {@inheritdoc}
198
     */
199
    public function hasShippingCharge($shippingCharge)
200
    {
201
        $shippingChargesText = $this->getElement('shipping_charges')->getText();
202
203
        return stripos($shippingChargesText, $shippingCharge) !== false;
204
    }
205
206
    /**
207
     * {@inheritdoc}
208
     */
209
    public function getPromotionTotal()
210
    {
211
        $promotionTotalElement = $this->getElement('promotion_total');
212
213
        return trim(str_replace('Promotion total:', '', $promotionTotalElement->getText()));
214
    }
215
216
    /**
217
     * {@inheritdoc}
218
     */
219
    public function hasPromotionDiscount($promotionDiscount)
220
    {
221
        $promotionDiscountsText = $this->getElement('promotion_discounts')->getText();
222
223
        return stripos($promotionDiscountsText, $promotionDiscount) !== false;
224
    }
225
226
    /**
227
     * {@inheritdoc}
228
     */
229
    public function hasTax($tax)
230
    {
231
        $taxesText = $this->getElement('taxes')->getText();
232
233
        return stripos($taxesText, $tax) !== false;
234
    }
235
236
    /**
237
     * @param string $itemName
238
     *
239
     * @return string
240
     */
241
    public function getItemUnitPrice($itemName)
242
    {
243
        return $this->getItemProperty($itemName, 'unit-price');
244
    }
245
246
    /**
247
     * @param string $itemName
248
     *
249
     * @return string
250
     */
251
    public function getItemDiscountedUnitPrice($itemName)
252
    {
253
        return $this->getItemProperty($itemName, 'discounted-unit-price');
254
    }
255
256
    /**
257
     * @param string $itemName
258
     *
259
     * @return string
260
     */
261
    public function getItemQuantity($itemName)
262
    {
263
        return $this->getItemProperty($itemName, 'quantity');
264
    }
265
266
    /**
267
     * @param string $itemName
268
     *
269
     * @return string
270
     */
271
    public function getItemSubtotal($itemName)
272
    {
273
        return $this->getItemProperty($itemName, 'subtotal');
274
    }
275
276
    /**
277
     * @param string $itemName
278
     *
279
     * @return string
280
     */
281
    public function getItemDiscount($itemName)
282
    {
283
        return $this->getItemProperty($itemName, 'discount');
284
    }
285
286
    /**
287
     * @param string $itemName
288
     *
289
     * @return string
290
     */
291
    public function getItemTax($itemName)
292
    {
293
        return $this->getItemProperty($itemName, 'tax');
294
    }
295
296
    /**
297
     * @param string $itemName
298
     *
299
     * @return string
300
     */
301
    public function getItemTotal($itemName)
302
    {
303
        return $this->getItemProperty($itemName, 'total');
304
    }
305
    
306
    /**
307
     * {@inheritdoc}
308
     */
309
    public function hasCancelButton()
310
    {
311
        return $this->getDocument()->hasButton('Cancel');
312
    }
313
314
    /**
315
     * {@inheritdoc}
316
     */
317
    public function getOrderState()
318
    {
319
        return $this->getElement('order_state')->getText();
320
    }
321
322
    public function cancelOrder()
323
    {
324
        $this->getDocument()->pressButton('Cancel');
325
    }
326
327
    public function deleteOrder()
328
    {
329
        $this->getDocument()->pressButton('Delete');
330
    }
331
332
    /**
333
     * {@inheritdoc}
334
     */
335
    public function hasNote($note)
336
    {
337
        $orderNotesElement = $this->getElement('order_notes');
338
339
        return $orderNotesElement->getText() === $note;
340
    }
341
342
    /**
343
     * {@inheritdoc}
344
     */
345
    public function getRouteName()
346
    {
347
        return 'sylius_admin_order_show';
348
    }
349
350
    /**
351
     * {@inheritdoc}
352
     */
353
    protected function getDefinedElements()
354
    {
355
        return array_merge(parent::getDefinedElements(), [
356
            'billing_address' => '#billing-address',
357
            'customer' => '#customer',
358
            'items_total' => '#items-total',
359
            'payments' => '#payments',
360
            'promotion_discounts' => '#promotion-discounts',
361
            'promotion_total' => '#promotion-total',
362
            'shipments' => '#shipments',
363
            'shipping_address' => '#shipping-address',
364
            'shipping_charges' => '#shipping-charges',
365
            'shipping_total' => '#shipping-total',
366
            'table' => '.table',
367
            'tax_total' => '#tax-total',
368
            'taxes' => '#taxes',
369
            'total' => '#total',
370
            'order_state' => 'div.sub.header > span.ui.label',
371
            'order_notes' => '#sylius-order-notes',
372
        ]);
373
    }
374
375
    /**
376
     * @return TableAccessorInterface
377
     */
378
    protected function getTableAccessor()
379
    {
380
        return $this->tableAccessor;
381
    }
382
383
    /**
384
     * @param string $elementText
385
     * @param string $customerName
386
     * @param string $street
387
     * @param string $postcode
388
     * @param string $city
389
     * @param string $countryName
390
     *
391
     * @return bool
392
     */
393
    private function hasAddress($elementText, $customerName, $street, $postcode, $city, $countryName)
394
    {
395
        return
396
            (stripos($elementText, $customerName) !== false) &&
397
            (stripos($elementText, $street) !== false) &&
398
            (stripos($elementText, $city) !== false) &&
399
            (stripos($elementText, $countryName.' '.$postcode) !== false)
400
        ;
401
    }
402
403
    /**
404
     * @param string $itemName
405
     * @param string $property
406
     *
407
     * @return string
408
     */
409
    private function getItemProperty($itemName, $property)
410
    {
411
        $rows = $this->tableAccessor->getRowsWithFields(
412
            $this->getElement('table'),
413
            ['item' => $itemName]
414
        );
415
416
        return $rows[0]->find('css', '.'.$property)->getText();
417
    }
418
419
    /**
420
     * @param OrderInterface $order
421
     *
422
     * @return NodeElement|null
423
     */
424
    private function getLastOrderPaymentElement(OrderInterface $order)
425
    {
426
        $payment = $order->getPayments()->last();
427
428
        $paymentStateElements = $this->getElement('payments')->findAll('css', sprintf('span.ui.label:contains(\'%s\')', ucfirst($payment->getState())));
429
        $paymentStateElement = end($paymentStateElements);
430
431
        return $paymentStateElement->getParent()->getParent();
432
    }
433
434
    /**
435
     * @param OrderInterface $order
436
     *
437
     * @return NodeElement|null
438
     */
439
    private function getLastOrderShipmentElement(OrderInterface $order)
440
    {
441
        $shipment = $order->getShipments()->last();
442
443
        $shipmentStateElements = $this->getElement('shipments')->findAll('css', sprintf('span.ui.label:contains(\'%s\')', ucfirst($shipment->getState())));
444
        $shipmentStateElement = end($shipmentStateElements);
445
446
        return $shipmentStateElement->getParent()->getParent();
447
    }
448
}
449