Passed
Push — 1.x ( 5a4286...a604cc )
by Kevin
01:41
created

BrowserKitBrowser::assertRedirectedTo()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 2
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
     * @return static
46
     */
47
    final public function followRedirects(): self
48
    {
49
        $this->inner->followRedirects(true);
50
51
        return $this;
52
    }
53
54
    /**
55
     * @param int $max The maximum number of redirects to follow (defaults to "infinite")
56
     *
57
     * @return static
58
     */
59
    final public function followRedirect(int $max = PHP_INT_MAX): self
60
    {
61
        for ($i = 0; $i < $max; ++$i) {
62
            $status = $this->minkSession()->getStatusCode();
63
64
            if ($status < 300 || $status > 400) {
65
                break;
66
            }
67
68
            $this->inner->followRedirect();
69
        }
70
71
        return $this;
72
    }
73
74
    /**
75
     * @param int $max The maximum number of redirects to follow (defaults to "infinite")
76
     *
77
     * @return static
78
     */
79
    final public function assertRedirectedTo(string $expected, int $max = PHP_INT_MAX): self
80
    {
81
        $this->assertRedirected();
82
        $this->followRedirect($max);
83
        $this->assertOn($expected);
84
85
        return $this;
86
    }
87
88
    final protected function makeRequest(string $method, string $url, HttpOptions $options): void
89
    {
90
        $this->inner->request($method, $url, $options->parameters(), $options->files(), $options->server(), $options->body());
91
    }
92
}
93