MockHandler.php 3.6 KB

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