GuzzleHttpHandlerMockHandler

Handler that returns responses or throw exceptions from a queue.

Defined (1)

The class is defined in the following location(s).

/lib/Azure/GuzzleHttp/Handler/MockHandler.php  
  1. class MockHandler implements \Countable 
  2. private $queue; 
  3. private $lastRequest; 
  4. private $lastOptions; 
  5. private $onFulfilled; 
  6. private $onRejected; 
  7.  
  8. /** 
  9. * Creates a new MockHandler that uses the default handler stack list of 
  10. * middlewares. 
  11. * @param array $queue Array of responses, callables, or exceptions. 
  12. * @param callable $onFulfilled Callback to invoke when the return value is fulfilled. 
  13. * @param callable $onRejected Callback to invoke when the return value is rejected. 
  14. * @return HandlerStack 
  15. */ 
  16. public static function createWithMiddleware( 
  17. array $queue = null,  
  18. callable $onFulfilled = null,  
  19. callable $onRejected = null 
  20. ) { 
  21. return HandlerStack::create(new self($queue, $onFulfilled, $onRejected)); 
  22.  
  23. /** 
  24. * The passed in value must be an array of 
  25. * {@see Psr7\Http\Message\ResponseInterface} objects, Exceptions,  
  26. * callables, or Promises. 
  27. * @param array $queue 
  28. * @param callable $onFulfilled Callback to invoke when the return value is fulfilled. 
  29. * @param callable $onRejected Callback to invoke when the return value is rejected. 
  30. */ 
  31. public function __construct( 
  32. array $queue = null,  
  33. callable $onFulfilled = null,  
  34. callable $onRejected = null 
  35. ) { 
  36. $this->onFulfilled = $onFulfilled; 
  37. $this->onRejected = $onRejected; 
  38.  
  39. if ($queue) { 
  40. call_user_func_array([$this, 'append'], $queue); 
  41.  
  42. public function __invoke(RequestInterface $request, array $options) 
  43. if (!$this->queue) { 
  44. throw new \OutOfBoundsException('Mock queue is empty'); 
  45.  
  46. if (isset($options['delay'])) { 
  47. usleep($options['delay'] * 1000); 
  48.  
  49. $this->lastRequest = $request; 
  50. $this->lastOptions = $options; 
  51. $response = array_shift($this->queue); 
  52.  
  53. if (is_callable($response)) { 
  54. $response = call_user_func($response, $request, $options); 
  55.  
  56. $response = $response instanceof \Exception 
  57. ? new RejectedPromise($response) 
  58. : \GuzzleHttp\Promise\promise_for($response); 
  59.  
  60. return $response->then( 
  61. function ($value) use ($request, $options) { 
  62. $this->invokeStats($request, $options, $value); 
  63. if ($this->onFulfilled) { 
  64. call_user_func($this->onFulfilled, $value); 
  65. if (isset($options['sink'])) { 
  66. $contents = (string) $value->getBody(); 
  67. $sink = $options['sink']; 
  68.  
  69. if (is_resource($sink)) { 
  70. fwrite($sink, $contents); 
  71. } elseif (is_string($sink)) { 
  72. file_put_contents($sink, $contents); 
  73. } elseif ($sink instanceof \Psr\Http\Message\StreamInterface) { 
  74. $sink->write($contents); 
  75.  
  76. return $value; 
  77. },  
  78. function ($reason) use ($request, $options) { 
  79. $this->invokeStats($request, $options, null, $reason); 
  80. if ($this->onRejected) { 
  81. call_user_func($this->onRejected, $reason); 
  82. return new RejectedPromise($reason); 
  83. ); 
  84.  
  85. /** 
  86. * Adds one or more variadic requests, exceptions, callables, or promises 
  87. * to the queue. 
  88. */ 
  89. public function append() 
  90. foreach (func_get_args() as $value) { 
  91. if ($value instanceof ResponseInterface 
  92. || $value instanceof \Exception 
  93. || $value instanceof PromiseInterface 
  94. || is_callable($value) 
  95. ) { 
  96. $this->queue[] = $value; 
  97. } else { 
  98. throw new \InvalidArgumentException('Expected a response or ' 
  99. . 'exception. Found ' . \GuzzleHttp\describe_type($value)); 
  100.  
  101. /** 
  102. * Get the last received request. 
  103. * @return RequestInterface 
  104. */ 
  105. public function getLastRequest() 
  106. return $this->lastRequest; 
  107.  
  108. /** 
  109. * Get the last received request options. 
  110. * @return RequestInterface 
  111. */ 
  112. public function getLastOptions() 
  113. return $this->lastOptions; 
  114.  
  115. /** 
  116. * Returns the number of remaining items in the queue. 
  117. * @return int 
  118. */ 
  119. public function count() 
  120. return count($this->queue); 
  121.  
  122. private function invokeStats( 
  123. RequestInterface $request,  
  124. array $options,  
  125. ResponseInterface $response = null,  
  126. $reason = null 
  127. ) { 
  128. if (isset($options['on_stats'])) { 
  129. $stats = new TransferStats($request, $response, 0, $reason); 
  130. call_user_func($options['on_stats'], $stats);