Completed
Pull Request — master (#51)
by
unknown
04:10
created

Select2::open()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace hipanel\tests\_support\Page\Widget\Input;
4
5
/**
6
 * Class Select2
7
 *
8
 * Represent Select2 input element.
9
 * @package hipanel\tests\_support\Page\Widget\Input
10
 */
11
class Select2 extends TestableInput
12
{
13
    /**
14
     * @return string
15
     */
16
    protected function getSearchSelector(): string
17
    {
18
        return self::AS_BASE . "div[data-title='{$this->title}']>select";
19
    }
20
21
    /**
22
     * @return string
23
     */
24
    protected function getFilterSelector(): string
25
    {
26
        return self::TF_BASE . "select[id*={$this->auxName}]";
27
    }
28
29
    /**
30
     * @param string $value
31
     * @throws \Exception
32
     */
33
    public function setValue(string $value): void
34
    {
35
        $this->open();
36
        $this->fillSearchField($value);
37
        $this->chooseOption($value);
38
    }
39
40
    /**
41
     * @param string $value
42
     * @throws \Exception
43
     */
44
    public function setValueLike(string $value): void
45
    {
46
        $this->open();
47
        $this->fillSearchField($value);
48
        $this->chooseOptionLike($value);
49
    }
50
51
    /**
52
     * @return Select2
53
     */
54
    public function open(): Select2
55
    {
56
        $this->tester->click($this->getClickSelector());
57
        $this->seeIsOpened();
58
59
        return $this;
60
    }
61
62
    /**
63
     * @return Select2
64
     */
65
    public function seeIsOpened(): Select2
66
    {
67
        $this->tester->seeElement('.select2-container--open');
68
69
        return $this;
70
    }
71
72
    /**
73
     * @return string
74
     */
75
    protected function getClickSelector(): string
76
    {
77
        return $this->selector . ' + span [role=combobox]';
78
    }
79
80
    /**
81
     * @param string $name
82
     * @return Select2
83
     * @throws \Exception
84
     */
85
    public function fillSearchField(string $name): Select2
86
    {
87
        $inputSelector = 'input.select2-search__field';
88
        $this->tester->fillField($inputSelector, $name);
89
        $this->tester->waitForElementNotVisible('.loading-results', 120);
90
91
        return $this;
92
    }
93
94
    /**
95
     * @param $optionName
96
     * @return Select2
97
     */
98
    public function chooseOption($optionName): Select2
99
    {
100
        $this->tester->executeJS(<<<JS
101
        $("li:contains('{$optionName}')").each(function() {
102
            if (this.firstChild.data === '{$optionName}') {
103
                $(this).trigger('mouseup');
104
            }
105
        });
106
JS
107
        );
108
        return $this;
109
    }
110
111
    /**
112
     * @param $optionName
113
     * @return $this
114
     */
115
    public function chooseOptionLike($optionName): Select2
116
    {
117
        $this->tester->executeJS(<<<JS
118
        $("li:contains('{$optionName}')").each(function() {
119
            if (this.firstChild.data.indexOf('{$optionName}') !== -1) {
120
                $(this).trigger('mouseup');
121
            }
122
        });
123
JS
124
        );
125
        return $this;
126
    }
127
}
128