Completed
Push — symfony3 ( 19c63d...99c17a )
by Kamil
18:50
created

CreatePage::getFirstLeafName()   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 1
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 Behat\Mink\Exception\UnsupportedDriverActionException;
18
use Sylius\Behat\Behaviour\SpecifiesItsCode;
19
use Sylius\Behat\Page\Admin\Crud\CreatePage as BaseCreatePage;
20
use Sylius\Component\Core\Model\TaxonInterface;
21
use Webmozart\Assert\Assert;
22
23
/**
24
 * @author Arkadiusz Krakowiak <[email protected]>
25
 */
26
class CreatePage extends BaseCreatePage implements CreatePageInterface
27
{
28
    use SpecifiesItsCode;
29
30
    /**
31
     * {@inheritdoc}
32
     */
33
    public function countTaxons()
34
    {
35
        return count($this->getLeaves());
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41
    public function countTaxonsByName($name)
42
    {
43
        $matchedLeavesCounter = 0;
44
        $leaves = $this->getLeaves();
45
        foreach ($leaves as $leaf) {
0 ignored issues
show
Bug introduced by
The expression $leaves of type array<integer,object<Beh...ment\NodeElement>>|null is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
46
            if (strpos($leaf->getText(), $name) !== false) {
47
                $matchedLeavesCounter++;
48
            }
49
        }
50
51
        return $matchedLeavesCounter;
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57
    public function chooseParent(TaxonInterface $taxon)
58
    {
59
        $this->getElement('parent')->selectOption($taxon->getName(), false);
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65
    public function deleteTaxonOnPageByName($name)
66
    {
67
        $leaves = $this->getLeaves();
68
        foreach ($leaves as $leaf) {
0 ignored issues
show
Bug introduced by
The expression $leaves of type array<integer,object<Beh...ment\NodeElement>>|null is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
69
            if ($leaf->getText() === $name) {
70
                $leaf->getParent()->find('css', '.ui.red.button')->press();
71
72
                return;
73
            }
74
        }
75
76
        throw new ElementNotFoundException($this->getDriver(), 'Delete button');
77
    }
78
79
    /**
80
     * {@inheritdoc}
81
     */
82
    public function describeItAs($description, $languageCode)
83
    {
84
        $this->getDocument()->fillField(sprintf('sylius_taxon_translations_%s_description', $languageCode), $description);
85
    }
86
87
    /**
88
     * {@inheritdoc}
89
     */
90
    public function hasTaxonWithName($name)
91
    {
92
        return 0 !== $this->countTaxonsByName($name);
93
    }
94
95
    /**
96
     * {@inheritdoc}
97
     */
98
    public function nameIt($name, $languageCode)
99
    {
100
        $this->getDocument()->fillField(sprintf('sylius_taxon_translations_%s_name', $languageCode), $name);
101
102
        $this->waitForSlugGenerationIfNecessary();
103
    }
104
105
    /**
106
     * {@inheritdoc}
107
     */
108
    public function specifySlug($slug)
109
    {
110
        $this->getDocument()->fillField('Slug', $slug);
111
    }
112
113
    /**
114
     * {@inheritdoc}
115
     */
116
    public function attachImage($path, $code = null)
117
    {
118
        $filesPath = $this->getParameter('files_path');
119
120
        $this->getDocument()->find('css', '[data-form-collection="add"]')->click();
121
122
        $imageForm = $this->getLastImageElement();
123
        $imageForm->fillField('Code', $code);
124
        $imageForm->find('css', 'input[type="file"]')->attachFile($filesPath.$path);
125
    }
126
127
    /**
128
     * {@inheritDoc}
129
     */
130
    public function moveUp(TaxonInterface $taxon)
131
    {
132
        $this->moveLeaf($taxon, self::MOVE_DIRECTION_UP);
133
    }
134
135
    /**
136
     * {@inheritDoc}
137
     */
138
    public function moveDown(TaxonInterface $taxon)
139
    {
140
        $this->moveLeaf($taxon, self::MOVE_DIRECTION_DOWN);
141
    }
142
143
    /**
144
     * {@inheritDoc}
145
     */
146
    public function getFirstLeafName(TaxonInterface $parentTaxon = null)
147
    {
148
        return $this->getLeaves($parentTaxon)[0]->getText();
149
    }
150
151
    /**
152
     * {@inheritdoc}
153
     */
154
    public function getLeaves(TaxonInterface $parentTaxon = null)
155
    {
156
        $tree = $this->getElement('tree');
157
        Assert::notNull($tree);
158
        /** @var NodeElement[] $leaves */
159
        $leaves = $tree->findAll('css', '.item > .content > .header > a');
160
161
        if (null === $parentTaxon) {
162
            return $leaves;
163
        }
164
165
        foreach ($leaves as $leaf) {
166
            if ($leaf->getText() === $parentTaxon->getName()) {
167
                return $leaf->findAll('css', '.item > .content > .header');
168
            }
169
        }
170
    }
171
172
    /**
173
     * {@inheritdoc}
174
     */
175
    protected function getDefinedElements()
176
    {
177
        return array_merge(parent::getDefinedElements(), [
178
            'code' => '#sylius_taxon_code',
179
            'description' => '#sylius_taxon_translations_en_US_description',
180
            'images' => '#sylius_taxon_images',
181
            'name' => '#sylius_taxon_translations_en_US_name',
182
            'parent' => '#sylius_taxon_parent',
183
            'slug' => '#sylius_taxon_translations_en_US_slug',
184
            'tree' => '.ui.list',
185
        ]);
186
    }
187
188
    /**
189
     * @param TaxonInterface $taxon
190
     * @param string $direction
191
     *
192
     * @throws ElementNotFoundException
193
     */
194
    private function moveLeaf(TaxonInterface $taxon, $direction)
195
    {
196
        Assert::oneOf($direction, [self::MOVE_DIRECTION_UP, self::MOVE_DIRECTION_DOWN]);
197
198
        $leaves = $this->getLeaves();
199
        foreach ($leaves as $leaf) {
0 ignored issues
show
Bug introduced by
The expression $leaves of type array<integer,object<Beh...ment\NodeElement>>|null is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
200
            if ($leaf->getText() === $taxon->getName()) {
201
                $moveButton = $leaf->getParent()->find('css', sprintf('.sylius-taxon-move-%s', $direction));
202
                $moveButton->click();
203
204
                $moveButton->waitFor(5, function () use ($moveButton) {
205
                    return !$moveButton->hasClass('loading');
206
                });
207
208
                return;
209
            }
210
        }
211
212
        throw new ElementNotFoundException(
213
            $this->getDriver(),
214
            sprintf('Move %s button for %s taxon', $direction, $taxon->getName())
215
        );
216
    }
217
218
    /**
219
     * @return NodeElement
220
     */
221
    private function getLastImageElement()
222
    {
223
        $images = $this->getElement('images');
224
        $items = $images->findAll('css', 'div[data-form-collection="item"]');
225
226
        Assert::notEmpty($items);
227
228
        return end($items);
229
    }
230
231
    private function waitForSlugGenerationIfNecessary()
232
    {
233
        if ($this->getDriver() instanceof Selenium2Driver) {
234
            $this->getDocument()->waitFor(10, function () {
235
                return '' !== $this->getElement('slug')->getValue();
236
            });
237
        }
238
    }
239
}
240