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

CreatePage::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\SpecifiesItsCode;
17
use Sylius\Behat\Page\Admin\Crud\CreatePage as BaseCreatePage;
18
use Sylius\Component\Core\Model\TaxonInterface;
19
use Webmozart\Assert\Assert;
20
21
/**
22
 * @author Arkadiusz Krakowiak <[email protected]>
23
 */
24
class CreatePage extends BaseCreatePage implements CreatePageInterface
25
{
26
    use SpecifiesItsCode;
27
28
    /**
29
     * {@inheritdoc}
30
     */
31
    public function countTaxons()
32
    {
33
        return count($this->getLeafs());
34
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39
    public function countTaxonsByName($name)
40
    {
41
        $matchedLeafsCounter = 0;
42
        $leafs = $this->getLeafs();
43
        foreach ($leafs as $leaf) {
44
            if ($leaf->getText() === $name) {
45
                $matchedLeafsCounter++;
46
            }
47
        }
48
49
        return $matchedLeafsCounter;
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55
    public function chooseParent(TaxonInterface $taxon)
56
    {
57
        $this->getElement('parent')->selectOption($taxon->getName(), false);
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63
    public function deleteTaxonOnPageByName($name)
64
    {
65
        $leafs = $this->getLeafs();
66
        foreach ($leafs as $leaf) {
67
            if ($leaf->getText() === $name) {
68
                $leaf->getParent()->pressButton('Delete');
69
70
                return;
71
            }
72
        }
73
74
        throw new ElementNotFoundException($this->getDriver(), 'Delete button');
75
    }
76
77
    /**
78
     * {@inheritdoc}
79
     */
80
    public function describeItAs($description, $languageCode)
81
    {
82
        $this->getDocument()->fillField(sprintf('sylius_taxon_translations_%s_description', $languageCode), $description);
83
    }
84
85
    /**
86
     * {@inheritdoc}
87
     */
88
    public function hasTaxonWithName($name)
89
    {
90
        return 0 !== $this->countTaxonsByName($name);
91
    }
92
93
    /**
94
     * {@inheritdoc}
95
     */
96
    public function nameIt($name, $languageCode)
97
    {
98
        $this->getDocument()->fillField(sprintf('sylius_taxon_translations_%s_name', $languageCode), $name);
99
    }
100
101
    /**
102
     * {@inheritdoc}
103
     */
104
    public function specifyPermalink($permalink, $languageCode)
105
    {
106
        $this->getDocument()->fillField(sprintf('sylius_taxon_translations_%s_permalink', $languageCode), $permalink);
107
    }
108
109
    /**
110
     * {@inheritdoc}
111
     */
112
    public function attachImage($path, $code = null)
113
    {
114
        $filesPath = $this->getParameter('files_path');
115
116
        $this->getDocument()->clickLink('Add');
117
118
        $imageForm = $this->getLastImageElement();
119
        $imageForm->fillField('Code', $code);
120
        $imageForm->find('css', 'input[type="file"]')->attachFile($filesPath.$path);
121
    }
122
123
    /**
124
     * {@inheritdoc}
125
     */
126
    protected function getDefinedElements()
127
    {
128
        return array_merge(parent::getDefinedElements(), [
129
            'code' => '#sylius_taxon_code',
130
            'description' => '#sylius_taxon_translations_en_US_description',
131
            'images' => '#sylius_taxon_images',
132
            'name' => '#sylius_taxon_translations_en_US_name',
133
            'parent' => '#sylius_taxon_parent',
134
            'permalink' => '#sylius_taxon_translations_en_US_permalink',
135
            'tree' => '.ui.list',
136
        ]);
137
    }
138
139
    /**
140
     * @return NodeElement[]
141
     *
142
     * @throws ElementNotFoundException
143
     */
144
    private function getLeafs()
145
    {
146
        $tree = $this->getElement('tree');
147
        Assert::notNull($tree);
148
149
        return $tree->findAll('css', '.item > .content > .header');
150
    }
151
152
    /**
153
     * @return NodeElement
154
     */
155
    private function getLastImageElement()
156
    {
157
        $images = $this->getElement('images');
158
        $items = $images->findAll('css', 'div[data-form-collection="item"]');
159
160
        Assert::notEmpty($items);
161
162
        return end($items);
163
    }
164
}
165