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