Passed
Push — 1.x ( e3781f...255b78 )
by Kevin
02:10
created

Http   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 17
c 1
b 0
f 0
dl 0
loc 100
rs 10
wmc 11

9 Methods

Rating   Name   Duplication   Size   Complexity  
A assertHeaderContains() 0 4 1
A assertStatus() 0 4 1
A assertRedirected() 0 7 2
A get() 0 3 1
A assertSuccessful() 0 7 2
A post() 0 3 1
A put() 0 3 1
A delete() 0 3 1
A assertHeaderEquals() 0 4 1
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
    /**
14
     * @param HttpOptions|array $options HttpOptions::DEFAULT_OPTIONS
15
     *
16
     * @return static
17
     */
18
    abstract public function request(string $method, string $url, $options = []): self;
19
20
    /**
21
     * @see request()
22
     *
23
     * @return static
24
     */
25
    final public function get(string $url, $options = []): self
26
    {
27
        return $this->request('GET', $url, $options);
28
    }
29
30
    /**
31
     * @see request()
32
     *
33
     * @return static
34
     */
35
    final public function post(string $url, $options = []): self
36
    {
37
        return $this->request('POST', $url, $options);
38
    }
39
40
    /**
41
     * @see request()
42
     *
43
     * @return static
44
     */
45
    final public function put(string $url, $options = []): self
46
    {
47
        return $this->request('PUT', $url, $options);
48
    }
49
50
    /**
51
     * @see request()
52
     *
53
     * @return static
54
     */
55
    final public function delete(string $url, $options = []): self
56
    {
57
        return $this->request('DELETE', $url, $options);
58
    }
59
60
    /**
61
     * @return static
62
     */
63
    final public function assertStatus(int $expected): self
64
    {
65
        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

65
        return $this->/** @scrutinizer ignore-call */ wrapMinkExpectation(
Loading history...
66
            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

66
            fn() => $this->/** @scrutinizer ignore-call */ webAssert()->statusCodeEquals($expected)
Loading history...
67
        );
68
    }
69
70
    /**
71
     * @return static
72
     */
73
    final public function assertSuccessful(): self
74
    {
75
        $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

75
        $status = $this->/** @scrutinizer ignore-call */ minkSession()->getStatusCode();
Loading history...
76
77
        PHPUnit::assertTrue($status >= 200 && $status < 300, "Expected successful status code (2xx), [{$status}] received.");
78
79
        return $this;
80
    }
81
82
    /**
83
     * @return static
84
     */
85
    final public function assertRedirected(): self
86
    {
87
        $status = $this->minkSession()->getStatusCode();
88
89
        PHPUnit::assertTrue($status >= 300 && $status < 400, "Expected redirect status code (3xx), [{$status}] received.");
90
91
        return $this;
92
    }
93
94
    /**
95
     * @return static
96
     */
97
    final public function assertHeaderEquals(string $header, string $expected): self
98
    {
99
        return $this->wrapMinkExpectation(
100
            fn() => $this->webAssert()->responseHeaderEquals($header, $expected)
101
        );
102
    }
103
104
    /**
105
     * @return static
106
     */
107
    final public function assertHeaderContains(string $header, string $expected): self
108
    {
109
        return $this->wrapMinkExpectation(
110
            fn() => $this->webAssert()->responseHeaderContains($header, $expected)
111
        );
112
    }
113
}
114