S3fsTestBase.php in S3 File System 8.3
File
tests/src/Functional/S3fsTestBase.php
View source
<?php
namespace Drupal\Tests\s3fs\Functional;
use Aws\Exception\AwsException;
use Aws\S3\Exception\S3Exception;
use Drupal\Core\Config\Config;
use Drupal\Tests\BrowserTestBase;
abstract class S3fsTestBase extends BrowserTestBase {
protected static $modules = [
's3fs',
];
protected $defaultTheme = 'stark';
protected $connection;
protected $s3Config;
protected $s3;
protected $s3fs;
protected $bucketNotFound = FALSE;
protected $remoteTestsFolder = '_s3fs_tests';
protected $remoteTestsFolderKey = '';
protected $remoteTestsFolderUri = '';
protected function setUp() : void {
parent::setUp();
$this
->prepareConfig($this
->config('s3fs.settings'));
if (empty($this->s3Config['bucket'])) {
$this->bucketNotFound = TRUE;
$this
->markTestSkipped('S3 not configured');
}
$this->s3fs = \Drupal::service('s3fs');
$this->connection = $this->container
->get('database');
$this->s3 = $this->s3fs
->getAmazonS3Client($this->s3Config);
$this->remoteTestsFolderKey = $this->s3Config['root_folder'];
$this->remoteTestsFolderUri = "s3://{$this->remoteTestsFolder}";
$this->bucketNotFound = !$this->s3
->doesBucketExist($this->s3Config['bucket']);
$connectAttempts = 0;
while ($this->bucketNotFound && $connectAttempts <= 5) {
try {
$result = $this->s3
->createBucket([
'Bucket' => $this->s3Config['bucket'],
]);
$this->bucketNotFound = FALSE;
} catch (S3Exception $e) {
$this->bucketNotFound = !$this->s3
->doesBucketExist($this->s3Config['bucket']);
} catch (AwsException $e) {
$this
->fail("Unable to create bucket '{$this->s3Config['bucket']}' in region '{$this->s3Config['region']}'.\n Please verify the S3 settings.");
}
if ($this->bucketNotFound) {
sleep(5);
}
$connectAttempts++;
}
if (!$this->bucketNotFound) {
$this->s3
->deleteMatchingObjects($this->s3Config['bucket'], $this->remoteTestsFolderKey);
$this
->verbose("Deleted file(s) from S3 test folder to prepare for the test.");
}
else {
$this
->fail("Unable to access bucket '{$this->s3Config['bucket']}' in region '{$this->s3Config['region']}'.\n Please verify the S3 settings.");
}
}
protected function tearDown() : void {
if (!$this->bucketNotFound) {
$this->s3
->deleteMatchingObjects($this->s3Config['bucket'], $this->remoteTestsFolderKey);
}
parent::tearDown();
}
protected function prepareConfig(Config $config) {
$this->s3Config = [];
$settings = [];
$config
->set('bucket', 's3fs-test-bucket')
->set('region', 'us-east-1')
->set('use_customhost', TRUE)
->set('hostname', 's3fslocalstack:4566')
->save();
$settings['settings']['s3fs.access_key'] = (object) [
'value' => 'test',
'required' => TRUE,
];
$settings['settings']['s3fs.secret_key'] = (object) [
'value' => 'test',
'required' => TRUE,
];
if (getenv('S3FS_AWS_BUCKET')) {
$config
->set('bucket', getenv('S3FS_AWS_BUCKET'))
->save();
}
if (getenv('S3FS_AWS_REGION')) {
$config
->set('region', getenv('S3FS_AWS_REGION'))
->save();
}
if (!empty(getenv('S3FS_AWS_NO_CUSTOM_HOST'))) {
$config
->set('use_customhost', FALSE)
->save();
}
if (!empty(getenv('S3FS_AWS_CUSTOM_HOST'))) {
$config
->set('hostname', getenv('S3FS_AWS_CUSTOM_HOST'))
->save();
}
if (getenv('S3FS_AWS_KEY')) {
$settings['settings']['s3fs.access_key'] = (object) [
'value' => getenv('S3FS_AWS_KEY'),
'required' => TRUE,
];
}
if (getenv('S3FS_AWS_SECRET')) {
$settings['settings']['s3fs.secret_key'] = (object) [
'value' => getenv('S3FS_AWS_SECRET'),
'required' => TRUE,
];
}
$rootPath = $this->remoteTestsFolder . '/' . uniqid('', TRUE);
if (!empty($config
->get('root_folder'))) {
$rootPath = $config
->get('root_folder') . '/' . $rootPath;
}
$config
->set('root_folder', $rootPath)
->save();
$this
->writeSettings($settings);
foreach ($config
->get() as $prop => $value) {
$this->s3Config[$prop] = $value;
}
}
}