Completed
Push — unused-definitions ( d9908f )
by Kamil
18:33
created

CreatePage::getLeafNameFromPosition()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
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\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 deleteTaxonOnPageByName($name)
58
    {
59
        $leaves = $this->getLeaves();
60
        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...
61
            if ($leaf->getText() === $name) {
62
                $leaf->getParent()->find('css', '.ui.red.button')->press();
63
64
                return;
65
            }
66
        }
67
68
        throw new ElementNotFoundException($this->getDriver(), 'Delete button');
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74
    public function describeItAs($description, $languageCode)
75
    {
76
        $this->getDocument()->fillField(sprintf('sylius_taxon_translations_%s_description', $languageCode), $description);
77
    }
78
79
    /**
80
     * {@inheritdoc}
81
     */
82
    public function hasTaxonWithName($name)
83
    {
84
        return 0 !== $this->countTaxonsByName($name);
85
    }
86
87
    /**
88
     * {@inheritdoc}
89
     */
90
    public function nameIt($name, $languageCode)
91
    {
92
        $this->activateLanguageTab($languageCode);
93
        $this->getElement('name', ['%language%' => $languageCode])->setValue($name);
94
95
        $this->waitForSlugGenerationIfNecessary($languageCode);
96
    }
97
98
    /**
99
     * {@inheritdoc}
100
     */
101
    public function specifySlug($slug, $languageCode)
102
    {
103
        $this->getDocument()->fillField(sprintf('sylius_taxon_translations_%s_slug', $languageCode), $slug);
104
    }
105
106
    /**
107
     * {@inheritdoc}
108
     */
109
    public function attachImage($path, $code = null)
110
    {
111
        $filesPath = $this->getParameter('files_path');
112
113
        $this->getDocument()->find('css', '[data-form-collection="add"]')->click();
114
115
        $imageForm = $this->getLastImageElement();
116
        $imageForm->fillField('Code', $code);
117
        $imageForm->find('css', 'input[type="file"]')->attachFile($filesPath.$path);
118
    }
119
120
    /**
121
     * {@inheritdoc}
122
     */
123
    public function getLeaves(TaxonInterface $parentTaxon = null)
124
    {
125
        $tree = $this->getElement('tree');
126
        Assert::notNull($tree);
127
        /** @var NodeElement[] $leaves */
128
        $leaves = $tree->findAll('css', '.item > .content > .header > a');
129
130
        if (null === $parentTaxon) {
131
            return $leaves;
132
        }
133
134
        foreach ($leaves as $leaf) {
135
            if ($leaf->getText() === $parentTaxon->getName()) {
136
                return $leaf->findAll('css', '.item > .content > .header');
137
            }
138
        }
139
    }
140
141
    /**
142
     * {@inheritdoc}
143
     */
144
    public function activateLanguageTab($locale)
145
    {
146
        if (!$this->getDriver() instanceof Selenium2Driver) {
147
            return;
148
        }
149
150
        $languageTabTitle = $this->getElement('language_tab', ['%locale%' => $locale]);
151
        if (!$languageTabTitle->hasClass('active')) {
152
            $languageTabTitle->click();
153
        }
154
    }
155
156
    /**
157
     * {@inheritdoc}
158
     */
159
    protected function getElement($name, array $parameters = [])
160
    {
161
        if (!isset($parameters['%language%'])) {
162
            $parameters['%language%'] = 'en_US';
163
        }
164
165
        return parent::getElement($name, $parameters);
166
    }
167
168
    /**
169
     * {@inheritdoc}
170
     */
171
    protected function getDefinedElements()
172
    {
173
        return array_merge(parent::getDefinedElements(), [
174
            'code' => '#sylius_taxon_code',
175
            'description' => '#sylius_taxon_translations_en_US_description',
176
            'images' => '#sylius_taxon_images',
177
            'language_tab' => '[data-locale="%locale%"] .title',
178
            'name' => '#sylius_taxon_translations_%language%_name',
179
            'slug' => '#sylius_taxon_translations_%language%_slug',
180
            'tree' => '.ui.list',
181
        ]);
182
    }
183
184
    /**
185
     * @return NodeElement
186
     */
187
    private function getLastImageElement()
188
    {
189
        $images = $this->getElement('images');
190
        $items = $images->findAll('css', 'div[data-form-collection="item"]');
191
192
        Assert::notEmpty($items);
193
194
        return end($items);
195
    }
196
197
    /**
198
     * @param string $languageCode
199
     */
200
    private function waitForSlugGenerationIfNecessary($languageCode)
201
    {
202
        if ($this->getDriver() instanceof Selenium2Driver) {
203
            $this->getDocument()->waitFor(10, function () use ($languageCode) {
204
                return '' !== $this->getElement('slug', ['%language%' => $languageCode])->getValue();
205
            });
206
        }
207
    }
208
}
209