Passed
Push — 1.x ( 040dab...5db90d )
by Kevin
02:18
created

Assertions::assertResponseContains()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 4
rs 10
1
<?php
2
3
namespace Zenstruck\Browser;
4
5
use Behat\Mink\Exception\ExpectationException;
6
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...
7
use function JmesPath\search;
8
9
/**
10
 * @author Kevin Bond <[email protected]>
11
 */
12
trait Assertions
13
{
14
    /**
15
     * @return static
16
     */
17
    final public function assertOn(string $expected): self
18
    {
19
        PHPUnit::assertSame(self::cleanUrl($expected), self::cleanUrl($this->minkSession()->getCurrentUrl()));
0 ignored issues
show
Bug introduced by
It seems like minkSession() 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
        PHPUnit::assertSame(self::cleanUrl($expected), self::cleanUrl($this->/** @scrutinizer ignore-call */ minkSession()->getCurrentUrl()));
Loading history...
20
21
        return $this;
22
    }
23
24
    /**
25
     * @return static
26
     */
27
    final public function assertNotOn(string $expected): self
28
    {
29
        PHPUnit::assertNotSame(self::cleanUrl($expected), self::cleanUrl($this->minkSession()->getCurrentUrl()));
30
31
        return $this;
32
    }
33
34
    /**
35
     * @return static
36
     */
37
    final public function assertStatus(int $expected): self
38
    {
39
        return $this->wrapMinkExpectation(
40
            fn() => $this->webAssert()->statusCodeEquals($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

40
            fn() => $this->/** @scrutinizer ignore-call */ webAssert()->statusCodeEquals($expected)
Loading history...
41
        );
42
    }
43
44
    /**
45
     * @return static
46
     */
47
    final public function assertSuccessful(): self
48
    {
49
        $status = $this->minkSession()->getStatusCode();
50
51
        PHPUnit::assertTrue($status >= 200 && $status < 300, "Expected successful status code (2xx), [{$status}] received.");
52
53
        return $this;
54
    }
55
56
    /**
57
     * @return static
58
     */
59
    final public function assertRedirected(): self
60
    {
61
        $status = $this->minkSession()->getStatusCode();
62
63
        PHPUnit::assertTrue($status >= 300 && $status < 400, "Expected redirect status code (3xx), [{$status}] received.");
64
65
        return $this;
66
    }
67
68
    /**
69
     * @return static
70
     */
71
    final public function assertContains(string $expected): self
72
    {
73
        return $this->wrapMinkExpectation(
74
            fn() => $this->webAssert()->responseContains($expected)
75
        );
76
    }
77
78
    /**
79
     * @return static
80
     */
81
    final public function assertNotContains(string $expected): self
82
    {
83
        return $this->wrapMinkExpectation(
84
            fn() => $this->webAssert()->responseNotContains($expected)
85
        );
86
    }
87
88
    /**
89
     * @return static
90
     */
91
    final public function assertHeaderEquals(string $header, string $expected): self
92
    {
93
        return $this->wrapMinkExpectation(
94
            fn() => $this->webAssert()->responseHeaderEquals($header, $expected)
95
        );
96
    }
97
98
    /**
99
     * @return static
100
     */
101
    final public function assertHeaderContains(string $header, string $expected): self
102
    {
103
        return $this->wrapMinkExpectation(
104
            fn() => $this->webAssert()->responseHeaderContains($header, $expected)
105
        );
106
    }
107
108
    /**
109
     * @return static
110
     */
111
    final public function assertSee(string $expected): self
112
    {
113
        return $this->wrapMinkExpectation(
114
            fn() => $this->webAssert()->pageTextContains($expected)
115
        );
116
    }
117
118
    /**
119
     * @return static
120
     */
121
    final public function assertNotSee(string $expected): self
122
    {
123
        return $this->wrapMinkExpectation(
124
            fn() => $this->webAssert()->pageTextNotContains($expected)
125
        );
126
    }
127
128
    /**
129
     * @return static
130
     */
131
    final public function assertSeeIn(string $selector, string $expected): self
132
    {
133
        return $this->wrapMinkExpectation(
134
            fn() => $this->webAssert()->elementTextContains('css', $selector, $expected)
135
        );
136
    }
137
138
    /**
139
     * @return static
140
     */
141
    final public function assertNotSeeIn(string $selector, string $expected): self
142
    {
143
        return $this->wrapMinkExpectation(
144
            fn() => $this->webAssert()->elementTextNotContains('css', $selector, $expected)
145
        );
146
    }
147
148
    /**
149
     * @return static
150
     */
151
    final public function assertSeeElement(string $selector): self
152
    {
153
        return $this->wrapMinkExpectation(
154
            fn() => $this->webAssert()->elementExists('css', $selector)
155
        );
156
    }
157
158
    /**
159
     * @return static
160
     */
161
    final public function assertNotSeeElement(string $selector): self
162
    {
163
        return $this->wrapMinkExpectation(
164
            fn() => $this->webAssert()->elementNotExists('css', $selector)
165
        );
166
    }
167
168
    /**
169
     * @return static
170
     */
171
    final public function assertElementCount(string $selector, int $count): self
172
    {
173
        return $this->wrapMinkExpectation(
174
            fn() => $this->webAssert()->elementsCount('css', $selector, $count)
175
        );
176
    }
177
178
    /**
179
     * @return static
180
     */
181
    final public function assertFieldEquals(string $selector, string $expected): self
182
    {
183
        return $this->wrapMinkExpectation(
184
            fn() => $this->webAssert()->fieldValueEquals($selector, $expected)
185
        );
186
    }
187
188
    /**
189
     * @return static
190
     */
191
    final public function assertFieldNotEquals(string $selector, string $expected): self
192
    {
193
        return $this->wrapMinkExpectation(
194
            fn() => $this->webAssert()->fieldValueNotEquals($selector, $expected)
195
        );
196
    }
197
198
    /**
199
     * @return static
200
     */
201
    final public function assertSelected(string $selector, string $expected): self
202
    {
203
        try {
204
            $field = $this->webAssert()->fieldExists($selector);
205
            PHPUnit::assertTrue(true);
206
        } catch (ExpectationException $e) {
207
            PHPUnit::fail($e->getMessage());
208
        }
209
210
        PHPUnit::assertContains($expected, (array) $field->getValue());
211
212
        return $this;
213
    }
214
215
    /**
216
     * @return static
217
     */
218
    final public function assertNotSelected(string $selector, string $expected): self
219
    {
220
        try {
221
            $field = $this->webAssert()->fieldExists($selector);
222
            PHPUnit::assertTrue(true);
223
        } catch (ExpectationException $e) {
224
            PHPUnit::fail($e->getMessage());
225
        }
226
227
        PHPUnit::assertNotContains($expected, (array) $field->getValue());
228
229
        return $this;
230
    }
231
232
    /**
233
     * @return static
234
     */
235
    final public function assertChecked(string $selector): self
236
    {
237
        return $this->wrapMinkExpectation(
238
            fn() => $this->webAssert()->checkboxChecked($selector)
239
        );
240
    }
241
242
    /**
243
     * @return static
244
     */
245
    final public function assertNotChecked(string $selector): self
246
    {
247
        return $this->wrapMinkExpectation(
248
            fn() => $this->webAssert()->checkboxNotChecked($selector)
249
        );
250
    }
251
252
    /**
253
     * @return static
254
     */
255
    final public function assertElementAttributeContains(string $selector, string $attribute, string $expected): self
256
    {
257
        return $this->wrapMinkExpectation(
258
            fn() => $this->webAssert()->elementAttributeContains('css', $selector, $attribute, $expected)
259
        );
260
    }
261
262
    /**
263
     * @return static
264
     */
265
    final public function assertElementAttributeNotContains(string $selector, string $attribute, string $expected): self
266
    {
267
        return $this->wrapMinkExpectation(
268
            fn() => $this->webAssert()->elementAttributeNotContains('css', $selector, $attribute, $expected)
269
        );
270
    }
271
272
    /**
273
     * @param string $expression JMESPath expression
274
     * @param mixed  $expected
275
     *
276
     * @return static
277
     */
278
    final public function assertJsonMatches(string $expression, $expected): self
279
    {
280
        $this->assertHeaderContains('Content-Type', 'application/json');
281
282
        $data = \json_decode($this->documentElement()->getContent(), true, 512, JSON_THROW_ON_ERROR);
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

282
        $data = \json_decode($this->/** @scrutinizer ignore-call */ documentElement()->getContent(), true, 512, JSON_THROW_ON_ERROR);
Loading history...
283
284
        PHPUnit::assertSame($expected, search($expression, $data));
285
286
        return $this;
287
    }
288
289
    /**
290
     * @return static
291
     */
292
    final protected function wrapMinkExpectation(callable $callback): self
293
    {
294
        try {
295
            $callback();
296
            PHPUnit::assertTrue(true);
297
        } catch (ExpectationException $e) {
298
            PHPUnit::fail($e->getMessage());
299
        }
300
301
        return $this;
302
    }
303
304
    private static function cleanUrl(string $url): array
305
    {
306
        $parts = \parse_url(\urldecode($url));
307
308
        unset($parts['host'], $parts['scheme'], $parts['port']);
309
310
        return $parts;
311
    }
312
}
313