Completed
Push — master ( bb2d64...78fd01 )
by Paweł
24s
created

ShowPage::isProductInTheList()   B

Complexity

Conditions 5
Paths 10

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 8.6737
c 0
b 0
f 0
cc 5
eloc 14
nc 10
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\Shop\Account\Order;
15
16
use Behat\Mink\Session;
17
use Sylius\Behat\Page\SymfonyPage;
18
use Sylius\Behat\Service\Accessor\TableAccessorInterface;
19
use Symfony\Component\Routing\RouterInterface;
20
21
class ShowPage extends SymfonyPage implements ShowPageInterface
22
{
23
    /**
24
     * @var TableAccessorInterface
25
     */
26
    private $tableAccessor;
27
28
    /**
29
     * @param Session $session
30
     * @param array $parameters
31
     * @param RouterInterface $router
32
     * @param TableAccessorInterface $tableAccessor
33
     */
34
    public function __construct(
35
        Session $session,
36
        array $parameters,
37
        RouterInterface $router,
38
        TableAccessorInterface $tableAccessor
39
    ) {
40
        parent::__construct($session, $parameters, $router);
41
42
        $this->tableAccessor = $tableAccessor;
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48
    public function getRouteName()
49
    {
50
        return 'sylius_shop_account_order_show';
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56
    public function getNumber()
57
    {
58
        $numberText = $this->getElement('number')->getText();
59
        $numberText = str_replace('#', '', $numberText);
60
61
        return $numberText;
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67
    public function hasShippingAddress($customerName, $street, $postcode, $city, $countryName)
68
    {
69
        $shippingAddressText = $this->getElement('shipping_address')->getText();
70
71
        return $this->hasAddress($shippingAddressText, $customerName, $street, $postcode, $city, $countryName);
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77
    public function hasBillingAddress($customerName, $street, $postcode, $city, $countryName)
78
    {
79
        $billingAddressText = $this->getElement('billing_address')->getText();
80
81
        return $this->hasAddress($billingAddressText, $customerName, $street, $postcode, $city, $countryName);
82
    }
83
84
    /**
85
     * {@inheritdoc}
86
     */
87
    public function getTotal()
88
    {
89
        $totalElement = $this->getElement('total');
90
91
        return trim(str_replace('Total:', '', $totalElement->getText()));
92
    }
93
94
    /**
95
     * {@inheritdoc}
96
     */
97
    public function getSubtotal()
98
    {
99
        $totalElement = $this->getElement('subtotal');
100
101
        return trim(str_replace('Subtotal:', '', $totalElement->getText()));
102
    }
103
104
    /**
105
     * {@inheritdoc}
106
     */
107
    public function countItems()
108
    {
109
        return $this->tableAccessor->countTableBodyRows($this->getElement('order_items'));
110
    }
111
112
    /**
113
     * {@inheritdoc}
114
     */
115
    public function getPaymentPrice()
116
    {
117
        $paymentsPrice = $this->getElement('payments')->find('css', 'p');
118
119
        return $paymentsPrice->getText();
120
    }
121
122
    /**
123
     * {@inheritdoc}
124
     */
125
    public function isProductInTheList(string $productName): bool
126
    {
127
        try {
128
            $table = $this->getElement('order_items');
129
            $rows = $this->tableAccessor->getRowsWithFields(
130
                $table,
131
                ['item' => $productName]
132
            );
133
134
            foreach ($rows as $row) {
135
                $field = $this->tableAccessor->getFieldFromRow($table, $row, 'item');
136
                $name = $field->find('css', '.sylius-product-name');
137
                if (null !== $name && $name->getText() === $productName) {
138
                    return true;
139
                }
140
            }
141
142
            return false;
143
        } catch (\InvalidArgumentException $exception) {
144
            return false;
145
        }
146
    }
147
148
    /**
149
     * {@inheritdoc}
150
     */
151
    public function getItemPrice()
152
    {
153
        return $this->getElement('product_price')->getText();
154
    }
155
156
    /**
157
     * {@inheritdoc}
158
     */
159
    public function hasShippingProvinceName($provinceName)
160
    {
161
        $shippingAddressText = $this->getElement('shipping_address')->getText();
162
163
        return false !== stripos($shippingAddressText, $provinceName);
164
    }
165
166
    /**
167
     * {@inheritdoc}
168
     */
169
    public function hasBillingProvinceName($provinceName)
170
    {
171
        $billingAddressText = $this->getElement('billing_address')->getText();
172
173
        return false !== stripos($billingAddressText, $provinceName);
174
    }
175
176
    /**
177
     * {@inheritdoc}
178
     */
179
    protected function getDefinedElements()
180
    {
181
        return array_merge(parent::getDefinedElements(), [
182
            'billing_address' => '#sylius-billing-address',
183
            'shipping_address' => '#sylius-shipping-address',
184
            'number' => '#number',
185
            'order_items' => '#sylius-order',
186
            'payments' => '#sylius-payments',
187
            'product_price' => '#sylius-order td:nth-child(2)',
188
            'subtotal' => '#subtotal',
189
            'total' => '#total',
190
        ]);
191
    }
192
193
    /**
194
     * @param string $elementText
195
     * @param string $customerName
196
     * @param string $street
197
     * @param string $postcode
198
     * @param string $city
199
     * @param string $countryName
200
     *
201
     * @return bool
202
     */
203
    private function hasAddress($elementText, $customerName, $street, $postcode, $city, $countryName)
204
    {
205
        return
206
            (stripos($elementText, $customerName) !== false) &&
207
            (stripos($elementText, $street) !== false) &&
208
            (stripos($elementText, $city . ', ' . $postcode) !== false) &&
209
            (stripos($elementText, $countryName) !== false)
210
        ;
211
    }
212
}
213