-
-
Notifications
You must be signed in to change notification settings - Fork 123
Expand file tree
/
Copy pathFakeResponse.php
More file actions
132 lines (111 loc) · 3.17 KB
/
Copy pathFakeResponse.php
File metadata and controls
132 lines (111 loc) · 3.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<?php
declare(strict_types=1);
namespace Saloon\Http\Faking;
use Closure;
use Throwable;
use Saloon\Traits\Makeable;
use Saloon\Http\PendingRequest;
use Saloon\Repositories\ArrayStore;
use Psr\Http\Message\ResponseInterface;
use Saloon\Contracts\Body\BodyRepository;
use Psr\Http\Message\StreamFactoryInterface;
use Psr\Http\Message\ResponseFactoryInterface;
use Saloon\Repositories\Body\JsonBodyRepository;
use Saloon\Repositories\Body\StringBodyRepository;
use Saloon\Contracts\ArrayStore as ArrayStoreContract;
use Saloon\Contracts\FakeResponse as FakeResponseContract;
/**
* @method static static make(mixed $body = [], int $status = 200, array<string, string|int|float|bool> $headers = [])
*/
class FakeResponse implements FakeResponseContract
{
use Makeable;
/**
* HTTP Status Code
*/
protected int $status;
/**
* Headers
*/
protected ArrayStoreContract $headers;
/**
* Request Body
*/
protected BodyRepository $body;
/**
* Exception Closure
*/
protected ?Closure $responseException = null;
/**
* Create a new mock response
*
* @param array<string, mixed>|string $body
* @param array<string, mixed> $headers
*/
public function __construct(array|string $body = [], int $status = 200, array $headers = [])
{
$this->body = is_array($body) ? new JsonBodyRepository($body) : new StringBodyRepository($body);
$this->status = $status;
$this->headers = new ArrayStore($headers);
}
/**
* Get the response body
*/
public function body(): BodyRepository
{
return $this->body;
}
/**
* Get the status from the responses
*/
public function status(): int
{
return $this->status;
}
/**
* Get the headers
*/
public function headers(): ArrayStoreContract
{
return $this->headers;
}
/**
* Throw an exception on the request.
*
* @return $this
*/
public function throw(Closure|Throwable $value): static
{
$closure = $value instanceof Throwable ? static fn () => $value : $value;
$this->responseException = $closure;
return $this;
}
/**
* Invoke the exception.
*/
public function getException(PendingRequest $pendingRequest): ?Throwable
{
if (! $this->responseException instanceof Closure) {
return null;
}
return call_user_func($this->responseException, $pendingRequest);
}
/**
* Create a new mock response from a fixture
*/
public static function fixture(string $name): Fixture
{
return new Fixture($name);
}
/**
* Get the response as a ResponseInterface
*/
public function createPsrResponse(ResponseFactoryInterface $responseFactory, StreamFactoryInterface $streamFactory): ResponseInterface
{
$response = $responseFactory->createResponse($this->status());
foreach ($this->headers()->all() as $headerName => $headerValue) {
$response = $response->withHeader($headerName, $headerValue);
}
return $response->withBody($this->body()->toStream($streamFactory));
}
}