Completed
Push — master ( cfa808...648607 )
by Paweł
63:01 queued 48:26
created

ShowPage::getDefinedElements()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 11
rs 9.4285
cc 1
eloc 8
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\Shop\Account\Order;
13
14
use Behat\Mink\Session;
15
use Sylius\Behat\Page\SymfonyPage;
16
use Sylius\Behat\Service\Accessor\TableAccessorInterface;
17
use Symfony\Component\Routing\RouterInterface;
18
19
/**
20
 * @author Grzegorz Sadowski <[email protected]>
21
 */
22
class ShowPage extends SymfonyPage implements ShowPageInterface
23
{
24
    /**
25
     * @var TableAccessorInterface
26
     */
27
    private $tableAccessor;
28
29
    /**
30
     * @param Session $session
31
     * @param array $parameters
32
     * @param RouterInterface $router
33
     * @param TableAccessorInterface $tableAccessor
34
     */
35
    public function __construct(
36
        Session $session,
37
        array $parameters,
38
        RouterInterface $router,
39
        TableAccessorInterface $tableAccessor
40
    ) {
41
        parent::__construct($session, $parameters, $router);
42
43
        $this->tableAccessor = $tableAccessor;
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49
    public function getRouteName()
50
    {
51
        return 'sylius_shop_account_order_show';
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57
    public function getNumber()
58
    {
59
        $numberText = $this->getElement('number')->find('css', 'strong')->getText();
60
        $numberText = str_replace('#', '', $numberText);
61
62
        return $numberText;
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68
    public function hasShippingAddress($customerName, $street, $postcode, $city, $countryName)
69
    {
70
        $shippingAddressText = $this->getElement('shipping_address')->getText();
71
72
        return $this->hasAddress($shippingAddressText, $customerName, $street, $postcode, $city, $countryName);
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78
    public function hasBillingAddress($customerName, $street, $postcode, $city, $countryName)
79
    {
80
        $billingAddressText = $this->getElement('billing_address')->getText();
81
82
        return $this->hasAddress($billingAddressText, $customerName, $street, $postcode, $city, $countryName);
83
    }
84
85
    /**
86
     * {@inheritdoc}
87
     */
88
    public function getTotal()
89
    {
90
        $totalElement = $this->getElement('total');
91
92
        return trim(str_replace('Total:', '', $totalElement->getText()));
93
    }
94
95
    /**
96
     * {@inheritdoc}
97
     */
98
    public function getSubtotal()
99
    {
100
        $totalElement = $this->getElement('subtotal');
101
102
        return trim(str_replace('Subtotal:', '', $totalElement->getText()));
103
    }
104
105
    /**
106
     * {@inheritdoc}
107
     */
108
    public function countItems()
109
    {
110
        return $this->tableAccessor->countTableBodyRows($this->getElement('order_items'));
111
    }
112
113
    /**
114
     * {@inheritdoc}
115
     */
116
    public function isProductInTheList($name)
117
    {
118
        try {
119
            $rows = $this->tableAccessor->getRowsWithFields(
120
                $this->getElement('order_items'),
121
                ['item' => $name]
122
            );
123
124
            return 1 === count($rows);
125
126
        } catch (\InvalidArgumentException $exception) {
127
            return false;
128
        }
129
    }
130
131
    /**
132
     * {@inheritdoc}
133
     */
134
    protected function getDefinedElements()
135
    {
136
        return array_merge(parent::getDefinedElements(), [
137
            'billing_address' => '#billing-address',
138
            'number' => '#number',
139
            'order_items' => '#sylius-customer-order-items',
140
            'shipping_address' => '#shipping-address',
141
            'subtotal' => '#subtotal',
142
            'total' => '#total',
143
        ]);
144
    }
145
146
    /**
147
     * @param string $elementText
148
     * @param string $customerName
149
     * @param string $street
150
     * @param string $postcode
151
     * @param string $city
152
     * @param string $countryName
153
     *
154
     * @return bool
155
     */
156
    private function hasAddress($elementText, $customerName, $street, $postcode, $city, $countryName)
157
    {
158
        return
159
            (stripos($elementText, $customerName) !== false) &&
160
            (stripos($elementText, $street) !== false) &&
161
            (stripos($elementText, $city) !== false) &&
162
            (stripos($elementText, $countryName.' '.$postcode) !== false)
163
        ;
164
    }
165
}
166