35 callable $onFulfilled =
null,
36 callable $onRejected =
null
52 callable $onFulfilled =
null,
53 callable $onRejected =
null
55 $this->onFulfilled = $onFulfilled;
56 $this->onRejected = $onRejected;
59 call_user_func_array([$this,
'append'], $queue);
66 throw new \OutOfBoundsException(
'Mock queue is empty');
69 if (isset($options[
'delay']) && is_numeric($options[
'delay'])) {
70 usleep($options[
'delay'] * 1000);
73 $this->lastRequest = $request;
74 $this->lastOptions = $options;
75 $response = array_shift($this->
queue);
77 if (isset($options[
'on_headers'])) {
78 if (!is_callable($options[
'on_headers'])) {
79 throw new \InvalidArgumentException(
'on_headers must be callable');
82 $options[
'on_headers']($response);
83 }
catch (\Exception $e) {
84 $msg =
'An error was encountered during the on_headers event';
89 if (is_callable($response)) {
90 $response = call_user_func($response, $request, $options);
93 $response = $response instanceof \Exception
94 ? \GuzzleHttp\Promise\rejection_for($response)
95 : \GuzzleHttp\Promise\promise_for($response);
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);
103 if (isset($options[
'sink'])) {
104 $contents = (string) $value->getBody();
105 $sink = $options[
'sink'];
107 if (is_resource($sink)) {
108 fwrite($sink, $contents);
109 } elseif (is_string($sink)) {
110 file_put_contents($sink, $contents);
112 $sink->write($contents);
118 function ($reason) use ($request, $options) {
119 $this->invokeStats($request, $options,
null, $reason);
120 if ($this->onRejected) {
121 call_user_func($this->onRejected, $reason);
123 return \GuzzleHttp\Promise\rejection_for($reason);
134 foreach (func_get_args() as $value) {
136 || $value instanceof \Exception
138 || is_callable($value)
140 $this->
queue[] = $value;
142 throw new \InvalidArgumentException(
'Expected a response or '
155 return $this->lastRequest;
165 return $this->lastOptions;
183 private function invokeStats(
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);