Passed
Push — 1.x ( 28bc3b...5a4286 )
by Kevin
08:02
created

HttpTests   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
eloc 45
dl 0
loc 76
rs 10
c 2
b 0
f 1
wmc 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A response_header_assertions() 0 6 1
A http_method_actions() 0 33 1
A exceptions_are_caught_by_default() 0 5 1
A can_set_default_http_options() 0 9 1
1
<?php
2
3
namespace Zenstruck\Browser\Tests\Extension;
4
5
use Zenstruck\Browser\Extension\Http\HttpOptions;
6
use Zenstruck\Browser\Tests\Fixture\CustomHttpOptions;
7
8
/**
9
 * @author Kevin Bond <[email protected]>
10
 */
11
trait HttpTests
12
{
13
    /**
14
     * @test
15
     */
16
    public function exceptions_are_caught_by_default(): void
17
    {
18
        $this->browser()
0 ignored issues
show
Bug introduced by
It seems like browser() 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

18
        $this->/** @scrutinizer ignore-call */ 
19
               browser()
Loading history...
19
            ->visit('/exception')
20
            ->assertStatus(500)
21
        ;
22
    }
23
24
    /**
25
     * @test
26
     */
27
    public function response_header_assertions(): void
28
    {
29
        $this->browser()
30
            ->visit('/page1')
31
            ->assertHeaderEquals('Content-Type', 'text/html; charset=UTF-8')
32
            ->assertHeaderContains('Content-Type', 'text/html')
33
        ;
34
    }
35
36
    /**
37
     * @test
38
     */
39
    public function http_method_actions(): void
40
    {
41
        $this->browser()
42
            ->get('/http-method')
43
            ->assertSuccessful()
44
            ->assertContains('"method":"GET"')
45
            ->post('/http-method')
46
            ->assertSuccessful()
47
            ->assertContains('"method":"POST"')
48
            ->delete('/http-method')
49
            ->assertSuccessful()
50
            ->assertContains('"method":"DELETE"')
51
            ->put('/http-method')
52
            ->assertSuccessful()
53
            ->assertContains('"method":"PUT"')
54
            ->assertContains('"ajax":false')
55
            ->post('/http-method', [
56
                'json' => ['foo' => 'bar'],
57
                'headers' => ['X-Foo' => 'Bar'],
58
                'ajax' => true,
59
            ])
60
            ->assertContains('"content-type":["application\/json"]')
61
            ->assertContains('"x-foo":["Bar"]')
62
            ->assertContains('"content":"{\u0022foo\u0022:\u0022bar\u0022}"')
63
            ->assertContains('"ajax":true')
64
            ->post('/http-method', HttpOptions::jsonAjax(['foo' => 'bar'])->withHeader('X-Foo', 'Bar'))
65
            ->assertContains('"content-type":["application\/json"]')
66
            ->assertContains('"x-foo":["Bar"]')
67
            ->assertContains('"content":"{\u0022foo\u0022:\u0022bar\u0022}"')
68
            ->assertContains('"ajax":true')
69
            ->post('/http-method', CustomHttpOptions::api('my-token'))
70
            ->assertContains('"content-type":["application\/json"]')
71
            ->assertContains('"x-token":["my-token"]')
72
        ;
73
    }
74
75
    /**
76
     * @test
77
     */
78
    public function can_set_default_http_options(): void
79
    {
80
        $this->browser()
81
            ->setDefaultHttpOptions(['headers' => ['x-foo' => 'bar']])
82
            ->post('/http-method')
83
            ->assertContains('"x-foo":["Bar"]')
84
            ->post('/http-method', ['headers' => ['x-bar' => 'foo']])
85
            ->assertContains('"x-bar":["Foo"]')
86
            ->assertContains('"x-foo":["Bar"]')
87
        ;
88
    }
89
}
90