Completed
Push — to-be-a-hat-or-not-to-be-kuhwa ( 7c7f7b...b72886 )
by Kamil
20:35
created

UpdatePage::attachImageWithCode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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