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

CreateSimpleProductPage::attachImage()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 8
nc 2
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\Product;
13
14
use Behat\Mink\Driver\Selenium2Driver;
15
use Behat\Mink\Element\NodeElement;
16
use Sylius\Behat\Behaviour\SpecifiesItsCode;
17
use Sylius\Behat\Page\Admin\Crud\CreatePage as BaseCreatePage;
18
use Webmozart\Assert\Assert;
19
20
/**
21
 * @author Łukasz Chruściel <[email protected]>
22
 */
23
class CreateSimpleProductPage extends BaseCreatePage implements CreateSimpleProductPageInterface
24
{
25
    use SpecifiesItsCode;
26
27
    /**
28
     * {@inheritdoc}
29
     */
30
    public function getRouteName()
31
    {
32
        return parent::getRouteName() . '_simple';
33
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38
    public function nameItIn($name, $localeCode)
39
    {
40
        $this->getDocument()->fillField(
41
            sprintf('sylius_product_translations_%s_name', $localeCode), $name
42
        );
43
44
        $this->waitForSlugGenerationIfNecessary();
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50
    public function specifySlug($slug)
51
    {
52
        $this->getDocument()->fillField('Slug', $slug);
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58
    public function specifyPrice($price)
59
    {
60
        $this->getDocument()->fillField('Price', $price);
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66
    public function addAttribute($attribute, $value)
67
    {
68
        $this->clickTabIfItsNotActive('attributes');
69
70
        $attributeOption = $this->getElement('attributes_choice')->find('css', sprintf('option:contains("%s")', $attribute));
71
        $this->selectElementFromAttributesDropdown($attributeOption->getAttribute('value'));
72
73
        $this->getDocument()->pressButton('Add attributes');
74
        $this->waitForFormElement();
75
76
        $this->getElement('attribute_value', ['%attribute%' => $attribute])->setValue($value);
77
    }
78
79
    /**
80
     * {@inheritdoc}
81
     */
82
    public function removeAttribute($attribute)
83
    {
84
        $this->clickTabIfItsNotActive('attributes');
85
86
        $this->getElement('attribute_delete_button', ['%attribute%' => $attribute])->press();
87
    }
88
89
    /**
90
     * {@inheritdoc}
91
     */
92
    public function attachImage($path, $code = null)
93
    {
94
        $this->clickTabIfItsNotActive('media');
95
96
        $filesPath = $this->getParameter('files_path');
97
98
        $this->getDocument()->clickLink('Add');
99
100
        $imageForm = $this->getLastImageElement();
101
        if (null !== $code) {
102
            $imageForm->fillField('Code', $code);
103
        }
104
105
        $imageForm->find('css', 'input[type="file"]')->attachFile($filesPath.$path);
106
    }
107
108
    public function enableSlugModification()
109
    {
110
        $this->getElement('toggle_slug_modification_button')->press();
111
    }
112
113
    /**
114
     * {@inheritdoc}
115
     */
116
    protected function getDefinedElements()
117
    {
118
        return array_merge(parent::getDefinedElements(), [
119
            'attribute_delete_button' => '.attribute .label:contains("%attribute%") ~ button',
120
            'attribute_value' => '.attribute .label:contains("%attribute%") ~ input',
121
            'attributes_choice' => 'select[name="sylius_product_attribute_choice"]',
122
            'code' => '#sylius_product_code',
123
            'form' => 'form[name="sylius_product"]',
124
            'images' => '#sylius_product_images',
125
            'name' => '#sylius_product_translations_en_US_name',
126
            'price' => '#sylius_product_variant_price',
127
            'slug' => '#sylius_product_translations_en_US_slug',
128
            'tab' => '.menu [data-tab="%name%"]',
129
            'toggle_slug_modification_button' => '#toggle-slug-modification',
130
        ]);
131
    }
132
133
    /**
134
     * @param int $id
135
     */
136
    private function selectElementFromAttributesDropdown($id)
137
    {
138
        /** @var Selenium2Driver $driver */
139
        $driver = $this->getDriver();
140
        Assert::isInstanceOf($driver, Selenium2Driver::class);
141
142
        $driver->executeScript('$(\'[name="sylius_product_attribute_choice"]\').dropdown(\'show\');');
143
        $driver->executeScript(sprintf('$(\'[name="sylius_product_attribute_choice"]\').dropdown(\'set selected\', %s);', $id));
144
    }
145
146
    /**
147
     * @param int $timeout
148
     */
149
    private function waitForFormElement($timeout = 5)
150
    {
151
        $form = $this->getElement('form');
152
        $this->getDocument()->waitFor($timeout, function () use ($form) {
153
            return false === strpos($form->getAttribute('class'), 'loading');
154
        });
155
    }
156
157
    /**
158
     * @param string $tabName
159
     */
160
    private function clickTabIfItsNotActive($tabName)
161
    {
162
        $attributesTab = $this->getElement('tab', ['%name%' => $tabName]);
163
        if (!$attributesTab->hasClass('active')) {
164
            $attributesTab->click();
165
        }
166
    }
167
168
    /**
169
     * @return NodeElement
170
     */
171
    private function getLastImageElement()
172
    {
173
        $images = $this->getElement('images');
174
        $items = $images->findAll('css', 'div[data-form-collection="item"]');
175
176
        Assert::notEmpty($items);
177
178
        return end($items);
179
    }
180
181
    private function waitForSlugGenerationIfNecessary()
182
    {
183
        if ($this->getDriver() instanceof Selenium2Driver) {
184
            $this->getDocument()->waitFor(1000, function () {
185
                return '' !== $this->getElement('slug')->getValue();
186
            });
187
        }
188
    }
189
}
190