BrowserKitBrowserTests::http_method_actions()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 46
Code Lines 43

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 43
c 2
b 0
f 0
nc 1
nop 0
dl 0
loc 46
rs 9.232
1
<?php
2
3
namespace Zenstruck\Browser\Tests;
4
5
use Zenstruck\Browser\HttpOptions;
6
use Zenstruck\Browser\Tests\Fixture\CustomHttpOptions;
7
8
/**
9
 * @author Kevin Bond <[email protected]>
10
 */
11
trait BrowserKitBrowserTests
12
{
13
    use BrowserTests;
14
15
    /**
16
     * @test
17
     */
18
    public function following_redirect_follows_all_by_default(): void
19
    {
20
        $this->browser()
21
            ->interceptRedirects()
0 ignored issues
show
Bug introduced by
The method interceptRedirects() does not exist on Zenstruck\Browser. It seems like you code against a sub-type of Zenstruck\Browser such as Zenstruck\Browser\BrowserKitBrowser. ( Ignorable by Annotation )

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

21
            ->/** @scrutinizer ignore-call */ interceptRedirects()
Loading history...
22
            ->visit('/redirect1')
23
            ->assertOn('/redirect1')
24
            ->followRedirect()
25
            ->assertOn('/page1')
26
            ->assertSuccessful()
27
        ;
28
    }
29
30
    /**
31
     * @test
32
     */
33
    public function can_re_enable_following_redirects(): void
34
    {
35
        $this->browser()
36
            ->interceptRedirects()
37
            ->visit('/redirect1')
38
            ->assertOn('/redirect1')
39
            ->followRedirects()
40
            ->visit('/redirect1')
41
            ->assertOn('/page1')
42
        ;
43
    }
44
45
    /**
46
     * @test
47
     */
48
    public function calling_follow_redirects_when_the_response_is_a_redirect_follows_the_redirect(): void
49
    {
50
        $this->browser()
51
            ->interceptRedirects()
52
            ->visit('/redirect1')
53
            ->followRedirects()
54
            ->assertOn('/page1')
55
            ->interceptRedirects()
56
            ->visit('/page1')
57
            ->followRedirects()
58
            ->assertOn('/page1')
59
        ;
60
    }
61
62
    /**
63
     * @test
64
     */
65
    public function calling_follow_redirects_before_a_request_has_been_made_just_enables_following_redirects(): void
66
    {
67
        $this->browser()
68
            ->followRedirects()
0 ignored issues
show
Bug introduced by
The method followRedirects() does not exist on Zenstruck\Browser. Did you maybe mean follow()? ( Ignorable by Annotation )

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

68
            ->/** @scrutinizer ignore-call */ followRedirects()

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
69
            ->visit('/redirect1')
70
            ->assertOn('/page1')
71
        ;
72
    }
73
74
    /**
75
     * @test
76
     */
77
    public function can_limit_redirects_followed(): void
78
    {
79
        $this->browser()
80
            ->interceptRedirects()
81
            ->visit('/redirect1')
82
            ->assertOn('/redirect1')
83
            ->assertRedirected()
84
            ->followRedirect(1)
85
            ->assertOn('/redirect2')
86
            ->assertRedirected()
87
            ->followRedirect(1)
88
            ->assertOn('/redirect3')
89
            ->assertRedirected()
90
            ->followRedirect(1)
91
            ->assertOn('/page1')
92
            ->assertSuccessful()
93
        ;
94
    }
95
96
    /**
97
     * @test
98
     */
99
    public function assert_redirected_to_follows_all_redirects_by_default(): void
100
    {
101
        $this->browser()
102
            ->interceptRedirects()
103
            ->visit('/redirect1')
104
            ->assertRedirectedTo('/page1')
105
        ;
106
    }
107
108
    /**
109
     * @test
110
     */
111
    public function assert_redirected_to_can_configure_number_of_redirects_to_follow(): void
112
    {
113
        $this->browser()
114
            ->interceptRedirects()
115
            ->visit('/redirect1')
116
            ->assertRedirectedTo('/redirect2', 1)
117
        ;
118
    }
119
120
    /**
121
     * @test
122
     */
123
    public function exception_thrown_if_asserting_redirected_and_not_intercepting_redirects(): void
124
    {
125
        $this->expectException(\RuntimeException::class);
0 ignored issues
show
Bug introduced by
It seems like expectException() 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

125
        $this->/** @scrutinizer ignore-call */ 
126
               expectException(\RuntimeException::class);
Loading history...
126
        $this->expectExceptionMessage('Cannot assert redirected if not intercepting redirects. Call ->interceptRedirects() before making the request.');
0 ignored issues
show
Bug introduced by
It seems like expectExceptionMessage() 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

126
        $this->/** @scrutinizer ignore-call */ 
127
               expectExceptionMessage('Cannot assert redirected if not intercepting redirects. Call ->interceptRedirects() before making the request.');
Loading history...
127
128
        $this->browser()
129
            ->visit('/redirect1')
130
            ->assertRedirected()
0 ignored issues
show
Bug introduced by
The method assertRedirected() does not exist on Zenstruck\Browser. It seems like you code against a sub-type of Zenstruck\Browser such as Zenstruck\Browser\BrowserKitBrowser. ( Ignorable by Annotation )

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

130
            ->/** @scrutinizer ignore-call */ assertRedirected()
Loading history...
131
        ;
132
    }
133
134
    /**
135
     * @test
136
     */
137
    public function exception_thrown_if_asserting_redirected_to_and_not_intercepting_redirects(): void
138
    {
139
        $this->expectException(\RuntimeException::class);
140
        $this->expectExceptionMessage('Cannot assert redirected if not intercepting redirects. Call ->interceptRedirects() before making the request.');
141
142
        $this->browser()
143
            ->visit('/redirect1')
144
            ->assertRedirectedTo('/page1')
0 ignored issues
show
Bug introduced by
The method assertRedirectedTo() does not exist on Zenstruck\Browser. It seems like you code against a sub-type of Zenstruck\Browser such as Zenstruck\Browser\BrowserKitBrowser. ( Ignorable by Annotation )

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

144
            ->/** @scrutinizer ignore-call */ assertRedirectedTo('/page1')
Loading history...
145
        ;
146
    }
147
148
    /**
149
     * @test
150
     */
151
    public function exceptions_are_caught_by_default(): void
152
    {
153
        $this->browser()
154
            ->visit('/exception')
155
            ->assertStatus(500)
0 ignored issues
show
Bug introduced by
The method assertStatus() does not exist on Zenstruck\Browser. Did you maybe mean assertSee()? ( Ignorable by Annotation )

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

155
            ->/** @scrutinizer ignore-call */ assertStatus(500)

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
156
        ;
157
    }
158
159
    /**
160
     * @test
161
     */
162
    public function response_header_assertions(): void
163
    {
164
        $this->browser()
165
            ->visit('/page1')
166
            ->assertHeaderEquals('Content-Type', 'text/html; charset=UTF-8')
0 ignored issues
show
Bug introduced by
The method assertHeaderEquals() does not exist on Zenstruck\Browser. It seems like you code against a sub-type of Zenstruck\Browser such as Zenstruck\Browser\BrowserKitBrowser. ( Ignorable by Annotation )

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

166
            ->/** @scrutinizer ignore-call */ assertHeaderEquals('Content-Type', 'text/html; charset=UTF-8')
Loading history...
167
            ->assertHeaderContains('Content-Type', 'text/html')
168
        ;
169
    }
170
171
    /**
172
     * @test
173
     */
174
    public function http_method_actions(): void
175
    {
176
        $this->browser()
177
            ->get('/http-method')
0 ignored issues
show
Bug introduced by
The method get() does not exist on Zenstruck\Browser. It seems like you code against a sub-type of Zenstruck\Browser such as Zenstruck\Browser\BrowserKitBrowser. ( Ignorable by Annotation )

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

177
            ->/** @scrutinizer ignore-call */ get('/http-method')
Loading history...
178
            ->assertSuccessful()
179
            ->assertContains('"method":"GET"')
180
            ->post('/http-method')
181
            ->assertSuccessful()
182
            ->assertContains('"method":"POST"')
183
            ->patch('/http-method')
184
            ->assertSuccessful()
185
            ->assertContains('"method":"PATCH"')
186
            ->delete('/http-method')
187
            ->assertSuccessful()
188
            ->assertContains('"method":"DELETE"')
189
            ->put('/http-method')
190
            ->assertSuccessful()
191
            ->assertContains('"method":"PUT"')
192
            ->assertContains('"ajax":false')
193
            ->post('/http-method', [
194
                'json' => ['foo' => 'bar'],
195
                'headers' => ['X-Foo' => 'Bar'],
196
                'ajax' => true,
197
            ])
198
            ->assertContains('"content-type":["application\/json"]')
199
            ->assertContains('"x-foo":["Bar"]')
200
            ->assertContains('"content":"{\u0022foo\u0022:\u0022bar\u0022}"')
201
            ->assertContains('"ajax":true')
202
            ->post('/http-method', HttpOptions::jsonAjax(['foo' => 'bar'])->withHeader('X-Foo', 'Bar'))
203
            ->assertContains('"content-type":["application\/json"]')
204
            ->assertContains('"x-foo":["Bar"]')
205
            ->assertContains('"content":"{\u0022foo\u0022:\u0022bar\u0022}"')
206
            ->assertContains('"ajax":true')
207
            ->post('/http-method', CustomHttpOptions::api('my-token'))
208
            ->assertContains('"content-type":["application\/json"]')
209
            ->assertContains('"x-token":["my-token"]')
210
            ->post('/http-method', HttpOptions::json()->withHeader('content-type', 'application/ld+json'))
211
            ->assertContains('"content-type":["application\/ld+json"]')
212
            ->post('/http-method?q1=qv1')
213
            ->assertContains('"query":{"q1":"qv1"}')
214
            ->post('/http-method', ['query' => ['q1' => 'qv1']])
215
            ->assertContains('"query":{"q1":"qv1"}')
216
            ->post('/http-method?q1=qv1', ['query' => ['q2' => 'qv2']])
217
            ->assertContains('"query":{"q1":"qv1","q2":"qv2"}')
218
            ->post('/http-method', ['body' => ['b1' => 'bv1']])
219
            ->assertContains('"request":{"b1":"bv1"}')
220
        ;
221
    }
222
223
    /**
224
     * @test
225
     */
226
    public function can_set_default_http_options(): void
227
    {
228
        $this->browser()
229
            ->setDefaultHttpOptions(['headers' => ['x-foo' => 'bar']])
0 ignored issues
show
Bug introduced by
The method setDefaultHttpOptions() does not exist on Zenstruck\Browser. It seems like you code against a sub-type of Zenstruck\Browser such as Zenstruck\Browser\BrowserKitBrowser. ( Ignorable by Annotation )

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

229
            ->/** @scrutinizer ignore-call */ setDefaultHttpOptions(['headers' => ['x-foo' => 'bar']])
Loading history...
230
            ->post('/http-method')
231
            ->assertContains('"x-foo":["Bar"]')
232
            ->post('/http-method', ['headers' => ['x-bar' => 'foo']])
233
            ->assertContains('"x-bar":["Foo"]')
234
            ->assertContains('"x-foo":["Bar"]')
235
        ;
236
    }
237
238
    /**
239
     * @test
240
     */
241
    public function can_handle_any_content_type(): void
242
    {
243
        $this->browser()
244
            ->get('/text')
245
            ->assertHeaderContains('content-type', 'text/plain')
246
            ->assertSuccessful()
247
            ->assertStatus(200)
248
            ->assertContains('text content')
249
        ;
250
    }
251
252
    /**
253
     * @test
254
     */
255
    public function can_assert_json_matches(): void
256
    {
257
        $this->browser()
258
            ->post('/json', ['json' => [
0 ignored issues
show
Bug introduced by
The method post() does not exist on Zenstruck\Browser. It seems like you code against a sub-type of Zenstruck\Browser such as Zenstruck\Browser\BrowserKitBrowser. ( Ignorable by Annotation )

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

258
            ->/** @scrutinizer ignore-call */ post('/json', ['json' => [
Loading history...
259
                'foo' => [
260
                    'bar' => ['baz' => 1],
261
                    'bam' => ['baz' => 2],
262
                    'boo' => ['baz' => 3],
263
                ],
264
            ]])
265
            ->assertJson()
266
            ->assertJsonMatches('foo.bar.baz', 1)
267
            ->assertJsonMatches('foo.*.baz', [1, 2, 3])
268
            ->assertJsonMatches('length(foo)', 3)
269
        ;
270
    }
271
272
    /**
273
     * @test
274
     */
275
    public function assert_json_alternate_content_types(): void
276
    {
277
        $this->browser()
278
            ->get('/json?content-type=application/vnd.custom.json', ['json' => 'foo'])
279
            ->assertSuccessful()
280
            ->assertJson()
281
            ->assertJson('application/vnd.custom.json')
282
        ;
283
    }
284
285
    /**
286
     * @test
287
     */
288
    public function can_dump_empty_json_request(): void
289
    {
290
        $output = self::catchVarDumperOutput(function() {
291
            $this->browser()
292
                ->post('/json')
293
                ->dump()
294
            ;
295
        });
296
297
        $this->assertStringContainsString('content-type: application/json', $output[0]);
0 ignored issues
show
Bug introduced by
It seems like assertStringContainsString() 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

297
        $this->/** @scrutinizer ignore-call */ 
298
               assertStringContainsString('content-type: application/json', $output[0]);
Loading history...
298
    }
299
300
    /**
301
     * @test
302
     */
303
    public function can_dump_json_response_as_array(): void
304
    {
305
        $output = self::catchVarDumperOutput(function() {
306
            $this->browser()
307
                ->post('/json', ['json' => ['foo' => 'bar']])
308
                ->dump()
309
            ;
310
        });
311
312
        $this->assertStringContainsString('    "foo": "bar"', $output[0]);
313
    }
314
315
    /**
316
     * @test
317
     */
318
    public function dump_includes_headers_and_status(): void
319
    {
320
        $output = self::catchVarDumperOutput(function() {
321
            $this->browser()
322
                ->visit('/page1')
323
                ->dump()
324
            ;
325
        });
326
327
        $this->assertStringContainsString('(200)', $output[0]);
328
        $this->assertStringContainsString('content-type: text/html;', $output[0]);
329
    }
330
331
    /**
332
     * @test
333
     */
334
    public function can_dump_json_array_key(): void
335
    {
336
        $output = self::catchVarDumperOutput(function() {
337
            $this->browser()
338
                ->post('/json', ['json' => ['foo' => 'bar']])
339
                ->dump('foo')
340
            ;
341
        });
342
343
        $this->assertSame('bar', $output[0]);
0 ignored issues
show
Bug introduced by
The method assertSame() does not exist on Zenstruck\Browser\Tests\BrowserKitBrowserTests. Did you maybe mean assert_on()? ( Ignorable by Annotation )

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

343
        $this->/** @scrutinizer ignore-call */ 
344
               assertSame('bar', $output[0]);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
344
    }
345
346
    /**
347
     * @test
348
     */
349
    public function can_dump_json_path_expression(): void
350
    {
351
        $output = self::catchVarDumperOutput(function() {
352
            $this->browser()
353
                ->post('/json', ['json' => [
354
                    'foo' => [
355
                        'bar' => ['baz' => 1],
356
                        'bam' => ['baz' => 2],
357
                        'boo' => ['baz' => 3],
358
                    ],
359
                ]])
360
                ->dump('foo.*.baz')
361
            ;
362
        });
363
364
        $this->assertSame([1, 2, 3], $output[0]);
365
    }
366
367
    /**
368
     * @test
369
     */
370
    public function can_save_formatted_json_source(): void
371
    {
372
        $contents = self::catchFileContents(__DIR__.'/../var/browser/source/source.txt', function() {
373
            $this->browser()
374
                ->visit('/http-method')
375
                ->saveSource('/source.txt')
376
            ;
377
        });
378
379
        $this->assertStringContainsString('/http-method', $contents);
380
        $this->assertStringContainsString('    "content": "",', $contents);
381
    }
382
383
    /**
384
     * @test
385
     */
386
    public function can_access_json_response(): void
387
    {
388
        $response = $this->browser()
389
            ->post('/json', ['json' => $expected = ['foo' => 'bar']])
390
            ->assertSuccessful()
391
            ->response()
392
            ->assertJson()
393
        ;
394
395
        $this->assertSame($expected, $response->json());
396
        $this->assertSame('bar', $response->search('foo'));
397
    }
398
399
    /**
400
     * @test
401
     */
402
    public function can_access_the_profiler(): void
403
    {
404
        $profile = $this->browser()
405
            ->visit('/page1')
406
            ->profile()
0 ignored issues
show
Bug introduced by
The method profile() does not exist on Zenstruck\Browser. It seems like you code against a sub-type of Zenstruck\Browser such as Zenstruck\Browser\BrowserKitBrowser. ( Ignorable by Annotation )

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

406
            ->/** @scrutinizer ignore-call */ profile()
Loading history...
407
        ;
408
409
        $this->assertTrue($profile->hasCollector('request'));
0 ignored issues
show
Bug introduced by
The method assertTrue() does not exist on Zenstruck\Browser\Tests\BrowserKitBrowserTests. Did you maybe mean assert_on()? ( Ignorable by Annotation )

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

409
        $this->/** @scrutinizer ignore-call */ 
410
               assertTrue($profile->hasCollector('request'));

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
410
    }
411
412
    /**
413
     * @test
414
     */
415
    public function can_dump_xml_selector(): void
416
    {
417
        $output = self::catchVarDumperOutput(function() {
418
            $this->browser()
419
                ->visit('/xml')
420
                ->dump('url loc')
421
            ;
422
        });
423
424
        $this->assertCount(2, $output);
0 ignored issues
show
Bug introduced by
The method assertCount() does not exist on Zenstruck\Browser\Tests\BrowserKitBrowserTests. Did you maybe mean assert_on()? ( Ignorable by Annotation )

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

424
        $this->/** @scrutinizer ignore-call */ 
425
               assertCount(2, $output);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
425
        $this->assertSame('<loc>https://www.example.com/page1</loc>', $output[0]);
426
        $this->assertSame('<loc attr="attribute">https://www.example.com/page2</loc>', $output[1]);
427
    }
428
429
    /**
430
     * @test
431
     */
432
    public function can_access_the_xml_crawler(): void
433
    {
434
        $crawler = $this->browser()
435
            ->visit('/xml')
436
            ->response()
437
            ->assertXml()
438
            ->crawler()
439
            ->filter('url loc')
440
        ;
441
442
        $this->assertCount(2, $crawler);
443
    }
444
}
445