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

attachImageWithCode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
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\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 CreateConfigurableProductPage extends BaseCreatePage implements CreateConfigurableProductPageInterface
24
{
25
    use SpecifiesItsCode;
26
27
    /**
28
     * {@inheritdoc}
29
     */
30
    public function nameItIn($name, $localeCode)
31
    {
32
        $this->getDocument()->fillField(
33
            sprintf('sylius_product_translations_%s_name', $localeCode), $name
34
        );
35
36
        $this->waitForSlugGenerationIfNecessary();
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    public function selectOption($optionName)
43
    {
44
        $this->getDocument()->selectFieldOption('Options', $optionName);
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50
    public function attachImage($path, $code = null)
51
    {
52
        $this->clickTabIfItsNotActive('media');
53
54
        $filesPath = $this->getParameter('files_path');
55
56
        $this->getDocument()->clickLink('Add');
57
58
        $imageForm = $this->getLastImageElement();
59
        if (null !== $code) {
60
            $imageForm->fillField('Code', $code);
61
        }
62
63
        $imageForm->find('css', 'input[type="file"]')->attachFile($filesPath.$path);
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69
    protected function getDefinedElements()
70
    {
71
        return array_merge(parent::getDefinedElements(), [
72
            'code' => '#sylius_product_code',
73
            'images' => '#sylius_product_images',
74
            'name' => '#sylius_product_translations_en_US_name',
75
            'slug' => '#sylius_product_translations_en_US_slug',
76
            'tab' => '.menu [data-tab="%name%"]',
77
        ]);
78
    }
79
80
    /**
81
     * @param string $tabName
82
     */
83
    private function clickTabIfItsNotActive($tabName)
84
    {
85
        $attributesTab = $this->getElement('tab', ['%name%' => $tabName]);
86
        if (!$attributesTab->hasClass('active')) {
87
            $attributesTab->click();
88
        }
89
    }
90
91
    /**
92
     * @return NodeElement
93
     */
94
    private function getLastImageElement()
95
    {
96
        $images = $this->getElement('images');
97
        $items = $images->findAll('css', 'div[data-form-collection="item"]');
98
99
        Assert::notEmpty($items);
100
101
        return end($items);
102
    }
103
104
    private function waitForSlugGenerationIfNecessary()
105
    {
106
        if ($this->getDriver() instanceof Selenium2Driver) {
107
            $this->getDocument()->waitFor(1000, function () {
108
                return '' !== $this->getElement('slug')->getValue();
109
            });
110
        }
111
    }
112
}
113