Completed
Push — 1.x ( b6efad...f02a91 )
by Kevin
16s queued 12s
created

Response   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 137
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 2
Metric Value
eloc 35
dl 0
loc 137
rs 10
c 2
b 0
f 2
wmc 25

14 Methods

Rating   Name   Duplication   Size   Complexity  
A statusCode() 0 3 1
A isRedirect() 0 3 2
A __construct() 0 3 1
A assertJson() 0 7 2
A assertHtml() 0 7 2
A session() 0 3 1
A rawBody() 0 3 1
A assertXml() 0 7 2
A dump() 0 7 2
A raw() 0 3 1
A rawMetadata() 0 11 3
A isSuccessful() 0 3 2
A body() 0 3 1
A createFor() 0 17 4
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\HtmlResponse;
9
use Zenstruck\Browser\Response\JsonResponse;
10
use Zenstruck\Browser\Response\XmlResponse;
11
12
/**
13
 * @author Kevin Bond <[email protected]>
14
 */
15
class Response
16
{
17
    private Session $session;
18
19
    /**
20
     * @internal
21
     */
22
    final public function __construct(Session $session)
23
    {
24
        $this->session = $session;
25
    }
26
27
    final public function statusCode(): int
28
    {
29
        return $this->session->getStatusCode();
30
    }
31
32
    /**
33
     * @internal
34
     */
35
    final public static function createFor(Session $session): self
36
    {
37
        $contentType = (string) $session->getResponseHeader('content-type');
38
39
        if (str_contains($contentType, 'json')) {
40
            return new JsonResponse($session);
41
        }
42
43
        if (str_contains($contentType, 'html')) {
44
            return new HtmlResponse($session);
45
        }
46
47
        if (str_contains($contentType, 'xml')) {
48
            return new XmlResponse($session);
49
        }
50
51
        return new self($session);
52
    }
53
54
    final public function body(): string
55
    {
56
        return $this->session->getPage()->getContent();
57
    }
58
59
    final public function assertJson(): JsonResponse
60
    {
61
        if (!$this instanceof JsonResponse) {
62
            PHPUnit::fail('Not a json response.');
63
        }
64
65
        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...
66
    }
67
68
    final public function assertXml(): XmlResponse
69
    {
70
        if (!$this instanceof XmlResponse) {
71
            PHPUnit::fail('Not an xml response.');
72
        }
73
74
        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...
75
    }
76
77
    final public function assertHtml(): HtmlResponse
78
    {
79
        if (!$this instanceof HtmlResponse) {
80
            PHPUnit::fail('Not an html response.');
81
        }
82
83
        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...
84
    }
85
86
    /**
87
     * @internal
88
     */
89
    final public function raw(): string
90
    {
91
        return "{$this->rawMetadata()}\n{$this->rawBody()}";
92
    }
93
94
    /**
95
     * @internal
96
     */
97
    final public function isSuccessful(): bool
98
    {
99
        return $this->statusCode() >= 200 && $this->statusCode() < 300;
100
    }
101
102
    /**
103
     * @internal
104
     */
105
    final public function isRedirect(): bool
106
    {
107
        return $this->statusCode() >= 300 && $this->statusCode() < 400;
108
    }
109
110
    /**
111
     * @internal
112
     */
113
    public function dump(?string $selector = null): void
114
    {
115
        if (null !== $selector) {
116
            throw new \LogicException('$selector cannot be used with this response type.');
117
        }
118
119
        VarDumper::dump($this->raw());
120
    }
121
122
    /**
123
     * @internal
124
     */
125
    final protected function session(): Session
126
    {
127
        return $this->session;
128
    }
129
130
    /**
131
     * @internal
132
     */
133
    protected function rawMetadata(): string
134
    {
135
        $ret = "URL: {$this->session->getCurrentUrl()} ({$this->statusCode()})\n\n";
136
137
        foreach ($this->session->getResponseHeaders() as $header => $values) {
138
            foreach ($values as $value) {
139
                $ret .= "{$header}: {$value}\n";
140
            }
141
        }
142
143
        return $ret;
144
    }
145
146
    /**
147
     * @internal
148
     */
149
    protected function rawBody(): string
150
    {
151
        return $this->body();
152
    }
153
}
154