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

Http::setDefaultHttpOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Zenstruck\Browser\Extension;
4
5
use PHPUnit\Framework\Assert as PHPUnit;
0 ignored issues
show
Bug introduced by
The type PHPUnit\Framework\Assert was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use Zenstruck\Browser\Extension\Http\HttpOptions;
7
8
/**
9
 * @author Kevin Bond <[email protected]>
10
 */
11
trait Http
12
{
13
    private ?HttpOptions $defaultHttpOptions = null;
14
15
    /**
16
     * @param HttpOptions|array $options
17
     *
18
     * @return static
19
     */
20
    final public function setDefaultHttpOptions($options): self
21
    {
22
        $this->defaultHttpOptions = HttpOptions::create($options);
23
24
        return $this;
25
    }
26
27
    /**
28
     * @param HttpOptions|array $options HttpOptions::DEFAULT_OPTIONS
29
     *
30
     * @return static
31
     */
32
    final public function request(string $method, string $url, $options = []): self
33
    {
34
        if ($this->defaultHttpOptions) {
35
            $options = $this->defaultHttpOptions->merge($options);
36
        }
37
38
        $this->makeRequest($method, $url, HttpOptions::create($options));
39
40
        return $this;
41
    }
42
43
    /**
44
     * @see request()
45
     *
46
     * @return static
47
     */
48
    final public function get(string $url, $options = []): self
49
    {
50
        return $this->request('GET', $url, $options);
51
    }
52
53
    /**
54
     * @see request()
55
     *
56
     * @return static
57
     */
58
    final public function post(string $url, $options = []): self
59
    {
60
        return $this->request('POST', $url, $options);
61
    }
62
63
    /**
64
     * @see request()
65
     *
66
     * @return static
67
     */
68
    final public function put(string $url, $options = []): self
69
    {
70
        return $this->request('PUT', $url, $options);
71
    }
72
73
    /**
74
     * @see request()
75
     *
76
     * @return static
77
     */
78
    final public function delete(string $url, $options = []): self
79
    {
80
        return $this->request('DELETE', $url, $options);
81
    }
82
83
    /**
84
     * @return static
85
     */
86
    final public function assertStatus(int $expected): self
87
    {
88
        return $this->wrapMinkExpectation(
0 ignored issues
show
Bug introduced by
It seems like wrapMinkExpectation() 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

88
        return $this->/** @scrutinizer ignore-call */ wrapMinkExpectation(
Loading history...
89
            fn() => $this->webAssert()->statusCodeEquals($expected)
0 ignored issues
show
Bug introduced by
It seems like webAssert() 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

89
            fn() => $this->/** @scrutinizer ignore-call */ webAssert()->statusCodeEquals($expected)
Loading history...
90
        );
91
    }
92
93
    /**
94
     * @return static
95
     */
96
    final public function assertSuccessful(): self
97
    {
98
        $status = $this->minkSession()->getStatusCode();
0 ignored issues
show
Bug introduced by
It seems like minkSession() 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

98
        $status = $this->/** @scrutinizer ignore-call */ minkSession()->getStatusCode();
Loading history...
99
100
        PHPUnit::assertTrue($status >= 200 && $status < 300, "Expected successful status code (2xx), [{$status}] received.");
101
102
        return $this;
103
    }
104
105
    /**
106
     * @return static
107
     */
108
    final public function assertRedirected(): self
109
    {
110
        $status = $this->minkSession()->getStatusCode();
111
112
        PHPUnit::assertTrue($status >= 300 && $status < 400, "Expected redirect status code (3xx), [{$status}] received.");
113
114
        return $this;
115
    }
116
117
    /**
118
     * @return static
119
     */
120
    final public function assertHeaderEquals(string $header, string $expected): self
121
    {
122
        return $this->wrapMinkExpectation(
123
            fn() => $this->webAssert()->responseHeaderEquals($header, $expected)
124
        );
125
    }
126
127
    /**
128
     * @return static
129
     */
130
    final public function assertHeaderContains(string $header, string $expected): self
131
    {
132
        return $this->wrapMinkExpectation(
133
            fn() => $this->webAssert()->responseHeaderContains($header, $expected)
134
        );
135
    }
136
137
    abstract protected function makeRequest(string $method, string $url, HttpOptions $options): void;
138
}
139