Skip to content

Commit 96385e3

Browse files
shironpavelsavelyev
authored andcommitted
add aws s3 adapter (#5)
1 parent 6349532 commit 96385e3

File tree

4 files changed

+111
-0
lines changed

4 files changed

+111
-0
lines changed

README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,16 @@ Provides a set of methods for checking and modifying files on remote storage.
3737
password: "password"
3838
port: "22"
3939
root: "/"
40+
s3:
41+
builderAdapter: \Lamoda\Codeception\Extension\AdapterFactory\AwsS3AdapterFactory
42+
config:
43+
bucket: "your-bucket"
44+
endpoint: "endpoint" # if you are using S3-compatible object storage service
45+
credentials:
46+
key: "key"
47+
secret: "secret"
48+
region: "region"
49+
version: "version"
4050
```
4151

4252
3. Include to suite
@@ -97,6 +107,33 @@ Usage:
97107
$fileSystem = $this->tester->getFileSystem('webdav');
98108
```
99109

110+
### [AWS S3](https://flysystem.thephpleague.com/adapter/aws-s3/)
111+
112+
Configuration example:
113+
114+
```yaml
115+
modules:
116+
config:
117+
\Lamoda\Codeception\Extension\FlySystemModule:
118+
adapters:
119+
s3:
120+
builderAdapter: \Lamoda\Codeception\Extension\AdapterFactory\AwsS3AdapterFactory
121+
config:
122+
bucket: "your-bucket"
123+
endpoint: "endpoint" # if you are using S3-compatible object storage service
124+
credentials:
125+
key: "key"
126+
secret: "secret"
127+
region: "region"
128+
version: "version"
129+
```
130+
131+
Usage:
132+
133+
```php
134+
$fileSystem = $this->tester->getFileSystem('s3');
135+
```
136+
100137
## Usage
101138

102139
Get instance of FileSystem by name from config:

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"php": ">=7.1",
99
"codeception/codeception": "~2.5",
1010
"oneup/flysystem-bundle": "~3.0",
11+
"league/flysystem-aws-s3-v3": "~1.0",
1112
"league/flysystem-sftp": "~1.0",
1213
"league/flysystem-webdav": "~1.0"
1314
},
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Lamoda\Codeception\Extension\AdapterFactory;
4+
5+
use Codeception\Exception\ModuleConfigException;
6+
use League\Flysystem\AdapterInterface;
7+
use Aws\S3\S3Client;
8+
use League\Flysystem\AwsS3v3\AwsS3Adapter;
9+
10+
class AwsS3AdapterFactory implements AdapterFactoryInterface
11+
{
12+
/**
13+
* @param array $config
14+
*
15+
* @return AdapterInterface
16+
*/
17+
public static function createAdapter(array $config)
18+
{
19+
if(!isset($config['bucket']) or !is_string($config['bucket'])) {
20+
$message = sprintf('Configuration for s3 is broken. Configuration must contains bucket parameter with string type.');
21+
throw new ModuleConfigException(__CLASS__, $message);
22+
}
23+
24+
$client = new S3Client($config);
25+
26+
return new AwsS3Adapter($client, $config['bucket']);
27+
}
28+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
namespace Lamoda\Codeception\Tests\Extension\AdapterFactory;
4+
5+
use Codeception\Exception\ModuleConfigException;
6+
use Codeception\Test\Unit;
7+
use Lamoda\Codeception\Extension\AdapterFactory\AwsS3AdapterFactory;
8+
use League\Flysystem\AwsS3v3\AwsS3Adapter;
9+
10+
class AwsS3AdapterFactoryTest extends Unit
11+
{
12+
public function testCreateAdapter()
13+
{
14+
$adapter = AwsS3AdapterFactory::createAdapter(['bucket' => 'your-bucket', 'region' => 'your-region', 'version' => 'latest']);
15+
$this->assertInstanceOf(AwsS3Adapter::class, $adapter);
16+
}
17+
18+
/**
19+
* @param array $config
20+
*
21+
* @throws \Codeception\Exception\ModuleConfigException
22+
*
23+
* @dataProvider dataFailedCreateAdapter
24+
*/
25+
public function testFailedCreateAdapter($config)
26+
{
27+
$expectedExceptionMessage = 'Configuration for s3 is broken. Configuration must contains bucket parameter with string type.';
28+
$this->expectException(ModuleConfigException::class);
29+
$this->expectExceptionMessage($expectedExceptionMessage);
30+
31+
AwsS3AdapterFactory::createAdapter($config);
32+
}
33+
34+
public function dataFailedCreateAdapter()
35+
{
36+
return [
37+
[
38+
[]
39+
],
40+
[
41+
['bucket' => ['str']]
42+
]
43+
];
44+
}
45+
}

0 commit comments

Comments
 (0)