Completed
Push — master ( 470ea1...92fad8 )
by Paweł
75:16 queued 59:44
created

SummaryPage::hasShippingAddress()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
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\Shop\Checkout;
13
14
use Sylius\Behat\Page\SymfonyPage;
15
use Sylius\Component\Core\Model\AddressInterface;
16
use Symfony\Component\Intl\Intl;
17
18
/**
19
 * @author Mateusz Zalewski <[email protected]>
20
 */
21
class SummaryPage extends SymfonyPage implements SummaryPageInterface
22
{
23
    /**
24
     * {@inheritdoc}
25
     */
26
    public function getRouteName()
27
    {
28
        return 'sylius_shop_checkout_summary';
29
    }
30
31
    /**
32
     * {@inheritdoc}
33
     */
34
    public function hasShippingAddress(AddressInterface $address)
35
    {
36
        $shippingAddress = $this->getElement('shipping_address')->getText();
37
38
        return $this->isAddressValid($shippingAddress, $address);
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44
    public function hasBillingAddress(AddressInterface $address)
45
    {
46
        $billingAddress = $this->getElement('billing_address')->getText();
47
48
        return $this->isAddressValid($billingAddress, $address);
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    protected function getDefinedElements()
55
    {
56
        return array_merge(parent::getDefinedElements(), [
57
            'billing_address' => '#addresses div:contains("Billing address") address',
58
            'shipping_address' => '#addresses div:contains("Shipping address") address',
59
        ]);
60
    }
61
62
    /**
63
     * @param string $displayedAddress
64
     * @param AddressInterface $address
65
     *
66
     * @return bool
67
     */
68
    private function isAddressValid($displayedAddress, AddressInterface $address)
69
    {
70
        return
71
            $this->hasAddressPart($displayedAddress, $address->getCompany(), true) &&
72
            $this->hasAddressPart($displayedAddress, $address->getFirstName()) &&
73
            $this->hasAddressPart($displayedAddress, $address->getLastName()) &&
74
            $this->hasAddressPart($displayedAddress, $address->getPhoneNumber(), true) &&
75
            $this->hasAddressPart($displayedAddress, $address->getStreet()) &&
76
            $this->hasAddressPart($displayedAddress, $address->getCity()) &&
77
            $this->hasAddressPart($displayedAddress, $address->getProvinceCode(), true) &&
78
            $this->hasAddressPart($displayedAddress, $this->getCountryName($address->getCountryCode())) &&
79
            $this->hasAddressPart($displayedAddress, $address->getPostcode())
80
        ;
81
    }
82
83
    /**
84
     * @param string $address
85
     * @param string $addressPart
86
     *
87
     * @return bool
88
     */
89
    private function hasAddressPart($address, $addressPart, $optional = false)
90
    {
91
        if ($optional && null === $addressPart) {
92
            return true;
93
        }
94
95
        return false !== strpos($address, $addressPart);
96
    }
97
98
    /**
99
     * @param string $countryCode
100
     *
101
     * @return string
102
     */
103
    private function getCountryName($countryCode)
104
    {
105
        return strtoupper(Intl::getRegionBundle()->getCountryName($countryCode, 'en'));
106
    }
107
}
108