Completed
Push — 1.x ( b6efad...f02a91 )
by Kevin
16s queued 12s
created

can_access_the_xml_crawler()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 0
dl 0
loc 11
rs 10
c 0
b 0
f 0
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
            ->delete('/http-method')
184
            ->assertSuccessful()
185
            ->assertContains('"method":"DELETE"')
186
            ->put('/http-method')
187
            ->assertSuccessful()
188
            ->assertContains('"method":"PUT"')
189
            ->assertContains('"ajax":false')
190
            ->post('/http-method', [
191
                'json' => ['foo' => 'bar'],
192
                'headers' => ['X-Foo' => 'Bar'],
193
                'ajax' => true,
194
            ])
195
            ->assertContains('"content-type":["application\/json"]')
196
            ->assertContains('"x-foo":["Bar"]')
197
            ->assertContains('"content":"{\u0022foo\u0022:\u0022bar\u0022}"')
198
            ->assertContains('"ajax":true')
199
            ->post('/http-method', HttpOptions::jsonAjax(['foo' => 'bar'])->withHeader('X-Foo', 'Bar'))
200
            ->assertContains('"content-type":["application\/json"]')
201
            ->assertContains('"x-foo":["Bar"]')
202
            ->assertContains('"content":"{\u0022foo\u0022:\u0022bar\u0022}"')
203
            ->assertContains('"ajax":true')
204
            ->post('/http-method', CustomHttpOptions::api('my-token'))
205
            ->assertContains('"content-type":["application\/json"]')
206
            ->assertContains('"x-token":["my-token"]')
207
        ;
208
    }
209
210
    /**
211
     * @test
212
     */
213
    public function can_set_default_http_options(): void
214
    {
215
        $this->browser()
216
            ->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

216
            ->/** @scrutinizer ignore-call */ setDefaultHttpOptions(['headers' => ['x-foo' => 'bar']])
Loading history...
217
            ->post('/http-method')
218
            ->assertContains('"x-foo":["Bar"]')
219
            ->post('/http-method', ['headers' => ['x-bar' => 'foo']])
220
            ->assertContains('"x-bar":["Foo"]')
221
            ->assertContains('"x-foo":["Bar"]')
222
        ;
223
    }
224
225
    /**
226
     * @test
227
     */
228
    public function can_handle_any_content_type(): void
229
    {
230
        $this->browser()
231
            ->get('/text')
232
            ->assertHeaderContains('content-type', 'text/plain')
233
            ->assertSuccessful()
234
            ->assertStatus(200)
235
            ->assertContains('text content')
236
        ;
237
    }
238
239
    /**
240
     * @test
241
     */
242
    public function can_assert_json_matches(): void
243
    {
244
        $this->browser()
245
            ->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

245
            ->/** @scrutinizer ignore-call */ post('/json', ['json' => [
Loading history...
246
                'foo' => [
247
                    'bar' => ['baz' => 1],
248
                    'bam' => ['baz' => 2],
249
                    'boo' => ['baz' => 3],
250
                ],
251
            ]])
252
            ->assertJson()
253
            ->assertJsonMatches('foo.bar.baz', 1)
254
            ->assertJsonMatches('foo.*.baz', [1, 2, 3])
255
            ->assertJsonMatches('length(foo)', 3)
256
        ;
257
    }
258
259
    /**
260
     * @test
261
     */
262
    public function assert_json_alternate_content_types(): void
263
    {
264
        $this->browser()
265
            ->get('/json?content-type=application/vnd.custom.json', ['json' => 'foo'])
266
            ->assertSuccessful()
267
            ->assertJson()
268
            ->assertJson('application/vnd.custom.json')
269
        ;
270
    }
271
272
    /**
273
     * @test
274
     */
275
    public function can_dump_empty_json_request(): void
276
    {
277
        $output = self::catchVarDumperOutput(function() {
278
            $this->browser()
279
                ->post('/json')
280
                ->dump()
281
            ;
282
        });
283
284
        $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

284
        $this->/** @scrutinizer ignore-call */ 
285
               assertStringContainsString('content-type: application/json', $output[0]);
Loading history...
285
    }
286
287
    /**
288
     * @test
289
     */
290
    public function can_dump_json_response_as_array(): void
291
    {
292
        $output = self::catchVarDumperOutput(function() {
293
            $this->browser()
294
                ->post('/json', ['json' => ['foo' => 'bar']])
295
                ->dump()
296
            ;
297
        });
298
299
        $this->assertStringContainsString('    "foo": "bar"', $output[0]);
300
    }
301
302
    /**
303
     * @test
304
     */
305
    public function dump_includes_headers_and_status(): void
306
    {
307
        $output = self::catchVarDumperOutput(function() {
308
            $this->browser()
309
                ->visit('/page1')
310
                ->dump()
311
            ;
312
        });
313
314
        $this->assertStringContainsString('(200)', $output[0]);
315
        $this->assertStringContainsString('content-type: text/html;', $output[0]);
316
    }
317
318
    /**
319
     * @test
320
     */
321
    public function can_dump_json_array_key(): void
322
    {
323
        $output = self::catchVarDumperOutput(function() {
324
            $this->browser()
325
                ->post('/json', ['json' => ['foo' => 'bar']])
326
                ->dump('foo')
327
            ;
328
        });
329
330
        $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

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

393
            ->/** @scrutinizer ignore-call */ profile()
Loading history...
394
        ;
395
396
        $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

396
        $this->/** @scrutinizer ignore-call */ 
397
               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...
397
    }
398
399
    /**
400
     * @test
401
     */
402
    public function can_dump_xml_selector(): void
403
    {
404
        $output = self::catchVarDumperOutput(function() {
405
            $this->browser()
406
                ->visit('/xml')
407
                ->dump('url loc')
408
            ;
409
        });
410
411
        $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

411
        $this->/** @scrutinizer ignore-call */ 
412
               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...
412
        $this->assertSame('https://www.example.com/page1', $output[0]);
413
        $this->assertSame('https://www.example.com/page2', $output[1]);
414
    }
415
416
    /**
417
     * @test
418
     */
419
    public function can_access_the_xml_crawler(): void
420
    {
421
        $crawler = $this->browser()
422
            ->visit('/xml')
423
            ->response()
424
            ->assertXml()
425
            ->crawler()
426
            ->filter('url loc')
427
        ;
428
429
        $this->assertCount(2, $crawler);
430
    }
431
}
432