Passed
Pull Request — 1.x (#74)
by Kevin
02:19
created

BrowserKitBrowserTests::can_use_data_collector()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 6
rs 10
c 1
b 0
f 1
1
<?php
2
3
namespace Zenstruck\Browser\Tests;
4
5
use Symfony\Component\HttpKernel\DataCollector\RequestDataCollector;
6
use Zenstruck\Browser\HttpOptions;
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 http_method_actions(): void
176
    {
177
        $this->browser()
178
            ->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

178
            ->/** @scrutinizer ignore-call */ get('/http-method')
Loading history...
179
            ->assertSuccessful()
180
            ->assertContains('"method":"GET"')
181
            ->post('/http-method')
182
            ->assertSuccessful()
183
            ->assertContains('"method":"POST"')
184
            ->patch('/http-method')
185
            ->assertSuccessful()
186
            ->assertContains('"method":"PATCH"')
187
            ->delete('/http-method')
188
            ->assertSuccessful()
189
            ->assertContains('"method":"DELETE"')
190
            ->put('/http-method')
191
            ->assertSuccessful()
192
            ->assertContains('"method":"PUT"')
193
            ->assertContains('"ajax":false')
194
            ->post('/http-method', [
195
                'json' => ['foo' => 'bar'],
196
                'headers' => ['X-Foo' => 'Bar'],
197
                'ajax' => true,
198
            ])
199
            ->assertContains('"content-type":["application\/json"]')
200
            ->assertContains('"x-foo":["Bar"]')
201
            ->assertContains('"content":"{\u0022foo\u0022:\u0022bar\u0022}"')
202
            ->assertContains('"ajax":true')
203
            ->post('/http-method', HttpOptions::jsonAjax(['foo' => 'bar'])->withHeader('X-Foo', 'Bar'))
204
            ->assertContains('"content-type":["application\/json"]')
205
            ->assertContains('"x-foo":["Bar"]')
206
            ->assertContains('"content":"{\u0022foo\u0022:\u0022bar\u0022}"')
207
            ->assertContains('"ajax":true')
208
            ->post('/http-method', CustomHttpOptions::api('my-token'))
209
            ->assertContains('"content-type":["application\/json"]')
210
            ->assertContains('"x-token":["my-token"]')
211
            ->post('/http-method', HttpOptions::json()->withHeader('content-type', 'application/ld+json'))
212
            ->assertContains('"content-type":["application\/ld+json"]')
213
            ->post('/http-method?q1=qv1')
214
            ->assertContains('"query":{"q1":"qv1"}')
215
            ->post('/http-method', ['query' => ['q1' => 'qv1']])
216
            ->assertContains('"query":{"q1":"qv1"}')
217
            ->post('/http-method?q1=qv1', ['query' => ['q2' => 'qv2']])
218
            ->assertContains('"query":{"q1":"qv1","q2":"qv2"}')
219
            ->post('/http-method', ['body' => ['b1' => 'bv1']])
220
            ->assertContains('"request":{"b1":"bv1"}')
221
        ;
222
    }
223
224
    /**
225
     * @test
226
     */
227
    public function can_set_default_http_options(): void
228
    {
229
        $this->browser()
230
            ->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

230
            ->/** @scrutinizer ignore-call */ setDefaultHttpOptions(['headers' => ['x-foo' => 'bar']])
Loading history...
231
            ->post('/http-method')
232
            ->assertContains('"x-foo":["Bar"]')
233
            ->post('/http-method', ['headers' => ['x-bar' => 'foo']])
234
            ->assertContains('"x-bar":["Foo"]')
235
            ->assertContains('"x-foo":["Bar"]')
236
        ;
237
    }
238
239
    /**
240
     * @test
241
     */
242
    public function can_handle_any_content_type(): void
243
    {
244
        $this->browser()
245
            ->get('/text')
246
            ->assertHeaderContains('content-type', 'text/plain')
247
            ->assertSuccessful()
248
            ->assertStatus(200)
249
            ->assertContains('text content')
250
        ;
251
    }
252
253
    /**
254
     * @test
255
     */
256
    public function can_assert_json_matches(): void
257
    {
258
        $this->browser()
259
            ->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

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

298
        $this->/** @scrutinizer ignore-call */ 
299
               assertStringContainsString('content-type: application/json', $output[0]);
Loading history...
299
    }
300
301
    /**
302
     * @test
303
     */
304
    public function can_dump_json_response_as_array(): void
305
    {
306
        $output = self::catchVarDumperOutput(function() {
307
            $this->browser()
308
                ->post('/json', ['json' => ['foo' => 'bar']])
309
                ->dump()
310
            ;
311
        });
312
313
        $this->assertStringContainsString('    "foo": "bar"', $output[0]);
314
    }
315
316
    /**
317
     * @test
318
     */
319
    public function dump_includes_headers_and_status(): void
320
    {
321
        $output = self::catchVarDumperOutput(function() {
322
            $this->browser()
323
                ->visit('/page1')
324
                ->dump()
325
            ;
326
        });
327
328
        $this->assertStringContainsString('(200)', $output[0]);
329
        $this->assertStringContainsString('content-type: text/html;', $output[0]);
330
    }
331
332
    /**
333
     * @test
334
     */
335
    public function can_dump_json_array_key(): void
336
    {
337
        $output = self::catchVarDumperOutput(function() {
338
            $this->browser()
339
                ->post('/json', ['json' => ['foo' => 'bar']])
340
                ->dump('foo')
341
            ;
342
        });
343
344
        $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

344
        $this->/** @scrutinizer ignore-call */ 
345
               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...
345
    }
346
347
    /**
348
     * @test
349
     */
350
    public function can_dump_json_path_expression(): void
351
    {
352
        $output = self::catchVarDumperOutput(function() {
353
            $this->browser()
354
                ->post('/json', ['json' => [
355
                    'foo' => [
356
                        'bar' => ['baz' => 1],
357
                        'bam' => ['baz' => 2],
358
                        'boo' => ['baz' => 3],
359
                    ],
360
                ]])
361
                ->dump('foo.*.baz')
362
            ;
363
        });
364
365
        $this->assertSame([1, 2, 3], $output[0]);
366
    }
367
368
    /**
369
     * @test
370
     */
371
    public function can_save_formatted_json_source(): void
372
    {
373
        $contents = self::catchFileContents(__DIR__.'/../var/browser/source/source.txt', function() {
374
            $this->browser()
375
                ->visit('/http-method')
376
                ->saveSource('/source.txt')
377
            ;
378
        });
379
380
        $this->assertStringContainsString('/http-method', $contents);
381
        $this->assertStringContainsString('    "content": "",', $contents);
382
    }
383
384
    /**
385
     * @test
386
     */
387
    public function can_access_json_response(): void
388
    {
389
        $response = $this->browser()
390
            ->post('/json', ['json' => $expected = ['foo' => 'bar']])
391
            ->assertSuccessful()
392
            ->response()
393
            ->assertJson()
394
        ;
395
396
        $this->assertSame($expected, $response->json());
397
        $this->assertSame('bar', $response->search('foo'));
398
    }
399
400
    /**
401
     * @test
402
     */
403
    public function can_access_the_profiler(): void
404
    {
405
        $profile = $this->browser()
406
            ->visit('/page1')
407
            ->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

407
            ->/** @scrutinizer ignore-call */ profile()
Loading history...
408
        ;
409
410
        $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

410
        $this->/** @scrutinizer ignore-call */ 
411
               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...
411
    }
412
413
    /**
414
     * @test
415
     */
416
    public function can_dump_xml_selector(): void
417
    {
418
        $output = self::catchVarDumperOutput(function() {
419
            $this->browser()
420
                ->visit('/xml')
421
                ->dump('url loc')
422
            ;
423
        });
424
425
        $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

425
        $this->/** @scrutinizer ignore-call */ 
426
               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...
426
        $this->assertSame('<loc>https://www.example.com/page1</loc>', $output[0]);
427
        $this->assertSame('<loc attr="attribute">https://www.example.com/page2</loc>', $output[1]);
428
    }
429
430
    /**
431
     * @test
432
     */
433
    public function can_access_the_xml_crawler(): void
434
    {
435
        $crawler = $this->browser()
436
            ->visit('/xml')
437
            ->response()
438
            ->assertXml()
439
            ->crawler()
440
            ->filter('url loc')
441
        ;
442
443
        $this->assertCount(2, $crawler);
444
    }
445
446
    /**
447
     * @test
448
     */
449
    public function can_use_data_collector(): void
450
    {
451
        $this->browser()
452
            ->visit('/page1')
453
            ->use(function(RequestDataCollector $collector) {
454
                $this->assertSame('/page1', $collector->getPathInfo());
455
            })
456
        ;
457
    }
458
}
459