Completed
Push — master ( 95ea58...1d26b8 )
by Paweł
66:37 queued 50:00
created

SummaryPage::getProductRowElement()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
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
    protected function getDefinedElements()
130
    {
131
        return array_merge(parent::getDefinedElements(), [
132
            'billing_address' => '#addresses div:contains("Billing address") address',
133
            'shipping_address' => '#addresses div:contains("Shipping address") address',
134
            'items_table' => '#items table',
135
            'product_row' => 'tbody tr:contains("%name%")',
136
            'order_total' => 'td:contains("Total")',
137
        ]);
138
    }
139
140
    /**
141
     * @param ProductInterface $product
142
     *
143
     * @return NodeElement
144
     */
145
    private function getProductRowElement(ProductInterface $product)
146
    {
147
        return $this->getElement('product_row', ['%name%' => $product->getName()]);
148
    }
149
150
    /**
151
     * @param string $displayedAddress
152
     * @param AddressInterface $address
153
     *
154
     * @return bool
155
     */
156
    private function isAddressValid($displayedAddress, AddressInterface $address)
157
    {
158
        return
159
            $this->hasAddressPart($displayedAddress, $address->getCompany(), true) &&
160
            $this->hasAddressPart($displayedAddress, $address->getFirstName()) &&
161
            $this->hasAddressPart($displayedAddress, $address->getLastName()) &&
162
            $this->hasAddressPart($displayedAddress, $address->getPhoneNumber(), true) &&
163
            $this->hasAddressPart($displayedAddress, $address->getStreet()) &&
164
            $this->hasAddressPart($displayedAddress, $address->getCity()) &&
165
            $this->hasAddressPart($displayedAddress, $address->getProvinceCode(), true) &&
166
            $this->hasAddressPart($displayedAddress, $this->getCountryName($address->getCountryCode())) &&
167
            $this->hasAddressPart($displayedAddress, $address->getPostcode())
168
        ;
169
    }
170
171
    /**
172
     * @param string $address
173
     * @param string $addressPart
174
     *
175
     * @return bool
176
     */
177
    private function hasAddressPart($address, $addressPart, $optional = false)
178
    {
179
        if ($optional && null === $addressPart) {
180
            return true;
181
        }
182
183
        return false !== strpos($address, $addressPart);
184
    }
185
186
    /**
187
     * @param string $countryCode
188
     *
189
     * @return string
190
     */
191
    private function getCountryName($countryCode)
192
    {
193
        return strtoupper(Intl::getRegionBundle()->getCountryName($countryCode, 'en'));
194
    }
195
196
    /**
197
     * @param string $price
198
     *
199
     * @return int
200
     */
201
    private function getPriceFromString($price)
202
    {
203
        return (int) round((str_replace(['€', '£', '$'], '', $price) * 100), 2);
204
    }
205
206
    /**
207
     * @param string $total
208
     * 
209
     * @return int
210
     */
211
    private function getTotalFromString($total)
212
    {
213
        $total = str_replace('Total:', '', $total);
214
215
        return $this->getPriceFromString($total);
216
    }
217
}
218