Open Journal Systems  3.3.0
MockHandler.php
1 <?php
2 namespace GuzzleHttp\Handler;
3 
11 
15 class MockHandler implements \Countable
16 {
17  private $queue = [];
18  private $lastRequest;
19  private $lastOptions;
20  private $onFulfilled;
21  private $onRejected;
22 
33  public static function createWithMiddleware(
34  array $queue = null,
35  callable $onFulfilled = null,
36  callable $onRejected = null
37  ) {
38  return HandlerStack::create(new self($queue, $onFulfilled, $onRejected));
39  }
40 
50  public function __construct(
51  array $queue = null,
52  callable $onFulfilled = null,
53  callable $onRejected = null
54  ) {
55  $this->onFulfilled = $onFulfilled;
56  $this->onRejected = $onRejected;
57 
58  if ($queue) {
59  call_user_func_array([$this, 'append'], $queue);
60  }
61  }
62 
63  public function __invoke(RequestInterface $request, array $options)
64  {
65  if (!$this->queue) {
66  throw new \OutOfBoundsException('Mock queue is empty');
67  }
68 
69  if (isset($options['delay']) && is_numeric($options['delay'])) {
70  usleep($options['delay'] * 1000);
71  }
72 
73  $this->lastRequest = $request;
74  $this->lastOptions = $options;
75  $response = array_shift($this->queue);
76 
77  if (isset($options['on_headers'])) {
78  if (!is_callable($options['on_headers'])) {
79  throw new \InvalidArgumentException('on_headers must be callable');
80  }
81  try {
82  $options['on_headers']($response);
83  } catch (\Exception $e) {
84  $msg = 'An error was encountered during the on_headers event';
85  $response = new RequestException($msg, $request, $response, $e);
86  }
87  }
88 
89  if (is_callable($response)) {
90  $response = call_user_func($response, $request, $options);
91  }
92 
93  $response = $response instanceof \Exception
94  ? \GuzzleHttp\Promise\rejection_for($response)
95  : \GuzzleHttp\Promise\promise_for($response);
96 
97  return $response->then(
98  function ($value) use ($request, $options) {
99  $this->invokeStats($request, $options, $value);
100  if ($this->onFulfilled) {
101  call_user_func($this->onFulfilled, $value);
102  }
103  if (isset($options['sink'])) {
104  $contents = (string) $value->getBody();
105  $sink = $options['sink'];
106 
107  if (is_resource($sink)) {
108  fwrite($sink, $contents);
109  } elseif (is_string($sink)) {
110  file_put_contents($sink, $contents);
111  } elseif ($sink instanceof \Psr\Http\Message\StreamInterface) {
112  $sink->write($contents);
113  }
114  }
115 
116  return $value;
117  },
118  function ($reason) use ($request, $options) {
119  $this->invokeStats($request, $options, null, $reason);
120  if ($this->onRejected) {
121  call_user_func($this->onRejected, $reason);
122  }
123  return \GuzzleHttp\Promise\rejection_for($reason);
124  }
125  );
126  }
127 
132  public function append()
133  {
134  foreach (func_get_args() as $value) {
135  if ($value instanceof ResponseInterface
136  || $value instanceof \Exception
137  || $value instanceof PromiseInterface
138  || is_callable($value)
139  ) {
140  $this->queue[] = $value;
141  } else {
142  throw new \InvalidArgumentException('Expected a response or '
143  . 'exception. Found ' . \GuzzleHttp\describe_type($value));
144  }
145  }
146  }
147 
153  public function getLastRequest()
154  {
155  return $this->lastRequest;
156  }
157 
163  public function getLastOptions()
164  {
165  return $this->lastOptions;
166  }
167 
173  public function count()
174  {
175  return count($this->queue);
176  }
177 
178  public function reset()
179  {
180  $this->queue = [];
181  }
182 
183  private function invokeStats(
184  RequestInterface $request,
185  array $options,
186  ResponseInterface $response = null,
187  $reason = null
188  ) {
189  if (isset($options['on_stats'])) {
190  $transferTime = isset($options['transfer_time']) ? $options['transfer_time'] : 0;
191  $stats = new TransferStats($request, $response, $transferTime, $reason);
192  call_user_func($options['on_stats'], $stats);
193  }
194  }
195 }
GuzzleHttp\Handler\MockHandler\append
append()
Definition: MockHandler.php:132
GuzzleHttp\Exception\RequestException
Definition: vendor/guzzlehttp/guzzle/src/Exception/RequestException.php:12
GuzzleHttp
Definition: vendor/guzzlehttp/guzzle/src/Client.php:2
GuzzleHttp\Handler\MockHandler
Definition: MockHandler.php:15
GuzzleHttp\Handler\MockHandler\getLastRequest
getLastRequest()
Definition: MockHandler.php:153
GuzzleHttp\Handler\MockHandler\__invoke
__invoke(RequestInterface $request, array $options)
Definition: MockHandler.php:63
Psr\Http\Message\StreamInterface
Definition: vendor/psr/http-message/src/StreamInterface.php:12
GuzzleHttp\Promise\PromiseInterface
Definition: PromiseInterface.php:13
Psr\Http\Message\RequestInterface
Definition: vendor/psr/http-message/src/RequestInterface.php:24
GuzzleHttp\Handler\MockHandler\reset
reset()
Definition: MockHandler.php:178
Psr
GuzzleHttp\Handler
Definition: CurlFactory.php:2
GuzzleHttp\Promise\queue
queue(TaskQueueInterface $assign=null)
Definition: guzzlehttp/promises/src/functions.php:21
GuzzleHttp\describe_type
describe_type($input)
Definition: guzzlehttp/guzzle/src/functions.php:41
GuzzleHttp\Handler\MockHandler\getLastOptions
getLastOptions()
Definition: MockHandler.php:163
GuzzleHttp\Handler\MockHandler\__construct
__construct(array $queue=null, callable $onFulfilled=null, callable $onRejected=null)
Definition: MockHandler.php:50
GuzzleHttp\HandlerStack\create
static create(callable $handler=null)
Definition: HandlerStack.php:49
GuzzleHttp\HandlerStack
Definition: HandlerStack.php:12
GuzzleHttp\TransferStats
Definition: TransferStats.php:12
Psr\Http\Message\ResponseInterface
Definition: vendor/psr/http-message/src/ResponseInterface.php:20
GuzzleHttp\Handler\MockHandler\createWithMiddleware
static createWithMiddleware(array $queue=null, callable $onFulfilled=null, callable $onRejected=null)
Definition: MockHandler.php:33
Http
GuzzleHttp\Handler\MockHandler\count
count()
Definition: MockHandler.php:173
GuzzleHttp\Promise\RejectedPromise
Definition: guzzlehttp/promises/src/RejectedPromise.php:10