Passed
Pull Request — 1.x (#62)
by Kevin
02:22
created

Response::assertXml()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 0
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Zenstruck\Browser;
4
5
use Behat\Mink\Session;
6
use Symfony\Component\VarDumper\VarDumper;
7
use Zenstruck\Assert;
8
use Zenstruck\Browser\Response\DomResponse;
9
use Zenstruck\Browser\Response\HtmlResponse;
10
use Zenstruck\Browser\Response\JsonResponse;
11
use Zenstruck\Browser\Response\XmlResponse;
12
13
/**
14
 * @author Kevin Bond <[email protected]>
15
 */
16
class Response
17
{
18
    private Session $session;
19
20
    /**
21
     * @internal
22
     */
23
    final public function __construct(Session $session)
24
    {
25
        $this->session = $session;
26
    }
27
28
    final public function statusCode(): int
29
    {
30
        return $this->session->getStatusCode();
31
    }
32
33
    final public function headers(): array
34
    {
35
        return $this->session->getResponseHeaders();
36
    }
37
38
    /**
39
     * @internal
40
     */
41
    final public static function createFor(Session $session): self
42
    {
43
        $contentType = (string) $session->getResponseHeader('content-type');
44
45
        if (str_contains($contentType, 'json')) {
46
            return new JsonResponse($session);
47
        }
48
49
        if (str_contains($contentType, 'html')) {
50
            return new HtmlResponse($session);
51
        }
52
53
        if (str_contains($contentType, 'xml')) {
54
            return new XmlResponse($session);
55
        }
56
57
        return new self($session);
58
    }
59
60
    final public function body(): string
61
    {
62
        return $this->session->getPage()->getContent();
63
    }
64
65
    final public function ensureJson(): JsonResponse
66
    {
67
        if (!$this instanceof JsonResponse) {
68
            Assert::fail('Not a json response.');
69
        }
70
71
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Zenstruck\Browser\Response which includes types incompatible with the type-hinted return Zenstruck\Browser\Response\JsonResponse.
Loading history...
72
    }
73
74
    final public function ensureXml(): XmlResponse
75
    {
76
        if (!$this instanceof XmlResponse) {
77
            Assert::fail('Not an xml response.');
78
        }
79
80
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Zenstruck\Browser\Response which includes types incompatible with the type-hinted return Zenstruck\Browser\Response\XmlResponse.
Loading history...
81
    }
82
83
    final public function ensureHtml(): HtmlResponse
84
    {
85
        if (!$this instanceof HtmlResponse) {
86
            Assert::fail('Not an html response.');
87
        }
88
89
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Zenstruck\Browser\Response which includes types incompatible with the type-hinted return Zenstruck\Browser\Response\HtmlResponse.
Loading history...
90
    }
91
92
    final public function ensureDom(): DomResponse
93
    {
94
        if (!$this instanceof DomResponse) {
95
            Assert::fail('Not an DOM response.');
96
        }
97
98
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Zenstruck\Browser\Response which includes types incompatible with the type-hinted return Zenstruck\Browser\Response\DomResponse.
Loading history...
99
    }
100
101
    final public function raw(): string
102
    {
103
        return "{$this->rawMetadata()}\n{$this->rawBody()}";
104
    }
105
106
    final public function isSuccessful(): bool
107
    {
108
        return $this->statusCode() >= 200 && $this->statusCode() < 300;
109
    }
110
111
    final public function isRedirect(): bool
112
    {
113
        return $this->statusCode() >= 300 && $this->statusCode() < 400;
114
    }
115
116
    public function dump(?string $selector = null): void
117
    {
118
        if (null !== $selector) {
119
            throw new \LogicException('$selector cannot be used with this response type.');
120
        }
121
122
        VarDumper::dump($this->raw());
123
    }
124
125
    /**
126
     * @internal
127
     */
128
    final protected function session(): Session
129
    {
130
        return $this->session;
131
    }
132
133
    /**
134
     * @internal
135
     */
136
    protected function rawMetadata(): string
137
    {
138
        $ret = "URL: {$this->session->getCurrentUrl()} ({$this->statusCode()})\n\n";
139
140
        foreach ($this->session->getResponseHeaders() as $header => $values) {
141
            foreach ($values as $value) {
142
                $ret .= "{$header}: {$value}\n";
143
            }
144
        }
145
146
        return $ret;
147
    }
148
149
    /**
150
     * @internal
151
     */
152
    protected function rawBody(): string
153
    {
154
        return $this->body();
155
    }
156
}
157