Completed
Push — master ( e2e5d3...075ad7 )
by Paweł
96:55 queued 86:21
created

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