Passed
Pull Request — 1.x (#62)
by Kevin
02:14
created

response_multi_header_assertions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 11
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 13
rs 9.9
1
<?php
2
3
namespace Zenstruck\Browser\Tests;
4
5
use Zenstruck\Browser\HttpOptions;
6
use Zenstruck\Browser\Response;
7
use Zenstruck\Browser\Tests\Fixture\CustomHttpOptions;
8
9
/**
10
 * @author Kevin Bond <[email protected]>
11
 */
12
trait BrowserKitBrowserTests
13
{
14
    use BrowserTests;
15
16
    /**
17
     * @test
18
     */
19
    public function following_redirect_follows_all_by_default(): void
20
    {
21
        $this->browser()
22
            ->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

22
            ->/** @scrutinizer ignore-call */ interceptRedirects()
Loading history...
23
            ->visit('/redirect1')
24
            ->assertOn('/redirect1')
25
            ->followRedirect()
26
            ->assertOn('/page1')
27
            ->assertSuccessful()
28
        ;
29
    }
30
31
    /**
32
     * @test
33
     */
34
    public function can_re_enable_following_redirects(): void
35
    {
36
        $this->browser()
37
            ->interceptRedirects()
38
            ->visit('/redirect1')
39
            ->assertOn('/redirect1')
40
            ->followRedirects()
41
            ->visit('/redirect1')
42
            ->assertOn('/page1')
43
        ;
44
    }
45
46
    /**
47
     * @test
48
     */
49
    public function calling_follow_redirects_when_the_response_is_a_redirect_follows_the_redirect(): void
50
    {
51
        $this->browser()
52
            ->interceptRedirects()
53
            ->visit('/redirect1')
54
            ->followRedirects()
55
            ->assertOn('/page1')
56
            ->interceptRedirects()
57
            ->visit('/page1')
58
            ->followRedirects()
59
            ->assertOn('/page1')
60
        ;
61
    }
62
63
    /**
64
     * @test
65
     */
66
    public function calling_follow_redirects_before_a_request_has_been_made_just_enables_following_redirects(): void
67
    {
68
        $this->browser()
69
            ->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

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

126
        $this->/** @scrutinizer ignore-call */ 
127
               expectException(\RuntimeException::class);
Loading history...
127
        $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

127
        $this->/** @scrutinizer ignore-call */ 
128
               expectExceptionMessage('Cannot assert redirected if not intercepting redirects. Call ->interceptRedirects() before making the request.');
Loading history...
128
129
        $this->browser()
130
            ->visit('/redirect1')
131
            ->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

131
            ->/** @scrutinizer ignore-call */ assertRedirected()
Loading history...
132
        ;
133
    }
134
135
    /**
136
     * @test
137
     */
138
    public function exception_thrown_if_asserting_redirected_to_and_not_intercepting_redirects(): void
139
    {
140
        $this->expectException(\RuntimeException::class);
141
        $this->expectExceptionMessage('Cannot assert redirected if not intercepting redirects. Call ->interceptRedirects() before making the request.');
142
143
        $this->browser()
144
            ->visit('/redirect1')
145
            ->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

145
            ->/** @scrutinizer ignore-call */ assertRedirectedTo('/page1')
Loading history...
146
        ;
147
    }
148
149
    /**
150
     * @test
151
     */
152
    public function exceptions_are_caught_by_default(): void
153
    {
154
        $this->browser()
155
            ->visit('/exception')
156
            ->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

156
            ->/** @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...
157
        ;
158
    }
159
160
    /**
161
     * @test
162
     */
163
    public function response_header_assertions(): void
164
    {
165
        $this->browser()
166
            ->visit('/page1')
167
            ->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

167
            ->/** @scrutinizer ignore-call */ assertHeaderEquals('Content-Type', 'text/html; charset=UTF-8')
Loading history...
168
            ->assertHeaderContains('Content-Type', 'text/html')
169
        ;
170
    }
171
172
    /**
173
     * @test
174
     */
175
    public function response_multi_header_assertions(): void
176
    {
177
        $this->browser()
178
            ->visit('/multi-headers')
179
            ->assertSuccessful()
0 ignored issues
show
Bug introduced by
The method assertSuccessful() 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

179
            ->/** @scrutinizer ignore-call */ assertSuccessful()
Loading history...
180
            ->assertHeaderEquals('X-FOO', 'bar 1')
181
            ->assertHeaderEquals('x-foo', 'bar 1')
182
            ->assertHeaderEquals('x-foo', 'bar 2')
183
            ->assertHeaderContains('x-foo', '1')
184
            ->assertHeaderContains('x-foo', '2')
185
            ->use(function(Response $response) {
186
                $this->assertStringContainsString('X-Foo:         bar 1', $response->raw());
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

186
                $this->/** @scrutinizer ignore-call */ 
187
                       assertStringContainsString('X-Foo:         bar 1', $response->raw());
Loading history...
187
                $this->assertStringContainsString('X-Foo:         bar 1', $response->raw());
188
            })
189
        ;
190
    }
191
192
    /**
193
     * @test
194
     */
195
    public function http_method_actions(): void
196
    {
197
        $this->browser()
198
            ->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

198
            ->/** @scrutinizer ignore-call */ get('/http-method')
Loading history...
199
            ->assertSuccessful()
200
            ->assertContains('"method":"GET"')
201
            ->post('/http-method')
202
            ->assertSuccessful()
203
            ->assertContains('"method":"POST"')
204
            ->delete('/http-method')
205
            ->assertSuccessful()
206
            ->assertContains('"method":"DELETE"')
207
            ->put('/http-method')
208
            ->assertSuccessful()
209
            ->assertContains('"method":"PUT"')
210
            ->assertContains('"ajax":false')
211
            ->post('/http-method', [
212
                'json' => ['foo' => 'bar'],
213
                'headers' => ['X-Foo' => 'Bar'],
214
                'ajax' => true,
215
            ])
216
            ->assertContains('"content-type":["application\/json"]')
217
            ->assertContains('"x-foo":["Bar"]')
218
            ->assertContains('"content":"{\u0022foo\u0022:\u0022bar\u0022}"')
219
            ->assertContains('"ajax":true')
220
            ->post('/http-method', HttpOptions::jsonAjax(['foo' => 'bar'])->withHeader('X-Foo', 'Bar'))
221
            ->assertContains('"content-type":["application\/json"]')
222
            ->assertContains('"x-foo":["Bar"]')
223
            ->assertContains('"content":"{\u0022foo\u0022:\u0022bar\u0022}"')
224
            ->assertContains('"ajax":true')
225
            ->post('/http-method', CustomHttpOptions::api('my-token'))
226
            ->assertContains('"content-type":["application\/json"]')
227
            ->assertContains('"x-token":["my-token"]')
228
            ->post('/http-method', HttpOptions::json()->withHeader('content-type', 'application/ld+json'))
229
            ->assertContains('"content-type":["application\/ld+json"]')
230
            ->post('/http-method?q1=qv1')
231
            ->assertContains('"query":{"q1":"qv1"}')
232
            ->post('/http-method', ['query' => ['q1' => 'qv1']])
233
            ->assertContains('"query":{"q1":"qv1"}')
234
            ->post('/http-method?q1=qv1', ['query' => ['q2' => 'qv2']])
235
            ->assertContains('"query":{"q1":"qv1","q2":"qv2"}')
236
            ->post('/http-method', ['body' => ['b1' => 'bv1']])
237
            ->assertContains('"request":{"b1":"bv1"}')
238
        ;
239
    }
240
241
    /**
242
     * @test
243
     */
244
    public function can_set_default_http_options(): void
245
    {
246
        $this->browser()
247
            ->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

247
            ->/** @scrutinizer ignore-call */ setDefaultHttpOptions(['headers' => ['x-foo' => 'bar']])
Loading history...
248
            ->post('/http-method')
249
            ->assertContains('"x-foo":["bar"]')
250
            ->post('/http-method', ['headers' => ['x-bar' => 'foo']])
251
            ->assertContains('"x-bar":["foo"]')
252
            ->assertContains('"x-foo":["bar"]')
253
        ;
254
    }
255
256
    /**
257
     * @test
258
     */
259
    public function can_handle_any_content_type(): void
260
    {
261
        $this->browser()
262
            ->get('/text')
263
            ->assertHeaderContains('content-type', 'text/plain')
264
            ->assertSuccessful()
265
            ->assertStatus(200)
266
            ->assertContains('text content')
267
        ;
268
    }
269
270
    /**
271
     * @test
272
     */
273
    public function can_assert_json_matches(): void
274
    {
275
        $this->browser()
276
            ->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

276
            ->/** @scrutinizer ignore-call */ post('/json', ['json' => [
Loading history...
277
                'foo' => [
278
                    'bar' => ['baz' => 1],
279
                    'bam' => ['baz' => 2],
280
                    'boo' => ['baz' => 3],
281
                ],
282
            ]])
283
            ->assertJson()
284
            ->assertJsonMatches('foo.bar.baz', 1)
285
            ->assertJsonMatches('foo.*.baz', [1, 2, 3])
286
            ->assertJsonMatches('length(foo)', 3)
287
        ;
288
    }
289
290
    /**
291
     * @test
292
     */
293
    public function assert_json_alternate_content_types(): void
294
    {
295
        $this->browser()
296
            ->get('/json?content-type=application/vnd.custom.json', ['json' => 'foo'])
297
            ->assertSuccessful()
298
            ->assertJson()
299
            ->assertJson('application/vnd.custom.json')
300
        ;
301
    }
302
303
    /**
304
     * @test
305
     */
306
    public function can_dump_empty_json_request(): void
307
    {
308
        $output = self::catchVarDumperOutput(function() {
309
            $this->browser()
310
                ->post('/json')
311
                ->dump()
312
            ;
313
        });
314
315
        $this->assertStringContainsString('Content-Type:  application/json', $output[0]);
316
    }
317
318
    /**
319
     * @test
320
     */
321
    public function can_dump_json_response_as_array(): void
322
    {
323
        $output = self::catchVarDumperOutput(function() {
324
            $this->browser()
325
                ->post('/json', ['json' => ['foo' => 'bar']])
326
                ->dump()
327
            ;
328
        });
329
330
        $this->assertStringContainsString('    "foo": "bar"', $output[0]);
331
    }
332
333
    /**
334
     * @test
335
     */
336
    public function dump_includes_headers_and_status(): void
337
    {
338
        $output = self::catchVarDumperOutput(function() {
339
            $this->browser()
340
                ->visit('/page1')
341
                ->dump()
342
            ;
343
        });
344
345
        $this->assertStringContainsString('(200)', $output[0]);
346
        $this->assertStringContainsString('Content-Type:  text/html;', $output[0]);
347
    }
348
349
    /**
350
     * @test
351
     */
352
    public function can_dump_json_array_key(): void
353
    {
354
        $output = self::catchVarDumperOutput(function() {
355
            $this->browser()
356
                ->post('/json', ['json' => ['foo' => 'bar']])
357
                ->dump('foo')
358
            ;
359
        });
360
361
        $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

361
        $this->/** @scrutinizer ignore-call */ 
362
               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...
362
    }
363
364
    /**
365
     * @test
366
     */
367
    public function can_dump_json_path_expression(): void
368
    {
369
        $output = self::catchVarDumperOutput(function() {
370
            $this->browser()
371
                ->post('/json', ['json' => [
372
                    'foo' => [
373
                        'bar' => ['baz' => 1],
374
                        'bam' => ['baz' => 2],
375
                        'boo' => ['baz' => 3],
376
                    ],
377
                ]])
378
                ->dump('foo.*.baz')
379
            ;
380
        });
381
382
        $this->assertSame([1, 2, 3], $output[0]);
383
    }
384
385
    /**
386
     * @test
387
     */
388
    public function can_save_formatted_json_source(): void
389
    {
390
        $contents = self::catchFileContents(__DIR__.'/../var/browser/source/source.txt', function() {
391
            $this->browser()
392
                ->visit('/http-method')
393
                ->saveSource('/source.txt')
394
            ;
395
        });
396
397
        $this->assertStringContainsString('/http-method', $contents);
398
        $this->assertStringContainsString('    "content": "",', $contents);
399
    }
400
401
    /**
402
     * @test
403
     */
404
    public function can_access_json_response(): void
405
    {
406
        $response = $this->browser()
407
            ->post('/json', ['json' => $expected = ['foo' => 'bar']])
408
            ->assertSuccessful()
409
            ->response()
410
            ->ensureJson()
411
        ;
412
413
        $this->assertSame($expected, $response->json());
414
        $this->assertSame('bar', $response->search('foo'));
415
    }
416
417
    /**
418
     * @test
419
     */
420
    public function can_access_the_profiler(): void
421
    {
422
        $profile = $this->browser()
423
            ->visit('/page1')
424
            ->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

424
            ->/** @scrutinizer ignore-call */ profile()
Loading history...
425
        ;
426
427
        $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

427
        $this->/** @scrutinizer ignore-call */ 
428
               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...
428
    }
429
430
    /**
431
     * @test
432
     */
433
    public function can_dump_xml_selector(): void
434
    {
435
        $output = self::catchVarDumperOutput(function() {
436
            $this->browser()
437
                ->visit('/xml')
438
                ->dump('url loc')
439
            ;
440
        });
441
442
        $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

442
        $this->/** @scrutinizer ignore-call */ 
443
               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...
443
        $this->assertSame('<loc>https://www.example.com/page1</loc>', $output[0]);
444
        $this->assertSame('<loc attr="attribute">https://www.example.com/page2</loc>', $output[1]);
445
    }
446
447
    /**
448
     * @test
449
     */
450
    public function can_access_the_xml_crawler(): void
451
    {
452
        $crawler = $this->browser()
453
            ->visit('/xml')
454
            ->response()
455
            ->ensureXml()
456
            ->crawler()
457
            ->filter('url loc')
458
        ;
459
460
        $this->assertCount(2, $crawler);
461
    }
462
}
463