MockHandler.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. namespace Aws;
  3. use Aws\Exception\AwsException;
  4. use GuzzleHttp\Promise;
  5. use GuzzleHttp\Promise\RejectedPromise;
  6. use Psr\Http\Message\RequestInterface;
  7. use Exception;
  8. /**
  9. * Returns promises that are rejected or fulfilled using a queue of
  10. * Aws\ResultInterface and Aws\Exception\AwsException objects.
  11. */
  12. class MockHandler implements \Countable
  13. {
  14. private $queue;
  15. private $lastCommand;
  16. private $lastRequest;
  17. private $onFulfilled;
  18. private $onRejected;
  19. /**
  20. * The passed in value must be an array of {@see Aws\ResultInterface} or
  21. * {@see AwsException} objects that acts as a queue of results or
  22. * exceptions to return each time the handler is invoked.
  23. *
  24. * @param array $resultOrQueue
  25. * @param callable $onFulfilled Callback to invoke when the return value is fulfilled.
  26. * @param callable $onRejected Callback to invoke when the return value is rejected.
  27. */
  28. public function __construct(
  29. array $resultOrQueue = [],
  30. callable $onFulfilled = null,
  31. callable $onRejected = null
  32. ) {
  33. $this->onFulfilled = $onFulfilled;
  34. $this->onRejected = $onRejected;
  35. if ($resultOrQueue) {
  36. call_user_func_array([$this, 'append'], $resultOrQueue);
  37. }
  38. }
  39. /**
  40. * Adds one or more variadic ResultInterface or AwsException objects to the
  41. * queue.
  42. */
  43. public function append()
  44. {
  45. foreach (func_get_args() as $value) {
  46. if ($value instanceof ResultInterface
  47. || $value instanceof Exception
  48. || is_callable($value)
  49. ) {
  50. $this->queue[] = $value;
  51. } else {
  52. throw new \InvalidArgumentException('Expected an Aws\ResultInterface or Exception.');
  53. }
  54. }
  55. }
  56. /**
  57. * Adds one or more \Exception or \Throwable to the queue
  58. */
  59. public function appendException()
  60. {
  61. foreach (func_get_args() as $value) {
  62. if ($value instanceof \Exception || $value instanceof \Throwable) {
  63. $this->queue[] = $value;
  64. } else {
  65. throw new \InvalidArgumentException('Expected an \Exception or \Throwable.');
  66. }
  67. }
  68. }
  69. public function __invoke(
  70. CommandInterface $command,
  71. RequestInterface $request
  72. ) {
  73. if (!$this->queue) {
  74. $last = $this->lastCommand
  75. ? ' The last command sent was ' . $this->lastCommand->getName() . '.'
  76. : '';
  77. throw new \RuntimeException('Mock queue is empty. Trying to send a '
  78. . $command->getName() . ' command failed.' . $last);
  79. }
  80. $this->lastCommand = $command;
  81. $this->lastRequest = $request;
  82. $result = array_shift($this->queue);
  83. if (is_callable($result)) {
  84. $result = $result($command, $request);
  85. }
  86. if ($result instanceof \Exception) {
  87. $result = new RejectedPromise($result);
  88. } else {
  89. // Add an effective URI and statusCode if not present.
  90. $meta = $result['@metadata'];
  91. if (!isset($meta['effectiveUri'])) {
  92. $meta['effectiveUri'] = (string) $request->getUri();
  93. }
  94. if (!isset($meta['statusCode'])) {
  95. $meta['statusCode'] = 200;
  96. }
  97. $result['@metadata'] = $meta;
  98. $result = Promise\promise_for($result);
  99. }
  100. $result->then($this->onFulfilled, $this->onRejected);
  101. return $result;
  102. }
  103. /**
  104. * Get the last received request.
  105. *
  106. * @return RequestInterface
  107. */
  108. public function getLastRequest()
  109. {
  110. return $this->lastRequest;
  111. }
  112. /**
  113. * Get the last received command.
  114. *
  115. * @return CommandInterface
  116. */
  117. public function getLastCommand()
  118. {
  119. return $this->lastCommand;
  120. }
  121. /**
  122. * Returns the number of remaining items in the queue.
  123. *
  124. * @return int
  125. */
  126. public function count()
  127. {
  128. return count($this->queue);
  129. }
  130. }