Completed
Push — master ( 799620...8a5869 )
by Kamil
116:02 queued 102:30
created

src/Sylius/Behat/Page/Admin/Taxon/CreatePage.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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\Taxon;
15
16
use Behat\Mink\Driver\Selenium2Driver;
17
use Behat\Mink\Element\NodeElement;
18
use Behat\Mink\Exception\ElementNotFoundException;
19
use Sylius\Behat\Behaviour\SpecifiesItsCode;
20
use Sylius\Behat\Page\Admin\Crud\CreatePage as BaseCreatePage;
21
use Sylius\Behat\Service\SlugGenerationHelper;
22
use Sylius\Component\Core\Model\TaxonInterface;
23
use Webmozart\Assert\Assert;
24
25
class CreatePage extends BaseCreatePage implements CreatePageInterface
26
{
27
    use SpecifiesItsCode;
28
29
    /**
30
     * {@inheritdoc}
31
     */
32
    public function countTaxons()
33
    {
34
        return count($this->getLeaves());
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40
    public function countTaxonsByName($name)
41
    {
42
        $matchedLeavesCounter = 0;
43
        $leaves = $this->getLeaves();
44
        foreach ($leaves as $leaf) {
45
            if (strpos($leaf->getText(), $name) !== false) {
46
                ++$matchedLeavesCounter;
47
            }
48
        }
49
50
        return $matchedLeavesCounter;
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56
    public function deleteTaxonOnPageByName($name)
57
    {
58
        $leaves = $this->getLeaves();
59
        foreach ($leaves as $leaf) {
60
            if ($leaf->getText() === $name) {
61
                $leaf->getParent()->find('css', '.ui.red.button')->press();
62
63
                return;
64
            }
65
        }
66
67
        throw new ElementNotFoundException($this->getDriver(), 'Delete button');
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73
    public function describeItAs($description, $languageCode)
74
    {
75
        $this->getDocument()->fillField(sprintf('sylius_taxon_translations_%s_description', $languageCode), $description);
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81
    public function hasTaxonWithName($name)
82
    {
83
        return 0 !== $this->countTaxonsByName($name);
84
    }
85
86
    /**
87
     * {@inheritdoc}
88
     */
89
    public function nameIt($name, $languageCode)
90
    {
91
        $this->activateLanguageTab($languageCode);
92
        $this->getElement('name', ['%language%' => $languageCode])->setValue($name);
93
94
        if ($this->getDriver() instanceof Selenium2Driver) {
95
            SlugGenerationHelper::waitForSlugGeneration(
96
                $this->getSession(),
97
                $this->getElement('slug', ['%language%' => $languageCode])
98
            );
99
        }
100
    }
101
102
    /**
103
     * {@inheritdoc}
104
     */
105
    public function specifySlug($slug, $languageCode)
106
    {
107
        $this->getDocument()->fillField(sprintf('sylius_taxon_translations_%s_slug', $languageCode), $slug);
108
    }
109
110
    /**
111
     * {@inheritdoc}
112
     */
113
    public function attachImage($path, $type = null)
114
    {
115
        $filesPath = $this->getParameter('files_path');
116
117
        $this->getDocument()->find('css', '[data-form-collection="add"]')->click();
118
119
        $imageForm = $this->getLastImageElement();
120
        $imageForm->fillField('Type', $type);
121
        $imageForm->find('css', 'input[type="file"]')->attachFile($filesPath . $path);
122
    }
123
124
    /**
125
     * {@inheritdoc}
126
     */
127
    public function getLeaves(TaxonInterface $parentTaxon = null)
128
    {
129
        $tree = $this->getElement('tree');
130
        Assert::notNull($tree);
131
        /** @var NodeElement[] $leaves */
132
        $leaves = $tree->findAll('css', '.item > .content > .header > a');
133
134
        if (null === $parentTaxon) {
135
            return $leaves;
136
        }
137
138
        foreach ($leaves as $leaf) {
139
            if ($leaf->getText() === $parentTaxon->getName()) {
140
                return $leaf->findAll('css', '.item > .content > .header');
141
            }
142
        }
143
144
        return [];
145
    }
146
147
    /**
148
     * {@inheritdoc}
149
     */
150
    public function activateLanguageTab($locale)
151
    {
152
        if (!$this->getDriver() instanceof Selenium2Driver) {
153
            return;
154
        }
155
156
        $languageTabTitle = $this->getElement('language_tab', ['%locale%' => $locale]);
157
        if (!$languageTabTitle->hasClass('active')) {
158
            $languageTabTitle->click();
159
        }
160
    }
161
162
    /**
163
     * {@inheritdoc}
164
     */
165
    protected function getElement($name, array $parameters = [])
166
    {
167
        if (!isset($parameters['%language%'])) {
168
            $parameters['%language%'] = 'en_US';
169
        }
170
171
        return parent::getElement($name, $parameters);
172
    }
173
174
    /**
175
     * {@inheritdoc}
176
     */
177
    protected function getDefinedElements()
178
    {
179
        return array_merge(parent::getDefinedElements(), [
180
            'code' => '#sylius_taxon_code',
181
            'description' => '#sylius_taxon_translations_en_US_description',
182
            'images' => '#sylius_taxon_images',
183
            'language_tab' => '[data-locale="%locale%"] .title',
184
            'name' => '#sylius_taxon_translations_%language%_name',
185
            'slug' => '#sylius_taxon_translations_%language%_slug',
186
            'tree' => '.ui.list',
187
        ]);
188
    }
189
190
    /**
191
     * @return NodeElement
192
     */
193
    private function getLastImageElement()
194
    {
195
        $images = $this->getElement('images');
196
        $items = $images->findAll('css', 'div[data-form-collection="item"]');
197
198
        Assert::notEmpty($items);
199
200
        return end($items);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The expression end($items); of type Behat\Mink\Element\NodeElement|false adds false to the return on line 200 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...
201
    }
202
}
203