Completed
Push — remove-content-bundle ( 201341 )
by Kamil
34:43
created

UpdatePage   A

Complexity

Total Complexity 30

Size/Duplication

Total Lines 259
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
wmc 30
lcom 1
cbo 8
dl 0
loc 259
rs 10
c 0
b 0
f 0

23 Methods

Rating   Name   Duplication   Size   Complexity  
A chooseParent() 0 4 1
A describeItAs() 0 4 1
A nameIt() 0 6 1
A specifySlug() 0 4 1
A attachImage() 0 13 2
A isImageWithCodeDisplayed() 0 15 2
A isSlugReadOnly() 0 4 1
A removeImageWithCode() 0 5 1
A removeFirstImage() 0 5 1
A enableSlugModification() 0 4 1
A countImages() 0 6 1
A changeImageWithCode() 0 7 1
A getParent() 0 4 1
A getValidationMessageForImage() 0 11 2
A getValidationMessageForImageAtPlace() 0 11 2
A isImageCodeDisabled() 0 4 1
A getCodeElement() 0 4 1
A getDefinedElements() 0 12 1
A getLastImageElement() 0 8 1
A getFirstImageElement() 0 6 1
A getImageElements() 0 6 1
A getImageElementByCode() 0 11 2
A waitForSlugGenerationIfNecessary() 0 16 3
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\Taxon;
13
14
use Behat\Mink\Driver\Selenium2Driver;
15
use Behat\Mink\Element\NodeElement;
16
use Behat\Mink\Exception\ElementNotFoundException;
17
use Sylius\Behat\Behaviour\ChecksCodeImmutability;
18
use Sylius\Behat\Page\Admin\Crud\UpdatePage as BaseUpdatePage;
19
use Sylius\Component\Core\Model\TaxonInterface;
20
use Webmozart\Assert\Assert;
21
22
/**
23
 * @author Arkadiusz Krakowiak <[email protected]>
24
 */
25
class UpdatePage extends BaseUpdatePage implements UpdatePageInterface
26
{
27
    use ChecksCodeImmutability;
28
29
    /**
30
     * {@inheritdoc}
31
     */
32
    public function chooseParent(TaxonInterface $taxon)
33
    {
34
        $this->getElement('parent')->selectOption($taxon->getName(), false);
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40
    public function describeItAs($description, $languageCode)
41
    {
42
        $this->getDocument()->fillField(sprintf('sylius_taxon_translations_%s_description', $languageCode), $description);
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48
    public function nameIt($name, $languageCode)
49
    {
50
        $this->getDocument()->fillField(sprintf('sylius_taxon_translations_%s_name', $languageCode), $name);
51
52
        $this->waitForSlugGenerationIfNecessary();
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58
    public function specifySlug($slug)
59
    {
60
        $this->getDocument()->fillField('Slug', $slug);
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66
    public function attachImage($path, $code = null)
67
    {
68
        $filesPath = $this->getParameter('files_path');
69
70
        $this->getDocument()->find('css', '[data-form-collection="add"]')->click();
71
72
        $imageForm = $this->getLastImageElement();
73
        if (null !== $code) {
74
            $imageForm->fillField('Code', $code);
75
        }
76
77
        $imageForm->find('css', 'input[type="file"]')->attachFile($filesPath.$path);
78
    }
79
80
    /**
81
     * {@inheritdoc}
82
     */
83
    public function isImageWithCodeDisplayed($code)
84
    {
85
        $imageElement = $this->getImageElementByCode($code);
86
87
        if (null === $imageElement) {
88
            return false;
89
        }
90
91
        $imageUrl = $imageElement->find('css', 'img')->getAttribute('src');
92
        $this->getDriver()->visit($imageUrl);
93
        $pageText = $this->getDocument()->getText();
94
        $this->getDriver()->back();
95
96
        return false === stripos($pageText, '404 Not Found');
97
    }
98
99
    /**
100
     * {@inheritdoc}
101
     */
102
    public function isSlugReadOnly()
103
    {
104
        return 'readonly' === $this->getElement('slug')->getAttribute('readonly');
105
    }
106
107
    /**
108
     * {@inheritdoc}
109
     */
110
    public function removeImageWithCode($code)
111
    {
112
        $imageElement = $this->getImageElementByCode($code);
113
        $imageElement->clickLink('Delete');
114
    }
115
116
    public function removeFirstImage()
117
    {
118
        $imageElement = $this->getFirstImageElement();
119
        $imageElement->clickLink('Delete');
120
    }
121
122
    public function enableSlugModification()
123
    {
124
        $this->getElement('toggle_taxon_slug_modification_button')->press();
125
    }
126
127
    /**
128
     * {@inheritdoc}
129
     */
130
    public function countImages()
131
    {
132
        $imageElements = $this->getImageElements();
133
134
        return count($imageElements);
135
    }
136
137
    /**
138
     * {@inheritdoc}
139
     */
140
    public function changeImageWithCode($code, $path)
141
    {
142
        $filesPath = $this->getParameter('files_path');
143
144
        $imageForm = $this->getImageElementByCode($code);
145
        $imageForm->find('css', 'input[type="file"]')->attachFile($filesPath.$path);
146
    }
147
148
    /**
149
     * @inheritDoc
150
     */
151
    public function getParent()
152
    {
153
        return $this->getElement('parent')->getValue();
154
    }
155
156
    /**
157
     * {@inheritdoc}
158
     */
159
    public function getValidationMessageForImage()
160
    {
161
        $provinceForm = $this->getLastImageElement();
162
163
        $foundElement = $provinceForm->find('css', '.sylius-validation-error');
164
        if (null === $foundElement) {
165
            throw new ElementNotFoundException($this->getSession(), 'Tag', 'css', '.sylius-validation-error');
166
        }
167
168
        return $foundElement->getText();
169
    }
170
171
    /**
172
     * {@inheritdoc}
173
     */
174
    public function getValidationMessageForImageAtPlace($place) {
175
        
176
        $images = $this->getImageElements();
177
        
178
        $foundElement = $images[$place]->find('css', '.sylius-validation-error');
179
        if (null === $foundElement) {
180
            throw new ElementNotFoundException($this->getSession(), 'Tag', 'css', '.sylius-validation-error');
181
        }
182
183
        return $foundElement->getText();
184
    }
185
186
    /**
187
     * @return bool
188
     */
189
    public function isImageCodeDisabled()
190
    {
191
        return 'disabled' === $this->getLastImageElement()->findField('Code')->getAttribute('disabled');
192
    }
193
194
    /**
195
     * @return NodeElement
196
     */
197
    protected function getCodeElement()
198
    {
199
        return $this->getElement('code');
200
    }
201
202
    /**
203
     * {@inheritdoc}
204
     */
205
    protected function getDefinedElements()
206
    {
207
        return array_merge(parent::getDefinedElements(), [
208
            'code' => '#sylius_taxon_code',
209
            'description' => '#sylius_taxon_translations_en_US_description',
210
            'images' => '#sylius_taxon_images',
211
            'name' => '#sylius_taxon_translations_en_US_name',
212
            'parent' => '#sylius_taxon_parent',
213
            'slug' => '#sylius_taxon_translations_en_US_slug',
214
            'toggle_taxon_slug_modification_button' => '#toggle-taxon-slug-modification',
215
        ]);
216
    }
217
218
    /**
219
     * @return NodeElement
220
     */
221
    private function getLastImageElement()
222
    {
223
        $imageElements = $this->getImageElements();
224
225
        Assert::notEmpty($imageElements);
226
227
        return end($imageElements);
228
    }
229
230
    /**
231
     * @return NodeElement
232
     */
233
    private function getFirstImageElement()
234
    {
235
        $imageElements = $this->getImageElements();
236
237
        return reset($imageElements);
238
    }
239
240
    /**
241
     * @return NodeElement[]
242
     */
243
    private function getImageElements()
244
    {
245
        $images = $this->getElement('images');
246
247
        return $images->findAll('css', 'div[data-form-collection="item"]');
248
    }
249
250
    /**
251
     * @param string $code
252
     *
253
     * @return NodeElement
254
     */
255
    private function getImageElementByCode($code)
256
    {
257
        $images = $this->getElement('images');
258
        $inputCode = $images->find('css', 'input[value="'.$code.'"]');
259
260
        if (null === $inputCode) {
261
            return null;
262
        }
263
264
        return $inputCode->getParent()->getParent()->getParent();
265
    }
266
267
    private function waitForSlugGenerationIfNecessary()
268
    {
269
        if (!$this->getDriver() instanceof Selenium2Driver) {
270
            return;
271
        }
272
273
        $slugElement = $this->getElement('slug');
274
        if ($slugElement->hasAttribute('readonly')) {
275
            return;
276
        }
277
278
        $value = $slugElement->getValue();
279
        $this->getDocument()->waitFor(10, function () use ($slugElement, $value) {
280
            return $value !== $slugElement->getValue();
281
        });
282
    }
283
}
284