-
Notifications
You must be signed in to change notification settings - Fork 4
/
S3ClientStub.php
188 lines (162 loc) · 5.42 KB
/
S3ClientStub.php
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
<?php
declare(strict_types=1);
namespace League\Flysystem\AsyncAwsS3;
use AsyncAws\Core\Exception\Exception;
use AsyncAws\Core\Exception\Http\NetworkException;
use AsyncAws\Core\Result;
use AsyncAws\S3\Input\CopyObjectRequest;
use AsyncAws\S3\Input\DeleteObjectRequest;
use AsyncAws\S3\Input\DeleteObjectsRequest;
use AsyncAws\S3\Input\GetObjectAclRequest;
use AsyncAws\S3\Input\GetObjectRequest;
use AsyncAws\S3\Input\HeadObjectRequest;
use AsyncAws\S3\Input\ListObjectsV2Request;
use AsyncAws\S3\Input\PutObjectAclRequest;
use AsyncAws\S3\Input\PutObjectRequest;
use AsyncAws\S3\Result\CopyObjectOutput;
use AsyncAws\S3\Result\DeleteObjectOutput;
use AsyncAws\S3\Result\DeleteObjectsOutput;
use AsyncAws\S3\Result\GetObjectAclOutput;
use AsyncAws\S3\Result\GetObjectOutput;
use AsyncAws\S3\Result\HeadObjectOutput;
use AsyncAws\S3\Result\ListObjectsV2Output;
use AsyncAws\S3\Result\ObjectExistsWaiter;
use AsyncAws\S3\Result\PutObjectAclOutput;
use AsyncAws\S3\Result\PutObjectOutput;
use AsyncAws\S3\S3Client;
use AsyncAws\SimpleS3\SimpleS3Client;
use DateTimeImmutable;
use Symfony\Component\HttpClient\MockHttpClient;
/**
* @codeCoverageIgnore
*/
class S3ClientStub extends SimpleS3Client
{
/**
* @var S3Client
*/
private $actualClient;
/**
* @var Exception[]
*/
private $stagedExceptions = [];
/**
* @var Result[]
*/
private $stagedResult = [];
public function __construct(SimpleS3Client $client, $configuration = [])
{
$this->actualClient = $client;
parent::__construct($configuration, null, new MockHttpClient());
}
public function throwExceptionWhenExecutingCommand(string $commandName, ?Exception $exception = null): void
{
$this->stagedExceptions[$commandName] = $exception ?? new NetworkException();
}
public function stageResultForCommand(string $commandName, Result $result): void
{
$this->stagedResult[$commandName] = $result;
}
private function getStagedResult(string $name): ?Result
{
if (array_key_exists($name, $this->stagedExceptions)) {
$exception = $this->stagedExceptions[$name];
unset($this->stagedExceptions[$name]);
throw $exception;
}
if (array_key_exists($name, $this->stagedResult)) {
$result = $this->stagedResult[$name];
unset($this->stagedResult[$name]);
return $result;
}
return null;
}
/**
* @param array|CopyObjectRequest $input
*/
public function copyObject($input): CopyObjectOutput
{
// @phpstan-ignore-next-line
return $this->getStagedResult('CopyObject') ?? $this->actualClient->copyObject($input);
}
/**
* @param array|DeleteObjectRequest $input
*/
public function deleteObject($input): DeleteObjectOutput
{
// @phpstan-ignore-next-line
return $this->getStagedResult('DeleteObject') ?? $this->actualClient->deleteObject($input);
}
/**
* @param array|HeadObjectRequest $input
*/
public function headObject($input): HeadObjectOutput
{
// @phpstan-ignore-next-line
return $this->getStagedResult('HeadObject') ?? $this->actualClient->headObject($input);
}
/**
* @param array|HeadObjectRequest $input
*/
public function objectExists($input): ObjectExistsWaiter
{
// @phpstan-ignore-next-line
return $this->getStagedResult('ObjectExists') ?? $this->actualClient->objectExists($input);
}
/**
* @param array|ListObjectsV2Request $input
*/
public function listObjectsV2($input): ListObjectsV2Output
{
// @phpstan-ignore-next-line
return $this->getStagedResult('ListObjectsV2') ?? $this->actualClient->listObjectsV2($input);
}
/**
* @param array|DeleteObjectsRequest $input
*/
public function deleteObjects($input): DeleteObjectsOutput
{
// @phpstan-ignore-next-line
return $this->getStagedResult('DeleteObjects') ?? $this->actualClient->deleteObjects($input);
}
/**
* @param array|GetObjectAclRequest $input
*/
public function getObjectAcl($input): GetObjectAclOutput
{
// @phpstan-ignore-next-line
return $this->getStagedResult('GetObjectAcl') ?? $this->actualClient->getObjectAcl($input);
}
/**
* @param array|PutObjectAclRequest $input
*/
public function putObjectAcl($input): PutObjectAclOutput
{
// @phpstan-ignore-next-line
return $this->getStagedResult('PutObjectAcl') ?? $this->actualClient->putObjectAcl($input);
}
/**
* @param array|PutObjectRequest $input
*/
public function putObject($input): PutObjectOutput
{
// @phpstan-ignore-next-line
return $this->getStagedResult('PutObject') ?? $this->actualClient->putObject($input);
}
/**
* @param array|GetObjectRequest $input
*/
public function getObject($input): GetObjectOutput
{
// @phpstan-ignore-next-line
return $this->getStagedResult('GetObject') ?? $this->actualClient->getObject($input);
}
public function getUrl(string $bucket, string $key): string
{
return $this->actualClient->getUrl($bucket, $key);
}
public function getPresignedUrl(string $bucket, string $key, ?DateTimeImmutable $expires = null, ?string $versionId = null): string
{
return $this->actualClient->getPresignedUrl($bucket, $key, $expires);
}
}