| 1 | <?php |
||
| 2 | |||
| 3 | namespace Faxity\Test; |
||
| 4 | |||
| 5 | use Faxity\Fetch\Fetch; |
||
| 6 | |||
| 7 | /** |
||
| 8 | * Send HTTP requests via cURL |
||
| 9 | */ |
||
| 10 | class MockFetch extends Fetch |
||
| 11 | { |
||
| 12 | private $queue = []; |
||
| 13 | |||
| 14 | |||
| 15 | /** |
||
| 16 | * Adds a response to the queue, to add multiple set an array instead |
||
| 17 | * @param object|array $response |
||
| 18 | */ |
||
| 19 | 8 | public function addResponse($response) |
|
| 20 | { |
||
| 21 | 8 | if (is_object($response)) { |
|
| 22 | 6 | $this->queue[] = $response; |
|
| 23 | 2 | } else if (is_array($response)) { |
|
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 24 | 2 | $this->queue = array_merge($this->queue, $response); |
|
| 25 | } |
||
| 26 | 8 | } |
|
| 27 | |||
| 28 | |||
| 29 | /** |
||
| 30 | * Gets teh first response in the queue. |
||
| 31 | * @param string $url URL to send request to |
||
| 32 | * @param mixed $params Optional query parameters as an array or object |
||
| 33 | * |
||
| 34 | * @return object |
||
| 35 | */ |
||
| 36 | 5 | public function get(string $url, $params = null) : ?object |
|
| 37 | { |
||
| 38 | 5 | if (empty($this->queue)) { |
|
| 39 | 1 | throw new \Exception("No responses in the queue."); |
|
| 40 | } |
||
| 41 | |||
| 42 | 4 | return array_shift($this->queue); |
|
| 43 | } |
||
| 44 | |||
| 45 | |||
| 46 | /** |
||
| 47 | * Gets multiple requests from start of the queue. |
||
| 48 | * @param array $requests An array of assoc-arrays with url and params as keys, like: |
||
| 49 | * [ |
||
| 50 | * "url" => "", |
||
| 51 | * "params" => [ "param" => "" ] |
||
| 52 | * ] |
||
| 53 | */ |
||
| 54 | 5 | public function getMulti(array $requests) |
|
| 55 | { |
||
| 56 | 5 | $len = count($requests); |
|
| 57 | |||
| 58 | 5 | if (count($this->queue) < $len) { |
|
| 59 | 1 | throw new \Exception("Not enough responses in the queue."); |
|
| 60 | } |
||
| 61 | |||
| 62 | 4 | return array_splice($this->queue, 0, $len); |
|
| 63 | } |
||
| 64 | } |
||
| 65 |