Passed
Push — 1.x ( e3781f...255b78 )
by Kevin
02:10
created

Html   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 250
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 61
c 0
b 0
f 0
dl 0
loc 250
rs 10
wmc 27

23 Methods

Rating   Name   Duplication   Size   Complexity  
A assertNotSeeIn() 0 4 1
A selectFieldOptions() 0 7 2
A assertNotSeeElement() 0 4 1
A click() 0 10 2
A assertSeeElement() 0 4 1
A assertElementAttributeContains() 0 4 1
A follow() 0 5 1
A assertNotSee() 0 4 1
A fillField() 0 5 1
A checkField() 0 5 1
A uncheckField() 0 5 1
A selectFieldOption() 0 5 1
A assertNotSelected() 0 12 2
A assertSeeIn() 0 4 1
A assertChecked() 0 4 1
A assertFieldNotEquals() 0 4 1
A assertSelected() 0 12 2
A attachFile() 0 5 1
A assertNotChecked() 0 4 1
A assertElementAttributeNotContains() 0 4 1
A assertSee() 0 4 1
A assertFieldEquals() 0 4 1
A assertElementCount() 0 4 1
1
<?php
2
3
namespace Zenstruck\Browser\Extension;
4
5
use Behat\Mink\Exception\ElementNotFoundException;
6
use Behat\Mink\Exception\ExpectationException;
7
use PHPUnit\Framework\Assert as PHPUnit;
0 ignored issues
show
Bug introduced by
The type PHPUnit\Framework\Assert was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
9
/**
10
 * @author Kevin Bond <[email protected]>
11
 */
12
trait Html
13
{
14
    /**
15
     * @return static
16
     */
17
    final public function follow(string $link): self
18
    {
19
        $this->documentElement()->clickLink($link);
0 ignored issues
show
Bug introduced by
It seems like documentElement() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

19
        $this->/** @scrutinizer ignore-call */ 
20
               documentElement()->clickLink($link);
Loading history...
20
21
        return $this;
22
    }
23
24
    /**
25
     * @return static
26
     */
27
    final public function fillField(string $selector, string $value): self
28
    {
29
        $this->documentElement()->fillField($selector, $value);
30
31
        return $this;
32
    }
33
34
    /**
35
     * @return static
36
     */
37
    public function checkField(string $selector): self
38
    {
39
        $this->documentElement()->checkField($selector);
40
41
        return $this;
42
    }
43
44
    /**
45
     * @return static
46
     */
47
    public function uncheckField(string $selector): self
48
    {
49
        $this->documentElement()->uncheckField($selector);
50
51
        return $this;
52
    }
53
54
    /**
55
     * @return static
56
     */
57
    public function selectFieldOption(string $selector, string $value): self
58
    {
59
        $this->documentElement()->selectFieldOption($selector, $value);
60
61
        return $this;
62
    }
63
64
    /**
65
     * @return static
66
     */
67
    public function selectFieldOptions(string $selector, array $values): self
68
    {
69
        foreach ($values as $value) {
70
            $this->documentElement()->selectFieldOption($selector, $value, true);
71
        }
72
73
        return $this;
74
    }
75
76
    /**
77
     * @return static
78
     */
79
    final public function attachFile(string $selector, string $path): self
80
    {
81
        $this->documentElement()->attachFileToField($selector, $path);
82
83
        return $this;
84
    }
85
86
    /**
87
     * @return static
88
     */
89
    final public function click(string $selector): self
90
    {
91
        try {
92
            $this->documentElement()->pressButton($selector);
93
        } catch (ElementNotFoundException $e) {
94
            // try link
95
            $this->documentElement()->clickLink($selector);
96
        }
97
98
        return $this;
99
    }
100
101
    /**
102
     * @return static
103
     */
104
    final public function assertSee(string $expected): self
105
    {
106
        return $this->wrapMinkExpectation(
0 ignored issues
show
Bug introduced by
It seems like wrapMinkExpectation() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

106
        return $this->/** @scrutinizer ignore-call */ wrapMinkExpectation(
Loading history...
107
            fn() => $this->webAssert()->pageTextContains($expected)
0 ignored issues
show
Bug introduced by
It seems like webAssert() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

107
            fn() => $this->/** @scrutinizer ignore-call */ webAssert()->pageTextContains($expected)
Loading history...
108
        );
109
    }
110
111
    /**
112
     * @return static
113
     */
114
    final public function assertNotSee(string $expected): self
115
    {
116
        return $this->wrapMinkExpectation(
117
            fn() => $this->webAssert()->pageTextNotContains($expected)
118
        );
119
    }
120
121
    /**
122
     * @return static
123
     */
124
    final public function assertSeeIn(string $selector, string $expected): self
125
    {
126
        return $this->wrapMinkExpectation(
127
            fn() => $this->webAssert()->elementTextContains('css', $selector, $expected)
128
        );
129
    }
130
131
    /**
132
     * @return static
133
     */
134
    final public function assertNotSeeIn(string $selector, string $expected): self
135
    {
136
        return $this->wrapMinkExpectation(
137
            fn() => $this->webAssert()->elementTextNotContains('css', $selector, $expected)
138
        );
139
    }
140
141
    /**
142
     * @return static
143
     */
144
    final public function assertSeeElement(string $selector): self
145
    {
146
        return $this->wrapMinkExpectation(
147
            fn() => $this->webAssert()->elementExists('css', $selector)
148
        );
149
    }
150
151
    /**
152
     * @return static
153
     */
154
    final public function assertNotSeeElement(string $selector): self
155
    {
156
        return $this->wrapMinkExpectation(
157
            fn() => $this->webAssert()->elementNotExists('css', $selector)
158
        );
159
    }
160
161
    /**
162
     * @return static
163
     */
164
    final public function assertElementCount(string $selector, int $count): self
165
    {
166
        return $this->wrapMinkExpectation(
167
            fn() => $this->webAssert()->elementsCount('css', $selector, $count)
168
        );
169
    }
170
171
    /**
172
     * @return static
173
     */
174
    final public function assertFieldEquals(string $selector, string $expected): self
175
    {
176
        return $this->wrapMinkExpectation(
177
            fn() => $this->webAssert()->fieldValueEquals($selector, $expected)
178
        );
179
    }
180
181
    /**
182
     * @return static
183
     */
184
    final public function assertFieldNotEquals(string $selector, string $expected): self
185
    {
186
        return $this->wrapMinkExpectation(
187
            fn() => $this->webAssert()->fieldValueNotEquals($selector, $expected)
188
        );
189
    }
190
191
    /**
192
     * @return static
193
     */
194
    final public function assertSelected(string $selector, string $expected): self
195
    {
196
        try {
197
            $field = $this->webAssert()->fieldExists($selector);
198
            PHPUnit::assertTrue(true);
199
        } catch (ExpectationException $e) {
200
            PHPUnit::fail($e->getMessage());
201
        }
202
203
        PHPUnit::assertContains($expected, (array) $field->getValue());
204
205
        return $this;
206
    }
207
208
    /**
209
     * @return static
210
     */
211
    final public function assertNotSelected(string $selector, string $expected): self
212
    {
213
        try {
214
            $field = $this->webAssert()->fieldExists($selector);
215
            PHPUnit::assertTrue(true);
216
        } catch (ExpectationException $e) {
217
            PHPUnit::fail($e->getMessage());
218
        }
219
220
        PHPUnit::assertNotContains($expected, (array) $field->getValue());
221
222
        return $this;
223
    }
224
225
    /**
226
     * @return static
227
     */
228
    final public function assertChecked(string $selector): self
229
    {
230
        return $this->wrapMinkExpectation(
231
            fn() => $this->webAssert()->checkboxChecked($selector)
232
        );
233
    }
234
235
    /**
236
     * @return static
237
     */
238
    final public function assertNotChecked(string $selector): self
239
    {
240
        return $this->wrapMinkExpectation(
241
            fn() => $this->webAssert()->checkboxNotChecked($selector)
242
        );
243
    }
244
245
    /**
246
     * @return static
247
     */
248
    final public function assertElementAttributeContains(string $selector, string $attribute, string $expected): self
249
    {
250
        return $this->wrapMinkExpectation(
251
            fn() => $this->webAssert()->elementAttributeContains('css', $selector, $attribute, $expected)
252
        );
253
    }
254
255
    /**
256
     * @return static
257
     */
258
    final public function assertElementAttributeNotContains(string $selector, string $attribute, string $expected): self
259
    {
260
        return $this->wrapMinkExpectation(
261
            fn() => $this->webAssert()->elementAttributeNotContains('css', $selector, $attribute, $expected)
262
        );
263
    }
264
}
265