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(); |
|
|
|
|
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
|
|
|
|
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:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.