-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathMockHttpClient.php
More file actions
57 lines (43 loc) · 1.35 KB
/
MockHttpClient.php
File metadata and controls
57 lines (43 loc) · 1.35 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
<?php
declare(strict_types=1);
namespace Otherguy\Currency\Tests\Support;
use Otherguy\Currency\Exceptions\MockHttpClientException;
use Psr\Http\Client\ClientExceptionInterface;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
class MockHttpClient implements ClientInterface
{
/** @var list<ResponseInterface|ClientExceptionInterface> */
private array $queue = [];
/** @var list<RequestInterface> */
private array $sentRequests = [];
public function enqueue(ResponseInterface|ClientExceptionInterface $item): self
{
$this->queue[] = $item;
return $this;
}
public function sendRequest(RequestInterface $request): ResponseInterface
{
$this->sentRequests[] = $request;
if ($this->queue === []) {
throw new MockHttpClientException('MockHttpClient queue is empty.');
}
$next = array_shift($this->queue);
if ($next instanceof ClientExceptionInterface) {
throw $next;
}
return $next;
}
public function lastRequest(): ?RequestInterface
{
return $this->sentRequests[count($this->sentRequests) - 1] ?? null;
}
/**
* @return list<RequestInterface>
*/
public function sentRequests(): array
{
return $this->sentRequests;
}
}