| 1 |
<?php |
| 2 |
namespace Aws; |
| 3 |
|
| 4 |
use Aws\Exception\AwsException; |
| 5 |
use GuzzleHttp\Promise; |
| 6 |
use GuzzleHttp\Promise\RejectedPromise; |
| 7 |
use Psr\Http\Message\RequestInterface; |
| 8 |
use Exception; |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
class MockHandler implements \Countable |
| 15 |
{ |
| 16 |
private $queue; |
| 17 |
private $lastCommand; |
| 18 |
private $lastRequest; |
| 19 |
private $onFulfilled; |
| 20 |
private $onRejected; |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
public function __construct( |
| 32 |
array $resultOrQueue = [], |
| 33 |
?callable $onFulfilled = null, |
| 34 |
?callable $onRejected = null |
| 35 |
) { |
| 36 |
$this->queue = []; |
| 37 |
$this->onFulfilled = $onFulfilled; |
| 38 |
$this->onRejected = $onRejected; |
| 39 |
|
| 40 |
if ($resultOrQueue) { |
| 41 |
call_user_func_array([$this, 'append'], array_values($resultOrQueue)); |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
public function append() |
| 50 |
{ |
| 51 |
foreach (func_get_args() as $value) { |
| 52 |
if ($value instanceof ResultInterface |
| 53 |
|| $value instanceof Exception |
| 54 |
|| is_callable($value) |
| 55 |
) { |
| 56 |
$this->queue[] = $value; |
| 57 |
} else { |
| 58 |
throw new \InvalidArgumentException('Expected an Aws\ResultInterface or Exception.'); |
| 59 |
} |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
public function appendException() |
| 67 |
{ |
| 68 |
foreach (func_get_args() as $value) { |
| 69 |
if ($value instanceof \Exception || $value instanceof \Throwable) { |
| 70 |
$this->queue[] = $value; |
| 71 |
} else { |
| 72 |
throw new \InvalidArgumentException('Expected an \Exception or \Throwable.'); |
| 73 |
} |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
public function __invoke( |
| 78 |
CommandInterface $command, |
| 79 |
RequestInterface $request |
| 80 |
) { |
| 81 |
if (!$this->queue) { |
| 82 |
$last = $this->lastCommand |
| 83 |
? ' The last command sent was ' . $this->lastCommand->getName() . '.' |
| 84 |
: ''; |
| 85 |
throw new \RuntimeException('Mock queue is empty. Trying to send a ' |
| 86 |
. $command->getName() . ' command failed.' . $last); |
| 87 |
} |
| 88 |
|
| 89 |
$this->lastCommand = $command; |
| 90 |
$this->lastRequest = $request; |
| 91 |
|
| 92 |
$result = array_shift($this->queue); |
| 93 |
|
| 94 |
if (is_callable($result)) { |
| 95 |
$result = $result($command, $request); |
| 96 |
} |
| 97 |
|
| 98 |
if ($result instanceof \Exception) { |
| 99 |
$result = new RejectedPromise($result); |
| 100 |
} else { |
| 101 |
|
| 102 |
$meta = $result['@metadata']; |
| 103 |
if (!isset($meta['effectiveUri'])) { |
| 104 |
$meta['effectiveUri'] = (string) $request->getUri(); |
| 105 |
} |
| 106 |
if (!isset($meta['statusCode'])) { |
| 107 |
$meta['statusCode'] = 200; |
| 108 |
} |
| 109 |
$result['@metadata'] = $meta; |
| 110 |
$result = Promise\Create::promiseFor($result); |
| 111 |
} |
| 112 |
|
| 113 |
$result->then($this->onFulfilled, $this->onRejected); |
| 114 |
|
| 115 |
return $result; |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
public function getLastRequest() |
| 124 |
{ |
| 125 |
return $this->lastRequest; |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
public function getLastCommand() |
| 134 |
{ |
| 135 |
return $this->lastCommand; |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
#[\ReturnTypeWillChange] |
| 144 |
public function count() |
| 145 |
{ |
| 146 |
return count($this->queue); |
| 147 |
} |
| 148 |
} |