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

Admin/Product/CreateConfigurableProductPage.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\Product;
15
16
use Behat\Mink\Driver\Selenium2Driver;
17
use Behat\Mink\Element\NodeElement;
18
use Sylius\Behat\Behaviour\SpecifiesItsCode;
19
use Sylius\Behat\Page\Admin\Crud\CreatePage as BaseCreatePage;
20
use Sylius\Behat\Service\SlugGenerationHelper;
21
use Webmozart\Assert\Assert;
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
        if ($this->getDriver() instanceof Selenium2Driver) {
37
            SlugGenerationHelper::waitForSlugGeneration($this->getSession(), $this->getElement('slug'));
38
        }
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44
    public function selectOption($optionName)
45
    {
46
        $this->getDocument()->selectFieldOption('Options', $optionName);
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52
    public function attachImage($path, $type = null)
53
    {
54
        $this->clickTabIfItsNotActive('media');
55
56
        $filesPath = $this->getParameter('files_path');
57
58
        $this->getDocument()->clickLink('Add');
59
60
        $imageForm = $this->getLastImageElement();
61
        if (null !== $type) {
62
            $imageForm->fillField('Type', $type);
63
        }
64
65
        $imageForm->find('css', 'input[type="file"]')->attachFile($filesPath . $path);
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71
    protected function getDefinedElements()
72
    {
73
        return array_merge(parent::getDefinedElements(), [
74
            'code' => '#sylius_product_code',
75
            'images' => '#sylius_product_images',
76
            'name' => '#sylius_product_translations_en_US_name',
77
            'slug' => '#sylius_product_translations_en_US_slug',
78
            'tab' => '.menu [data-tab="%name%"]',
79
        ]);
80
    }
81
82
    /**
83
     * @param string $tabName
84
     */
85
    private function clickTabIfItsNotActive($tabName)
86
    {
87
        $attributesTab = $this->getElement('tab', ['%name%' => $tabName]);
88
        if (!$attributesTab->hasClass('active')) {
89
            $attributesTab->click();
90
        }
91
    }
92
93
    /**
94
     * @return NodeElement
95
     */
96
    private function getLastImageElement()
97
    {
98
        $images = $this->getElement('images');
99
        $items = $images->findAll('css', 'div[data-form-collection="item"]');
100
101
        Assert::notEmpty($items);
102
103
        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 103 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...
104
    }
105
}
106