Completed
Push — master ( 684346...f34924 )
by Paweł
59:47 queued 43:24
created

SummaryPage::hasTaxTotal()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
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\Shop\Checkout;
13
14
use Behat\Mink\Session;
15
use Behat\Mink\Element\NodeElement;
16
use Sylius\Behat\Page\SymfonyPage;
17
use Sylius\Behat\Service\Accessor\TableAccessorInterface;
18
use Sylius\Component\Core\Model\AddressInterface;
19
use Sylius\Component\Core\Model\ProductInterface;
20
use Symfony\Component\Intl\Intl;
21
use Symfony\Component\Routing\RouterInterface;
22
23
/**
24
 * @author Mateusz Zalewski <[email protected]>
25
 */
26
class SummaryPage extends SymfonyPage implements SummaryPageInterface
27
{
28
    /**
29
     * @var TableAccessorInterface
30
     */
31
    private $tableAccessor;
32
33
    /**
34
     * @param Session $session
35
     * @param array $parameters
36
     * @param RouterInterface $router
37
     * @param TableAccessorInterface $tableAccessor
38
     */
39
    public function __construct(
40
        Session $session,
41
        array $parameters,
42
        RouterInterface $router,
43
        TableAccessorInterface $tableAccessor
44
    ) {
45
        parent::__construct($session, $parameters, $router);
46
47
        $this->tableAccessor = $tableAccessor;
48
    }
49
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    public function getRouteName()
55
    {
56
        return 'sylius_shop_checkout_summary';
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62
    public function hasItemWithProductAndQuantity($productName, $quantity)
63
    {
64
        $table = $this->getElement('items_table');
65
66
        try {
67
            $this->tableAccessor->getRowWithFields($table, ['item' => $productName, 'qty' => $quantity]);
68
        } catch (\InvalidArgumentException $exception) {
69
            return false;
70
        }
71
72
        return true;
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78
    public function hasShippingAddress(AddressInterface $address)
79
    {
80
        $shippingAddress = $this->getElement('shipping_address')->getText();
81
82
        return $this->isAddressValid($shippingAddress, $address);
83
    }
84
85
    /**
86
     * {@inheritdoc}
87
     */
88
    public function hasBillingAddress(AddressInterface $address)
89
    {
90
        $billingAddress = $this->getElement('billing_address')->getText();
91
92
        return $this->isAddressValid($billingAddress, $address);
93
    }
94
95
    /**
96
     * {@inheritdoc}
97
     */
98
    public function hasProductDiscountedUnitPriceBy(ProductInterface $product, $amount)
99
    {
100
        $productRowElement = $this->getProductRowElement($product);
101
        $discountedPriceElement = $productRowElement->find('css', 'td > s');
102
        if (null === $discountedPriceElement) {
103
            return false;
104
        }
105
        $priceElement = $discountedPriceElement->getParent();
106
        $prices = explode(' ', $priceElement->getText());
107
        $priceWithoutDiscount = $this->getPriceFromString($prices[0]);
108
        $priceWithDiscount = $this->getPriceFromString($prices[1]);
109
        $discount = $priceWithoutDiscount - $priceWithDiscount;
110
111
        return $discount === $amount;
112
    }
113
114
    /**
115
     * {@inheritdoc}
116
     */
117
    public function hasOrderTotal($total)
118
    {
119
        if (!$this->hasElement('order_total')) {
120
            return false;
121
        }
122
123
        return $this->getTotalFromString($this->getElement('order_total')->getText()) === $total;
124
    }
125
126
    /**
127
     * {@inheritdoc}
128
     */
129
    public function hasTaxTotal($taxTotal)
130
    {
131
        return false !== strpos($this->getElement('tax_total')->getText(), $taxTotal);
132
    }
133
134
    /**
135
     * {@inheritdoc}
136
     */
137
    protected function getDefinedElements()
138
    {
139
        return array_merge(parent::getDefinedElements(), [
140
            'billing_address' => '#addresses div:contains("Billing address") address',
141
            'shipping_address' => '#addresses div:contains("Shipping address") address',
142
            'items_table' => '#items table',
143
            'product_row' => 'tbody tr:contains("%name%")',
144
            'order_total' => 'td:contains("Total")',
145
            'tax_total' => '#tax-total',
146
        ]);
147
    }
148
149
    /**
150
     * @param ProductInterface $product
151
     *
152
     * @return NodeElement
153
     */
154
    private function getProductRowElement(ProductInterface $product)
155
    {
156
        return $this->getElement('product_row', ['%name%' => $product->getName()]);
157
    }
158
159
    /**
160
     * @param string $displayedAddress
161
     * @param AddressInterface $address
162
     *
163
     * @return bool
164
     */
165
    private function isAddressValid($displayedAddress, AddressInterface $address)
166
    {
167
        return
168
            $this->hasAddressPart($displayedAddress, $address->getCompany(), true) &&
169
            $this->hasAddressPart($displayedAddress, $address->getFirstName()) &&
170
            $this->hasAddressPart($displayedAddress, $address->getLastName()) &&
171
            $this->hasAddressPart($displayedAddress, $address->getPhoneNumber(), true) &&
172
            $this->hasAddressPart($displayedAddress, $address->getStreet()) &&
173
            $this->hasAddressPart($displayedAddress, $address->getCity()) &&
174
            $this->hasAddressPart($displayedAddress, $address->getProvinceCode(), true) &&
175
            $this->hasAddressPart($displayedAddress, $this->getCountryName($address->getCountryCode())) &&
176
            $this->hasAddressPart($displayedAddress, $address->getPostcode())
177
        ;
178
    }
179
180
    /**
181
     * @param string $address
182
     * @param string $addressPart
183
     *
184
     * @return bool
185
     */
186
    private function hasAddressPart($address, $addressPart, $optional = false)
187
    {
188
        if ($optional && null === $addressPart) {
189
            return true;
190
        }
191
192
        return false !== strpos($address, $addressPart);
193
    }
194
195
    /**
196
     * @param string $countryCode
197
     *
198
     * @return string
199
     */
200
    private function getCountryName($countryCode)
201
    {
202
        return strtoupper(Intl::getRegionBundle()->getCountryName($countryCode, 'en'));
203
    }
204
205
    /**
206
     * @param string $price
207
     *
208
     * @return int
209
     */
210
    private function getPriceFromString($price)
211
    {
212
        return (int) round((str_replace(['€', '£', '$'], '', $price) * 100), 2);
213
    }
214
215
    /**
216
     * @param string $total
217
     * 
218
     * @return int
219
     */
220
    private function getTotalFromString($total)
221
    {
222
        $total = str_replace('Total:', '', $total);
223
224
        return $this->getPriceFromString($total);
225
    }
226
}
227