Completed
Push — master ( 1d849e...3a997f )
by Paweł
09:48
created

CreatePage::waitForSlugGenerationIfNecessary()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
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 Sylius\Behat\Behaviour\SpecifiesItsCode;
18
use Sylius\Behat\Page\Admin\Crud\CreatePage as BaseCreatePage;
19
use Sylius\Behat\Service\SlugGenerationHelper;
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) {
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) {
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
        if ($this->getDriver() instanceof Selenium2Driver) {
96
            SlugGenerationHelper::waitForSlugGeneration(
97
                $this->getSession(),
98
                $this->getElement('slug', ['%language%' => $languageCode])
99
            );
100
        }
101
    }
102
103
    /**
104
     * {@inheritdoc}
105
     */
106
    public function specifySlug($slug, $languageCode)
107
    {
108
        $this->getDocument()->fillField(sprintf('sylius_taxon_translations_%s_slug', $languageCode), $slug);
109
    }
110
111
    /**
112
     * {@inheritdoc}
113
     */
114
    public function attachImage($path, $type = null)
115
    {
116
        $filesPath = $this->getParameter('files_path');
117
118
        $this->getDocument()->find('css', '[data-form-collection="add"]')->click();
119
120
        $imageForm = $this->getLastImageElement();
121
        $imageForm->fillField('Type', $type);
122
        $imageForm->find('css', 'input[type="file"]')->attachFile($filesPath.$path);
123
    }
124
125
    /**
126
     * {@inheritdoc}
127
     */
128
    public function getLeaves(TaxonInterface $parentTaxon = null)
129
    {
130
        $tree = $this->getElement('tree');
131
        Assert::notNull($tree);
132
        /** @var NodeElement[] $leaves */
133
        $leaves = $tree->findAll('css', '.item > .content > .header > a');
134
135
        if (null === $parentTaxon) {
136
            return $leaves;
137
        }
138
139
        foreach ($leaves as $leaf) {
140
            if ($leaf->getText() === $parentTaxon->getName()) {
141
                return $leaf->findAll('css', '.item > .content > .header');
142
            }
143
        }
144
145
        return [];
146
    }
147
148
    /**
149
     * {@inheritdoc}
150
     */
151
    public function activateLanguageTab($locale)
152
    {
153
        if (!$this->getDriver() instanceof Selenium2Driver) {
154
            return;
155
        }
156
157
        $languageTabTitle = $this->getElement('language_tab', ['%locale%' => $locale]);
158
        if (!$languageTabTitle->hasClass('active')) {
159
            $languageTabTitle->click();
160
        }
161
    }
162
163
    /**
164
     * {@inheritdoc}
165
     */
166
    protected function getElement($name, array $parameters = [])
167
    {
168
        if (!isset($parameters['%language%'])) {
169
            $parameters['%language%'] = 'en_US';
170
        }
171
172
        return parent::getElement($name, $parameters);
173
    }
174
175
    /**
176
     * {@inheritdoc}
177
     */
178
    protected function getDefinedElements()
179
    {
180
        return array_merge(parent::getDefinedElements(), [
181
            'code' => '#sylius_taxon_code',
182
            'description' => '#sylius_taxon_translations_en_US_description',
183
            'images' => '#sylius_taxon_images',
184
            'language_tab' => '[data-locale="%locale%"] .title',
185
            'name' => '#sylius_taxon_translations_%language%_name',
186
            'slug' => '#sylius_taxon_translations_%language%_slug',
187
            'tree' => '.ui.list',
188
        ]);
189
    }
190
191
    /**
192
     * @return NodeElement
193
     */
194
    private function getLastImageElement()
195
    {
196
        $images = $this->getElement('images');
197
        $items = $images->findAll('css', 'div[data-form-collection="item"]');
198
199
        Assert::notEmpty($items);
200
201
        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 201 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...
202
    }
203
}
204