Passed
Push — 1.x ( 496aa5...1354ab )
by Kevin
02:18 queued 12s
created

BrowserKitBrowser::cookies()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Zenstruck\Browser;
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 Symfony\Component\BrowserKit\AbstractBrowser;
7
use Symfony\Component\BrowserKit\CookieJar;
8
use Symfony\Component\HttpKernel\Profiler\Profile;
9
use Zenstruck\Browser;
10
use Zenstruck\Browser\Mink\BrowserKitDriver;
11
use Zenstruck\Browser\Response\JsonResponse;
12
13
/**
14
 * @author Kevin Bond <[email protected]>
15
 */
16
abstract class BrowserKitBrowser extends Browser
17
{
18
    private AbstractBrowser $inner;
19
    private ?HttpOptions $defaultHttpOptions = null;
20
21
    public function __construct(AbstractBrowser $inner)
22
    {
23
        $this->inner = $inner;
24
25
        parent::__construct(new BrowserKitDriver($inner));
26
    }
27
28
    /**
29
     * @return static
30
     */
31
    final public function interceptRedirects(): self
32
    {
33
        $this->inner->followRedirects(false);
34
35
        return $this;
36
    }
37
38
    /**
39
     * @return static
40
     */
41
    final public function followRedirects(): self
42
    {
43
        $this->inner->followRedirects(true);
44
45
        if ($this->minkSession()->isStarted() && $this->response()->isRedirect()) {
46
            $this->followRedirect();
47
        }
48
49
        return $this;
50
    }
51
52
    /**
53
     * @param int $max The maximum number of redirects to follow (defaults to "infinite")
54
     *
55
     * @return static
56
     */
57
    final public function followRedirect(int $max = \PHP_INT_MAX): self
58
    {
59
        for ($i = 0; $i < $max; ++$i) {
60
            if (!$this->response()->isRedirect()) {
61
                break;
62
            }
63
64
            $this->inner->followRedirect();
65
        }
66
67
        return $this;
68
    }
69
70
    /**
71
     * @param int $max The maximum number of redirects to follow (defaults to "infinite")
72
     *
73
     * @return static
74
     */
75
    final public function assertRedirectedTo(string $expected, int $max = \PHP_INT_MAX): self
76
    {
77
        $this->assertRedirected();
78
        $this->followRedirect($max);
79
        $this->assertOn($expected);
80
81
        return $this;
82
    }
83
84
    /**
85
     * @param HttpOptions|array $options
86
     *
87
     * @return static
88
     */
89
    final public function setDefaultHttpOptions($options): self
90
    {
91
        $this->defaultHttpOptions = HttpOptions::create($options);
92
93
        return $this;
94
    }
95
96
    /**
97
     * @param HttpOptions|array $options HttpOptions::DEFAULT_OPTIONS
98
     *
99
     * @return static
100
     */
101
    final public function request(string $method, string $url, $options = []): self
102
    {
103
        if ($this->defaultHttpOptions) {
104
            $options = $this->defaultHttpOptions->merge($options);
105
        }
106
107
        $options = HttpOptions::create($options);
108
109
        $this->inner->request($method, $url, $options->query(), $options->files(), $options->server(), $options->body());
110
111
        return $this;
112
    }
113
114
    /**
115
     * @see request()
116
     *
117
     * @return static
118
     */
119
    final public function get(string $url, $options = []): self
120
    {
121
        return $this->request('GET', $url, $options);
122
    }
123
124
    /**
125
     * @see request()
126
     *
127
     * @return static
128
     */
129
    final public function post(string $url, $options = []): self
130
    {
131
        return $this->request('POST', $url, $options);
132
    }
133
134
    /**
135
     * @see request()
136
     *
137
     * @return static
138
     */
139
    final public function put(string $url, $options = []): self
140
    {
141
        return $this->request('PUT', $url, $options);
142
    }
143
144
    /**
145
     * @see request()
146
     *
147
     * @return static
148
     */
149
    final public function delete(string $url, $options = []): self
150
    {
151
        return $this->request('DELETE', $url, $options);
152
    }
153
154
    /**
155
     * @return static
156
     */
157
    final public function assertStatus(int $expected): self
158
    {
159
        return $this->wrapMinkExpectation(
160
            fn() => $this->webAssert()->statusCodeEquals($expected)
161
        );
162
    }
163
164
    /**
165
     * @return static
166
     */
167
    final public function assertSuccessful(): self
168
    {
169
        PHPUnit::assertTrue($this->response()->isSuccessful(), "Expected successful status code (2xx), [{$this->response()->statusCode()}] received.");
170
171
        return $this;
172
    }
173
174
    /**
175
     * @return static
176
     */
177
    final public function assertRedirected(): self
178
    {
179
        if ($this->inner->isFollowingRedirects()) {
180
            throw new \RuntimeException('Cannot assert redirected if not intercepting redirects. Call ->interceptRedirects() before making the request.');
181
        }
182
183
        PHPUnit::assertTrue($this->response()->isRedirect(), "Expected redirect status code (3xx), [{$this->response()->statusCode()}] received.");
184
185
        return $this;
186
    }
187
188
    /**
189
     * @return static
190
     */
191
    final public function assertHeaderEquals(string $header, string $expected): self
192
    {
193
        return $this->wrapMinkExpectation(
194
            fn() => $this->webAssert()->responseHeaderEquals($header, $expected)
195
        );
196
    }
197
198
    /**
199
     * @return static
200
     */
201
    final public function assertHeaderContains(string $header, string $expected): self
202
    {
203
        return $this->wrapMinkExpectation(
204
            fn() => $this->webAssert()->responseHeaderContains($header, $expected)
205
        );
206
    }
207
208
    /**
209
     * @return static
210
     */
211
    final public function assertJson(): self
212
    {
213
        return $this->wrapMinkExpectation(
214
            fn() => $this->webAssert()->responseHeaderContains('Content-Type', 'application/json')
215
        );
216
    }
217
218
    /**
219
     * @param string $expression JMESPath expression
220
     * @param mixed  $expected
221
     *
222
     * @return static
223
     */
224
    final public function assertJsonMatches(string $expression, $expected): self
225
    {
226
        if (!$this->response() instanceof JsonResponse) {
227
            PHPUnit::fail('Not a json response.');
228
        }
229
230
        PHPUnit::assertSame($expected, $this->response()->search($expression));
0 ignored issues
show
Bug introduced by
The method search() does not exist on Zenstruck\Browser\Response. It seems like you code against a sub-type of Zenstruck\Browser\Response such as Zenstruck\Browser\Response\JsonResponse. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

230
        PHPUnit::assertSame($expected, $this->response()->/** @scrutinizer ignore-call */ search($expression));
Loading history...
231
232
        return $this;
233
    }
234
235
    public function cookies(): CookieJar
236
    {
237
        return $this->inner->getCookieJar();
238
    }
239
240
    abstract public function profile(): Profile;
241
242
    final protected function inner(): AbstractBrowser
243
    {
244
        return $this->inner;
245
    }
246
}
247