1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Yii\Testing; |
6
|
|
|
|
7
|
|
|
use Psr\Http\Message\ResponseInterface; |
8
|
|
|
use Psr\Http\Message\StreamInterface; |
9
|
|
|
|
10
|
|
|
final class ResponseAccessor implements ResponseInterface |
11
|
|
|
{ |
12
|
15 |
|
public function __construct(private ResponseInterface $response) |
13
|
|
|
{ |
14
|
15 |
|
} |
15
|
|
|
|
16
|
3 |
|
public function getContent(): string |
17
|
|
|
{ |
18
|
3 |
|
$stream = $this->response->getBody(); |
19
|
3 |
|
$stream->rewind(); |
20
|
3 |
|
return $stream->getContents(); |
21
|
|
|
} |
22
|
|
|
|
23
|
1 |
|
public function getContentAsJson( |
24
|
|
|
int $flags = JSON_OBJECT_AS_ARRAY | JSON_THROW_ON_ERROR | JSON_UNESCAPED_UNICODE |
25
|
|
|
): mixed { |
26
|
1 |
|
return json_decode($this->getContent(), flags: $flags); |
27
|
|
|
} |
28
|
|
|
|
29
|
2 |
|
public function getProtocolVersion(): string |
30
|
|
|
{ |
31
|
2 |
|
return $this->response->getProtocolVersion(); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param string $version |
36
|
|
|
*/ |
37
|
1 |
|
public function withProtocolVersion($version): self |
38
|
|
|
{ |
39
|
1 |
|
return $this->withResponse( |
40
|
1 |
|
$this->response->withProtocolVersion($version) |
41
|
1 |
|
); |
42
|
|
|
} |
43
|
|
|
|
44
|
1 |
|
public function getHeaders(): array |
45
|
|
|
{ |
46
|
1 |
|
return $this->response->getHeaders(); |
47
|
|
|
} |
48
|
|
|
|
49
|
1 |
|
public function hasHeader($name): bool |
50
|
|
|
{ |
51
|
1 |
|
return $this->response->hasHeader($name); |
52
|
|
|
} |
53
|
|
|
|
54
|
4 |
|
public function getHeader($name): array |
55
|
|
|
{ |
56
|
4 |
|
return $this->response->getHeader($name); |
57
|
|
|
} |
58
|
|
|
|
59
|
1 |
|
public function getHeaderLine($name): string |
60
|
|
|
{ |
61
|
1 |
|
return $this->response->getHeaderLine($name); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param string $name |
66
|
|
|
* @param string|string[] $value |
67
|
|
|
*/ |
68
|
1 |
|
public function withHeader($name, $value): self |
69
|
|
|
{ |
70
|
1 |
|
return $this->withResponse( |
71
|
1 |
|
$this->response->withHeader($name, $value) |
72
|
1 |
|
); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param string $name |
77
|
|
|
* @param string|string[] $value |
78
|
|
|
*/ |
79
|
2 |
|
public function withAddedHeader($name, $value): self |
80
|
|
|
{ |
81
|
2 |
|
return $this->withResponse( |
82
|
2 |
|
$this->response->withAddedHeader($name, $value) |
83
|
2 |
|
); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param string $name |
88
|
|
|
*/ |
89
|
1 |
|
public function withoutHeader($name): self |
90
|
|
|
{ |
91
|
1 |
|
return $this->withResponse( |
92
|
1 |
|
$this->response->withoutHeader($name) |
93
|
1 |
|
); |
94
|
|
|
} |
95
|
|
|
|
96
|
1 |
|
public function getBody(): StreamInterface |
97
|
|
|
{ |
98
|
1 |
|
return $this->response->getBody(); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @psalm-suppress MoreSpecificReturnType,LessSpecificReturnStatement It's false-positive errors in Psalm <= 4.30 |
103
|
|
|
*/ |
104
|
1 |
|
public function withBody(StreamInterface $body): self |
105
|
|
|
{ |
106
|
1 |
|
return $this->withResponse( |
107
|
1 |
|
$this->response->withBody($body) |
108
|
1 |
|
); |
109
|
|
|
} |
110
|
|
|
|
111
|
2 |
|
public function getStatusCode(): int |
112
|
|
|
{ |
113
|
2 |
|
return $this->response->getStatusCode(); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @param int $code |
118
|
|
|
* @param string $reasonPhrase |
119
|
|
|
*/ |
120
|
1 |
|
public function withStatus($code, $reasonPhrase = ''): self |
121
|
|
|
{ |
122
|
1 |
|
return $this->withResponse( |
123
|
1 |
|
$this->response->withStatus($code, $reasonPhrase) |
124
|
1 |
|
); |
125
|
|
|
} |
126
|
|
|
|
127
|
1 |
|
public function getReasonPhrase(): string |
128
|
|
|
{ |
129
|
1 |
|
return $this->response->getReasonPhrase(); |
130
|
|
|
} |
131
|
|
|
|
132
|
6 |
|
public function getResponse(): ResponseInterface |
133
|
|
|
{ |
134
|
6 |
|
return $this->response; |
135
|
|
|
} |
136
|
|
|
|
137
|
6 |
|
private function withResponse(ResponseInterface $response): self |
138
|
|
|
{ |
139
|
6 |
|
$new = clone $this; |
140
|
6 |
|
$new->response = $response; |
141
|
6 |
|
return $new; |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|