Completed
Push — master ( 30cb53...645fea )
by Paweł
36:15 queued 26:35
created

UpdatePage::getScope()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
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\Admin\Zone;
13
14
use Behat\Mink\Element\NodeElement;
15
use Behat\Mink\Exception\ElementNotFoundException;
16
use Sylius\Behat\Behaviour\ChecksCodeImmutability;
17
use Sylius\Behat\Behaviour\NamesIt;
18
use Sylius\Behat\Page\Admin\Crud\UpdatePage as BaseUpdatePage;
19
use Sylius\Component\Addressing\Model\ZoneMemberInterface;
20
21
/**
22
 * @author Arkadiusz Krakowiak <[email protected]>
23
 */
24
class UpdatePage extends BaseUpdatePage implements UpdatePageInterface
25
{
26
    use NamesIt;
27
    use ChecksCodeImmutability;
28
29
    /**
30
     * {@inheritdoc}
31
     */
32
    public function countMembers()
33
    {
34
        $selectedZoneMembers = $this->getSelectedZoneMembers();
35
36
        return count($selectedZoneMembers);
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    public function getScope()
43
    {
44
        return $this->getElement('scope')->getValue();
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->getElement('scope')->getValue(); (string|boolean|array) is incompatible with the return type declared by the interface Sylius\Behat\Page\Admin\...PageInterface::getScope 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...
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50
    public function hasMember(ZoneMemberInterface $zoneMember)
51
    {
52
        $selectedZoneMembers = $this->getSelectedZoneMembers();
53
54
        foreach ($selectedZoneMembers as $selectedZoneMember) {
55
            if ($selectedZoneMember->getValue() === $zoneMember->getCode()) {
56
                return true;
57
            }
58
        }
59
60
        return false;
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66
    public function removeMember(ZoneMemberInterface $zoneMember)
67
    {
68
        $zoneMembers = $this->getElement('zone_members');
69
        $items = $zoneMembers->findAll('css', 'div[data-form-collection="item"]');
70
71
        /** @var NodeElement $item */
72
        foreach ($items as $item) {
73
            $selectedItem = $item->find('css', 'option[selected="selected"]');
74
75
            if (null === $selectedItem) {
76
                throw new ElementNotFoundException($this->getDriver(), 'selected option', 'css', 'option[selected="selected"]');
77
            }
78
79
            if ($selectedItem->getValue() === $zoneMember->getCode()) {
80
                $this->getDeleteButtonForCollectionItem($item)->click();
81
82
                break;
83
            }
84
        }
85
    }
86
87
    /**
88
     * @return NodeElement
89
     *
90
     * @throws ElementNotFoundException
91
     */
92
    protected function getCodeElement()
93
    {
94
        return $this->getElement('code');
95
    }
96
97
    /**
98
     * {@inheritdoc}
99
     */
100
    protected function getDefinedElements()
101
    {
102
        return array_merge(parent::getDefinedElements(), [
103
            'code' => '#sylius_zone_code',
104
            'member' => '.one.field',
105
            'name' => '#sylius_zone_name',
106
            'scope' => '#sylius_zone_scope',
107
            'type' => '#sylius_zone_type',
108
            'zone_members' => '#sylius_zone_members',
109
        ]);
110
    }
111
112
    /**
113
     * @param NodeElement $item
114
     *
115
     * @return NodeElement
116
     *
117
     * @throws ElementNotFoundException
118
     */
119
    private function getDeleteButtonForCollectionItem(NodeElement $item)
120
    {
121
        $deleteButton = $item->find('css', 'a[data-form-collection="delete"]');
122
        if (null === $deleteButton) {
123
            throw new ElementNotFoundException($this->getDriver(), 'link', 'css', 'a[data-form-collection="delete"]');
124
        }
125
126
        return $deleteButton;
127
    }
128
129
    /**
130
     * @return \Behat\Mink\Element\NodeElement[]
131
     *
132
     * @throws ElementNotFoundException
133
     */
134
    private function getSelectedZoneMembers()
135
    {
136
        $zoneMembers = $this->getElement('zone_members');
137
        $selectedZoneMembers = $zoneMembers->findAll('css', 'option[selected="selected"]');
138
139
        return $selectedZoneMembers;
140
    }
141
}
142