You are here

protected function S3fsTestBase::setUp in S3 File System 8.3

Same name and namespace in other branches
  1. 4.0.x tests/src/Functional/S3fsTestBase.php \Drupal\Tests\s3fs\Functional\S3fsTestBase::setUp()

Overrides BrowserTestBase::setUp

2 calls to S3fsTestBase::setUp()
S3fsConfigFormTest::setUp in tests/src/Functional/S3fsConfigFormTest.php
S3fsDrushTest::setUp in tests/src/Functional/S3fsDrushTest.php
2 methods override S3fsTestBase::setUp()
S3fsConfigFormTest::setUp in tests/src/Functional/S3fsConfigFormTest.php
S3fsDrushTest::setUp in tests/src/Functional/S3fsDrushTest.php

File

tests/src/Functional/S3fsTestBase.php, line 100

Class

S3fsTestBase
S3 File System Test Base.

Namespace

Drupal\Tests\s3fs\Functional

Code

protected function setUp() : void {
  parent::setUp();
  $this
    ->prepareConfig($this
    ->config('s3fs.settings'));
  if (empty($this->s3Config['bucket'])) {

    // No sense to test anything if credentials absent.
    $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) {

      // Bucket possibly was created by another script between checking.
      $this->bucketNotFound = !$this->s3
        ->doesBucketExist($this->s3Config['bucket']);
    } catch (AwsException $e) {

      // No need to continue tests if can't access the bucket. Either the
      // credentials are incorrect or problem with S3Client.
      $this
        ->fail("Unable to create bucket '{$this->s3Config['bucket']}' in region '{$this->s3Config['region']}'.\n          Please verify the S3 settings.");
    }
    if ($this->bucketNotFound) {

      // Wait before we loop again.
      sleep(5);
    }
    $connectAttempts++;
  }
  if (!$this->bucketNotFound) {

    // Empty out the bucket before the test, to prevent unexpected errors.
    $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.");
  }
}