Completed
Push — master ( 0a1951...1a638f )
by Paweł
22:32 queued 13:24
created

UpdatePage::getSelectedProvince()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
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\AddressBook;
13
14
use Behat\Mink\Element\NodeElement;
15
use Sylius\Behat\Page\SymfonyPage;
16
17
/**
18
 * @author Jan Góralski <[email protected]>
19
 */
20
final class UpdatePage extends SymfonyPage implements UpdatePageInterface
21
{
22
    /**
23
     * {@inheritdoc}
24
     */
25
    public function getRouteName()
26
    {
27
        return 'sylius_shop_account_address_book_update';
28
    }
29
30
    /**
31
     * {@inheritdoc}
32
     */
33
    public function fillField($field, $value)
34
    {
35
        $field = $this->getElement(str_replace(' ', '_', strtolower($field)));
36
        $field->setValue($value);
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    public function getSpecifiedProvince()
43
    {
44
        $this->waitForElement(5, 'province_name');
45
46
        return $this->getElement('province_name')->getValue();
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->getElement...nce_name')->getValue(); (string|boolean|array) is incompatible with the return type declared by the interface Sylius\Behat\Page\Shop\A...e::getSpecifiedProvince of type string.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52
    public function getSelectedProvince()
53
    {
54
        $this->waitForElement(5, 'province_code');
55
56
        return $this->getElement('selected_province')->getText();
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62
    public function specifyProvince($name)
63
    {
64
        $this->waitForElement(5, 'province_name');
65
66
        $province = $this->getElement('province_name');
67
        $province->setValue($name);
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73
    public function selectProvince($name)
74
    {
75
        $this->waitForElement(5, 'province_code');
76
77
        $province = $this->getElement('province_code');
78
        $province->selectOption($name);
79
    }
80
81
    /**
82
     * {@inheritdoc}
83
     */
84
    public function selectCountry($name)
85
    {
86
        $country = $this->getElement('country');
87
        $country->selectOption($name);
88
    }
89
90
    public function saveChanges()
91
    {
92
        $this->getElement('save_button')->press();
93
    }
94
95
    /**
96
     * {@inheritdoc}
97
     */
98
    protected function getDefinedElements()
99
    {
100
        return array_merge(parent::getDefinedElements(), [
101
            'city' => '#sylius_address_city',
102
            'country' => '#sylius_address_countryCode',
103
            'first_name' => '#sylius_address_firstName',
104
            'last_name' => '#sylius_address_lastName',
105
            'postcode' => '#sylius_address_postcode',
106
            'province_name' => 'input[name="sylius_address[provinceName]"]',
107
            'province_code' => 'select[name="sylius_address[provinceCode]"]',
108
            'save_button' => 'button:contains("Save changes")',
109
            'selected_province' => 'select[name="sylius_address[provinceCode]"] option[selected="selected"]',
110
            'street' => '#sylius_address_street',
111
        ]);
112
    }
113
114
    /**
115
     * @param int $timeout
116
     * @param string $elementName
117
     */
118
    private function waitForElement($timeout, $elementName)
119
    {
120
        $this->getDocument()->waitFor($timeout, function () use ($elementName) {
121
            return $this->hasElement($elementName);
122
        });
123
    }
124
}
125