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
|
|
|
|
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
|
|
|
* @return ResponseAccessor |
37
|
|
|
* @psalm-suppress LessSpecificReturnStatement |
38
|
|
|
*/ |
39
|
1 |
|
public function withProtocolVersion($version): ResponseAccessor |
40
|
|
|
{ |
41
|
1 |
|
$response = $this->response->withProtocolVersion($version); |
42
|
1 |
|
return new self($response); |
43
|
|
|
} |
44
|
|
|
|
45
|
1 |
|
public function getHeaders(): array |
46
|
|
|
{ |
47
|
1 |
|
return $this->response->getHeaders(); |
48
|
|
|
} |
49
|
|
|
|
50
|
1 |
|
public function hasHeader($name): bool |
51
|
|
|
{ |
52
|
1 |
|
return $this->response->hasHeader($name); |
53
|
|
|
} |
54
|
|
|
|
55
|
4 |
|
public function getHeader($name): array |
56
|
|
|
{ |
57
|
4 |
|
return $this->response->getHeader($name); |
58
|
|
|
} |
59
|
|
|
|
60
|
1 |
|
public function getHeaderLine($name): string |
61
|
|
|
{ |
62
|
1 |
|
return $this->response->getHeaderLine($name); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param string $name |
67
|
|
|
* @param string|string[] $value |
68
|
|
|
* @return ResponseAccessor |
69
|
|
|
* @psalm-suppress LessSpecificReturnStatement |
70
|
|
|
*/ |
71
|
1 |
|
public function withHeader($name, $value): ResponseAccessor |
72
|
|
|
{ |
73
|
1 |
|
$response = $this->response->withHeader($name, $value); |
74
|
1 |
|
return new self($response); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param string $name |
79
|
|
|
* @param string|string[] $value |
80
|
|
|
* @return ResponseAccessor |
81
|
|
|
* @psalm-suppress LessSpecificReturnStatement |
82
|
|
|
*/ |
83
|
2 |
|
public function withAddedHeader($name, $value): ResponseAccessor |
84
|
|
|
{ |
85
|
2 |
|
$response = $this->response->withAddedHeader($name, $value); |
86
|
2 |
|
return new self($response); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param string $name |
91
|
|
|
* @return ResponseAccessor |
92
|
|
|
* @psalm-suppress LessSpecificReturnStatement |
93
|
|
|
*/ |
94
|
1 |
|
public function withoutHeader($name): ResponseAccessor |
95
|
|
|
{ |
96
|
1 |
|
$response = $this->response->withoutHeader($name); |
97
|
1 |
|
return new self($response); |
98
|
|
|
} |
99
|
|
|
|
100
|
1 |
|
public function getBody(): StreamInterface |
101
|
|
|
{ |
102
|
1 |
|
return $this->response->getBody(); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param StreamInterface $body |
107
|
|
|
* @return ResponseAccessor |
108
|
|
|
* @psalm-suppress LessSpecificReturnStatement |
109
|
|
|
*/ |
110
|
1 |
|
public function withBody(StreamInterface $body): ResponseAccessor |
111
|
|
|
{ |
112
|
1 |
|
$response = $this->response->withBody($body); |
113
|
1 |
|
return new self($response); |
114
|
|
|
} |
115
|
|
|
|
116
|
2 |
|
public function getStatusCode(): int |
117
|
|
|
{ |
118
|
2 |
|
return $this->response->getStatusCode(); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @param int $code |
123
|
|
|
* @param string $reasonPhrase |
124
|
|
|
* @return ResponseAccessor |
125
|
|
|
* @psalm-suppress LessSpecificReturnStatement |
126
|
|
|
*/ |
127
|
1 |
|
public function withStatus($code, $reasonPhrase = ''): ResponseAccessor |
128
|
|
|
{ |
129
|
1 |
|
$response = $this->response->withStatus($code, $reasonPhrase); |
130
|
|
|
|
131
|
1 |
|
return new self($response); |
132
|
|
|
} |
133
|
|
|
|
134
|
1 |
|
public function getReasonPhrase(): string |
135
|
|
|
{ |
136
|
1 |
|
return $this->response->getReasonPhrase(); |
137
|
|
|
} |
138
|
|
|
|
139
|
6 |
|
public function getResponse(): ResponseInterface |
140
|
|
|
{ |
141
|
6 |
|
return $this->response; |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|