These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
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 | declare(strict_types=1); |
||
13 | |||
14 | namespace Sylius\Behat\Page\Admin\Product; |
||
15 | |||
16 | use Behat\Mink\Driver\Selenium2Driver; |
||
17 | use Behat\Mink\Element\NodeElement; |
||
18 | use Sylius\Behat\Behaviour\ChecksCodeImmutability; |
||
19 | use Sylius\Behat\Page\Admin\Crud\UpdatePage as BaseUpdatePage; |
||
20 | use Sylius\Component\Taxonomy\Model\TaxonInterface; |
||
21 | use Webmozart\Assert\Assert; |
||
22 | |||
23 | class UpdateConfigurableProductPage extends BaseUpdatePage implements UpdateConfigurableProductPageInterface |
||
24 | { |
||
25 | use ChecksCodeImmutability; |
||
26 | |||
27 | /** @var array */ |
||
28 | private $imageUrls = []; |
||
29 | |||
30 | /** |
||
31 | * {@inheritdoc} |
||
32 | */ |
||
33 | public function nameItIn($name, $localeCode) |
||
34 | { |
||
35 | $this->getDocument()->fillField( |
||
36 | sprintf('sylius_product_translations_%s_name', $localeCode), $name |
||
37 | ); |
||
38 | } |
||
39 | |||
40 | /** |
||
41 | * {@inheritdoc} |
||
42 | */ |
||
43 | public function isProductOptionChosen($option) |
||
44 | { |
||
45 | return $this->getElement('options')->find('named', ['option', $option])->hasAttribute('selected'); |
||
46 | } |
||
47 | |||
48 | /** |
||
49 | * @return bool |
||
50 | */ |
||
51 | public function isProductOptionsDisabled() |
||
52 | { |
||
53 | return 'disabled' === $this->getElement('options')->getAttribute('disabled'); |
||
54 | } |
||
55 | |||
56 | /** |
||
57 | * {@inheritdoc} |
||
58 | */ |
||
59 | public function isMainTaxonChosen($taxonName) |
||
60 | { |
||
61 | $this->openTaxonBookmarks(); |
||
62 | Assert::notNull($this->getDocument()->find('css', '.search > .text')); |
||
63 | |||
64 | return $taxonName === $this->getDocument()->find('css', '.search > .text')->getText(); |
||
65 | } |
||
66 | |||
67 | /** |
||
68 | * {@inheritdoc} |
||
69 | */ |
||
70 | public function selectMainTaxon(TaxonInterface $taxon) |
||
71 | { |
||
72 | $this->openTaxonBookmarks(); |
||
73 | |||
74 | Assert::isInstanceOf($this->getDriver(), Selenium2Driver::class); |
||
75 | |||
76 | $this->getDriver()->executeScript(sprintf('$(\'input.search\').val(\'%s\')', $taxon->getName())); |
||
77 | $this->getElement('search')->click(); |
||
78 | $this->getElement('search')->waitFor(10, function () { |
||
79 | return $this->hasElement('search_item_selected'); |
||
80 | }); |
||
81 | $itemSelected = $this->getElement('search_item_selected'); |
||
82 | $itemSelected->click(); |
||
83 | } |
||
84 | |||
85 | /** |
||
86 | * {@inheritdoc} |
||
87 | */ |
||
88 | public function checkChannel($channelName) |
||
89 | { |
||
90 | $this->getElement('channel_checkbox', ['%channel%' => $channelName])->check(); |
||
91 | } |
||
92 | |||
93 | /** |
||
94 | * {@inheritdoc} |
||
95 | */ |
||
96 | public function isImageWithTypeDisplayed($type) |
||
97 | { |
||
98 | $imageElement = $this->getImageElementByType($type); |
||
99 | |||
100 | $imageUrl = $imageElement ? $imageElement->find('css', 'img')->getAttribute('src') : $this->provideImageUrlForType($type); |
||
101 | if (null === $imageElement && null === $imageUrl) { |
||
102 | return false; |
||
103 | } |
||
104 | |||
105 | $this->getDriver()->visit($imageUrl); |
||
106 | $pageText = $this->getDocument()->getText(); |
||
107 | $this->getDriver()->back(); |
||
108 | |||
109 | return false === stripos($pageText, '404 Not Found'); |
||
110 | } |
||
111 | |||
112 | /** |
||
113 | * {@inheritdoc} |
||
114 | */ |
||
115 | public function attachImage($path, $type = null) |
||
116 | { |
||
117 | $this->clickTabIfItsNotActive('media'); |
||
118 | |||
119 | $filesPath = $this->getParameter('files_path'); |
||
120 | |||
121 | $this->getDocument()->clickLink('Add'); |
||
122 | |||
123 | $imageForm = $this->getLastImageElement(); |
||
124 | if (null !== $type) { |
||
125 | $imageForm->fillField('Type', $type); |
||
126 | } |
||
127 | |||
128 | $imageForm->find('css', 'input[type="file"]')->attachFile($filesPath . $path); |
||
129 | } |
||
130 | |||
131 | /** |
||
132 | * {@inheritdoc} |
||
133 | */ |
||
134 | public function changeImageWithType($type, $path) |
||
135 | { |
||
136 | $filesPath = $this->getParameter('files_path'); |
||
137 | |||
138 | $imageForm = $this->getImageElementByType($type); |
||
139 | $imageForm->find('css', 'input[type="file"]')->attachFile($filesPath . $path); |
||
140 | } |
||
141 | |||
142 | /** |
||
143 | * {@inheritdoc} |
||
144 | */ |
||
145 | public function removeImageWithType($type) |
||
146 | { |
||
147 | $this->clickTabIfItsNotActive('media'); |
||
148 | |||
149 | $imageElement = $this->getImageElementByType($type); |
||
150 | $imageSourceElement = $imageElement->find('css', 'img'); |
||
151 | if (null !== $imageSourceElement) { |
||
152 | $this->saveImageUrlForType($type, $imageSourceElement->getAttribute('src')); |
||
153 | } |
||
154 | |||
155 | $imageElement->clickLink('Delete'); |
||
156 | } |
||
157 | |||
158 | public function removeFirstImage() |
||
159 | { |
||
160 | $this->clickTabIfItsNotActive('media'); |
||
161 | $imageElement = $this->getFirstImageElement(); |
||
162 | $imageTypeElement = $imageElement->find('css', 'input[type=text]'); |
||
163 | $imageSourceElement = $imageElement->find('css', 'img'); |
||
164 | |||
165 | if (null !== $imageTypeElement && null !== $imageSourceElement) { |
||
166 | $this->saveImageUrlForType( |
||
167 | $imageTypeElement->getValue(), |
||
168 | $imageSourceElement->getAttribute('src') |
||
169 | ); |
||
170 | } |
||
171 | |||
172 | $imageElement->clickLink('Delete'); |
||
173 | } |
||
174 | |||
175 | /** |
||
176 | * {@inheritdoc} |
||
177 | */ |
||
178 | public function modifyFirstImageType($type) |
||
179 | { |
||
180 | $this->clickTabIfItsNotActive('media'); |
||
181 | |||
182 | $firstImage = $this->getFirstImageElement(); |
||
183 | $this->setImageType($firstImage, $type); |
||
184 | } |
||
185 | |||
186 | /** |
||
187 | * {@inheritdoc} |
||
188 | */ |
||
189 | public function countImages() |
||
190 | { |
||
191 | $imageElements = $this->getImageElements(); |
||
192 | |||
193 | return count($imageElements); |
||
194 | } |
||
195 | |||
196 | /** |
||
197 | * {@inheritdoc} |
||
198 | */ |
||
199 | protected function getCodeElement() |
||
200 | { |
||
201 | return $this->getElement('code'); |
||
202 | } |
||
203 | |||
204 | /** |
||
205 | * {@inheritdoc} |
||
206 | */ |
||
207 | protected function getDefinedElements() |
||
208 | { |
||
209 | return array_merge(parent::getDefinedElements(), [ |
||
210 | 'channel_checkbox' => '.checkbox:contains("%channel%") input', |
||
211 | 'channels' => '#sylius_product_channels', |
||
212 | 'code' => '#sylius_product_code', |
||
213 | 'images' => '#sylius_product_images', |
||
214 | 'name' => '#sylius_product_translations_en_US_name', |
||
215 | 'options' => '#sylius_product_options', |
||
216 | 'price' => '#sylius_product_variant_price', |
||
217 | 'search' => '.ui.fluid.search.selection.dropdown', |
||
218 | 'search_item_selected' => 'div.menu > div.item.selected', |
||
219 | 'tab' => '.menu [data-tab="%name%"]', |
||
220 | 'taxonomy' => 'a[data-tab="taxonomy"]', |
||
221 | ]); |
||
222 | } |
||
223 | |||
224 | private function openTaxonBookmarks() |
||
225 | { |
||
226 | $this->getElement('taxonomy')->click(); |
||
227 | } |
||
228 | |||
229 | /** |
||
230 | * @param string $tabName |
||
231 | */ |
||
232 | private function clickTabIfItsNotActive($tabName) |
||
233 | { |
||
234 | $attributesTab = $this->getElement('tab', ['%name%' => $tabName]); |
||
235 | if (!$attributesTab->hasClass('active')) { |
||
236 | $attributesTab->click(); |
||
237 | } |
||
238 | } |
||
239 | |||
240 | /** |
||
241 | * @param string $type |
||
242 | * |
||
243 | * @return NodeElement |
||
244 | */ |
||
245 | private function getImageElementByType($type) |
||
246 | { |
||
247 | $images = $this->getElement('images'); |
||
248 | $typeInput = $images->find('css', 'input[value="' . $type . '"]'); |
||
249 | |||
250 | if (null === $typeInput) { |
||
251 | return null; |
||
252 | } |
||
253 | |||
254 | return $typeInput->getParent()->getParent()->getParent(); |
||
255 | } |
||
256 | |||
257 | /** |
||
258 | * @return NodeElement[] |
||
259 | */ |
||
260 | private function getImageElements() |
||
261 | { |
||
262 | $images = $this->getElement('images'); |
||
263 | |||
264 | return $images->findAll('css', 'div[data-form-collection="item"]'); |
||
265 | } |
||
266 | |||
267 | /** |
||
268 | * @return NodeElement |
||
269 | */ |
||
270 | private function getLastImageElement() |
||
271 | { |
||
272 | $imageElements = $this->getImageElements(); |
||
273 | |||
274 | Assert::notEmpty($imageElements); |
||
275 | |||
276 | return end($imageElements); |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
Loading history...
|
|||
277 | } |
||
278 | |||
279 | /** |
||
280 | * @return NodeElement |
||
281 | */ |
||
282 | private function getFirstImageElement() |
||
283 | { |
||
284 | $imageElements = $this->getImageElements(); |
||
285 | |||
286 | Assert::notEmpty($imageElements); |
||
287 | |||
288 | return reset($imageElements); |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
The expression
reset($imageElements); of type Behat\Mink\Element\NodeElement|false adds false to the return on line 288 which is incompatible with the return type documented by Sylius\Behat\Page\Admin\...e::getFirstImageElement of type Behat\Mink\Element\NodeElement . It seems like you forgot to handle an error condition.
Loading history...
|
|||
289 | } |
||
290 | |||
291 | /** |
||
292 | * @param NodeElement $imageElement |
||
293 | * @param string $type |
||
294 | */ |
||
295 | private function setImageType(NodeElement $imageElement, $type) |
||
296 | { |
||
297 | $typeField = $imageElement->findField('Type'); |
||
298 | $typeField->setValue($type); |
||
299 | } |
||
300 | |||
301 | private function provideImageUrlForType(string $type): ?string |
||
302 | { |
||
303 | return $this->imageUrls[$type] ?? null; |
||
304 | } |
||
305 | |||
306 | private function saveImageUrlForType(string $type, string $imageUrl): void |
||
307 | { |
||
308 | $this->imageUrls[$type] = $imageUrl; |
||
309 | } |
||
310 | } |
||
311 |