Passed
Push — 1.x ( bfdb67...14afbb )
by Kevin
08:20
created

BrowserKitBrowserTests::http_method_actions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 33
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 30
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 33
rs 9.44
1
<?php
2
3
namespace Zenstruck\Browser\Tests;
4
5
use Zenstruck\Browser\HttpOptions;
6
use Zenstruck\Browser\Tests\Component\EmailTests;
7
use Zenstruck\Browser\Tests\Fixture\CustomHttpOptions;
8
9
/**
10
 * @author Kevin Bond <[email protected]>
11
 */
12
trait BrowserKitBrowserTests
13
{
14
    use BrowserTests, EmailTests;
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 exceptions_are_caught_by_default(): void
125
    {
126
        $this->browser()
127
            ->visit('/exception')
128
            ->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

128
            ->/** @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...
129
        ;
130
    }
131
132
    /**
133
     * @test
134
     */
135
    public function response_header_assertions(): void
136
    {
137
        $this->browser()
138
            ->visit('/page1')
139
            ->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

139
            ->/** @scrutinizer ignore-call */ assertHeaderEquals('Content-Type', 'text/html; charset=UTF-8')
Loading history...
140
            ->assertHeaderContains('Content-Type', 'text/html')
141
        ;
142
    }
143
144
    /**
145
     * @test
146
     */
147
    public function http_method_actions(): void
148
    {
149
        $this->browser()
150
            ->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

150
            ->/** @scrutinizer ignore-call */ get('/http-method')
Loading history...
151
            ->assertSuccessful()
152
            ->assertContains('"method":"GET"')
153
            ->post('/http-method')
154
            ->assertSuccessful()
155
            ->assertContains('"method":"POST"')
156
            ->delete('/http-method')
157
            ->assertSuccessful()
158
            ->assertContains('"method":"DELETE"')
159
            ->put('/http-method')
160
            ->assertSuccessful()
161
            ->assertContains('"method":"PUT"')
162
            ->assertContains('"ajax":false')
163
            ->post('/http-method', [
164
                'json' => ['foo' => 'bar'],
165
                'headers' => ['X-Foo' => 'Bar'],
166
                'ajax' => true,
167
            ])
168
            ->assertContains('"content-type":["application\/json"]')
169
            ->assertContains('"x-foo":["Bar"]')
170
            ->assertContains('"content":"{\u0022foo\u0022:\u0022bar\u0022}"')
171
            ->assertContains('"ajax":true')
172
            ->post('/http-method', HttpOptions::jsonAjax(['foo' => 'bar'])->withHeader('X-Foo', 'Bar'))
173
            ->assertContains('"content-type":["application\/json"]')
174
            ->assertContains('"x-foo":["Bar"]')
175
            ->assertContains('"content":"{\u0022foo\u0022:\u0022bar\u0022}"')
176
            ->assertContains('"ajax":true')
177
            ->post('/http-method', CustomHttpOptions::api('my-token'))
178
            ->assertContains('"content-type":["application\/json"]')
179
            ->assertContains('"x-token":["my-token"]')
180
        ;
181
    }
182
183
    /**
184
     * @test
185
     */
186
    public function can_set_default_http_options(): void
187
    {
188
        $this->browser()
189
            ->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

189
            ->/** @scrutinizer ignore-call */ setDefaultHttpOptions(['headers' => ['x-foo' => 'bar']])
Loading history...
190
            ->post('/http-method')
191
            ->assertContains('"x-foo":["Bar"]')
192
            ->post('/http-method', ['headers' => ['x-bar' => 'foo']])
193
            ->assertContains('"x-bar":["Foo"]')
194
            ->assertContains('"x-foo":["Bar"]')
195
        ;
196
    }
197
198
    /**
199
     * @test
200
     */
201
    public function can_handle_any_content_type(): void
202
    {
203
        $this->browser()
204
            ->get('/text')
205
            ->assertHeaderContains('content-type', 'text/plain')
206
            ->assertSuccessful()
207
            ->assertStatus(200)
208
            ->assertContains('text content')
209
        ;
210
    }
211
212
    /**
213
     * @test
214
     */
215
    public function can_assert_json_matches(): void
216
    {
217
        $this->browser()
218
            ->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

218
            ->/** @scrutinizer ignore-call */ post('/json', ['json' => [
Loading history...
219
                'foo' => [
220
                    'bar' => ['baz' => 1],
221
                    'bam' => ['baz' => 2],
222
                    'boo' => ['baz' => 3],
223
                ],
224
            ]])
225
            ->assertJsonMatches('foo.bar.baz', 1)
226
            ->assertJsonMatches('foo.*.baz', [1, 2, 3])
227
            ->assertJsonMatches('length(foo)', 3)
228
        ;
229
    }
230
231
    /**
232
     * @test
233
     */
234
    public function can_dump_json_response_as_array(): void
235
    {
236
        $output = self::catchVarDumperOutput(function() {
237
            $this->browser()
238
                ->post('/json', ['json' => ['foo' => 'bar']])
239
                ->dump()
240
            ;
241
        });
242
243
        $this->assertStringContainsString('    "foo": "bar"', $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

243
        $this->/** @scrutinizer ignore-call */ 
244
               assertStringContainsString('    "foo": "bar"', $output[0]);
Loading history...
244
    }
245
246
    /**
247
     * @test
248
     */
249
    public function can_dump_json_array_key(): void
250
    {
251
        $output = self::catchVarDumperOutput(function() {
252
            $this->browser()
253
                ->post('/json', ['json' => ['foo' => 'bar']])
254
                ->dump('foo')
255
            ;
256
        });
257
258
        $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

258
        $this->/** @scrutinizer ignore-call */ 
259
               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...
259
    }
260
261
    /**
262
     * @test
263
     */
264
    public function can_dump_json_path_expression(): void
265
    {
266
        $output = self::catchVarDumperOutput(function() {
267
            $this->browser()
268
                ->post('/json', ['json' => [
269
                    'foo' => [
270
                        'bar' => ['baz' => 1],
271
                        'bam' => ['baz' => 2],
272
                        'boo' => ['baz' => 3],
273
                    ],
274
                ]])
275
                ->dump('foo.*.baz')
276
            ;
277
        });
278
279
        $this->assertSame([1, 2, 3], $output[0]);
280
    }
281
282
    /**
283
     * @test
284
     */
285
    public function can_save_formatted_json_source(): void
286
    {
287
        $contents = self::catchFileContents(__DIR__.'/../var/browser/source/source.txt', function() {
288
            $this->browser()
289
                ->visit('/http-method')
290
                ->saveSource('/source.txt')
291
            ;
292
        });
293
294
        $this->assertStringContainsString('/http-method', $contents);
295
        $this->assertStringContainsString('    "content": "",', $contents);
296
    }
297
298
    /**
299
     * @test
300
     */
301
    public function can_access_the_profiler(): void
302
    {
303
        $profile = $this->browser()
304
            ->visit('/page1')
305
            ->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

305
            ->/** @scrutinizer ignore-call */ profile()
Loading history...
306
        ;
307
308
        $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

308
        $this->/** @scrutinizer ignore-call */ 
309
               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...
309
    }
310
}
311