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

BrowserKitBrowser::request()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 7
rs 10
1
<?php
2
3
namespace Zenstruck\Browser;
4
5
use Symfony\Component\BrowserKit\AbstractBrowser;
6
use Zenstruck\Browser;
7
use Zenstruck\Browser\Extension\Html;
8
use Zenstruck\Browser\Extension\Http;
9
use Zenstruck\Browser\Extension\Http\HttpOptions;
10
use Zenstruck\Browser\Extension\Json;
11
use Zenstruck\Browser\Mink\BrowserKitDriver;
12
13
/**
14
 * @author Kevin Bond <[email protected]>
15
 */
16
abstract class BrowserKitBrowser extends Browser
17
{
18
    use Html, Http, Json;
19
20
    private AbstractBrowser $inner;
21
22
    public function __construct(AbstractBrowser $inner)
23
    {
24
        $this->inner = $inner;
25
26
        parent::__construct(new BrowserKitDriver($inner));
27
    }
28
29
    final public function inner(): AbstractBrowser
30
    {
31
        return $this->inner;
32
    }
33
34
    /**
35
     * @return static
36
     */
37
    final public function interceptRedirects(): self
38
    {
39
        $this->inner->followRedirects(false);
40
41
        return $this;
42
    }
43
44
    /**
45
     * @param int $max The maximum number of redirects to follow (defaults to "infinite")
46
     *
47
     * @return static
48
     */
49
    final public function followRedirect(int $max = PHP_INT_MAX): self
50
    {
51
        for ($i = 0; $i < $max; ++$i) {
52
            $status = $this->minkSession()->getStatusCode();
53
54
            if ($status < 300 || $status > 400) {
55
                break;
56
            }
57
58
            $this->inner->followRedirect();
59
        }
60
61
        return $this;
62
    }
63
64
    /**
65
     * @param int $max The maximum number of redirects to follow (defaults to "infinite")
66
     *
67
     * @return static
68
     */
69
    final public function assertRedirectedTo(string $expected, int $max = PHP_INT_MAX): self
70
    {
71
        $this->assertRedirected();
72
        $this->followRedirect($max);
73
        $this->assertOn($expected);
74
75
        return $this;
76
    }
77
78
    final protected function makeRequest(string $method, string $url, HttpOptions $options): void
79
    {
80
        $this->inner->request($method, $url, $options->parameters(), $options->files(), $options->server(), $options->body());
81
    }
82
}
83