Passed
Push — 1.x ( 50798f...12c6d5 )
by Kevin
02:43
created

Response::assertDom()   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
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 7
rs 10
1
<?php
2
3
namespace Zenstruck\Browser;
4
5
use Behat\Mink\Session;
6
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...
7
use Symfony\Component\VarDumper\VarDumper;
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
    /**
34
     * @internal
35
     */
36
    final public static function createFor(Session $session): self
37
    {
38
        $contentType = (string) $session->getResponseHeader('content-type');
39
40
        if (str_contains($contentType, 'json')) {
41
            return new JsonResponse($session);
42
        }
43
44
        if (str_contains($contentType, 'html')) {
45
            return new HtmlResponse($session);
46
        }
47
48
        if (str_contains($contentType, 'xml')) {
49
            return new XmlResponse($session);
50
        }
51
52
        return new self($session);
53
    }
54
55
    final public function body(): string
56
    {
57
        return $this->session->getPage()->getContent();
58
    }
59
60
    final public function assertJson(): JsonResponse
61
    {
62
        if (!$this instanceof JsonResponse) {
63
            PHPUnit::fail('Not a json response.');
64
        }
65
66
        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...
67
    }
68
69
    final public function assertXml(): XmlResponse
70
    {
71
        if (!$this instanceof XmlResponse) {
72
            PHPUnit::fail('Not an xml response.');
73
        }
74
75
        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...
76
    }
77
78
    final public function assertHtml(): HtmlResponse
79
    {
80
        if (!$this instanceof HtmlResponse) {
81
            PHPUnit::fail('Not an html response.');
82
        }
83
84
        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...
85
    }
86
87
    final public function assertDom(): DomResponse
88
    {
89
        if (!$this instanceof DomResponse) {
90
            PHPUnit::fail('Not an DOM response.');
91
        }
92
93
        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...
94
    }
95
96
    final public function raw(): string
97
    {
98
        return "{$this->rawMetadata()}\n{$this->rawBody()}";
99
    }
100
101
    final public function isSuccessful(): bool
102
    {
103
        return $this->statusCode() >= 200 && $this->statusCode() < 300;
104
    }
105
106
    final public function isRedirect(): bool
107
    {
108
        return $this->statusCode() >= 300 && $this->statusCode() < 400;
109
    }
110
111
    public function dump(?string $selector = null): void
112
    {
113
        if (null !== $selector) {
114
            throw new \LogicException('$selector cannot be used with this response type.');
115
        }
116
117
        VarDumper::dump($this->raw());
118
    }
119
120
    /**
121
     * @internal
122
     */
123
    final protected function session(): Session
124
    {
125
        return $this->session;
126
    }
127
128
    /**
129
     * @internal
130
     */
131
    protected function rawMetadata(): string
132
    {
133
        $ret = "URL: {$this->session->getCurrentUrl()} ({$this->statusCode()})\n\n";
134
135
        foreach ($this->session->getResponseHeaders() as $header => $values) {
136
            foreach ($values as $value) {
137
                $ret .= "{$header}: {$value}\n";
138
            }
139
        }
140
141
        return $ret;
142
    }
143
144
    /**
145
     * @internal
146
     */
147
    protected function rawBody(): string
148
    {
149
        return $this->body();
150
    }
151
}
152