You are here

class S3Test in Flysystem - S3 8

Same name and namespace in other branches
  1. 2.0.x tests/src/Unit/Flysystem/S3Test.php \NoDrupal\Tests\flysystem_s3\Unit\Flysystem\S3Test

@coversDefaultClass \Drupal\flysystem_s3\Flysystem\S3 @covers \Drupal\flysystem_s3\Flysystem\S3 @group flysystem_s3

Hierarchy

Expanded class hierarchy of S3Test

File

tests/src/Unit/Flysystem/S3Test.php, line 24

Namespace

NoDrupal\Tests\flysystem_s3\Unit\Flysystem
View source
class S3Test extends UnitTestCase {
  public function test() {
    $configuration = [
      'bucket' => 'example-bucket',
      'prefix' => 'test prefix',
      'cname' => 'example.com',
    ];
    $client = new S3Client([
      'version' => 'latest',
      'region' => 'beep',
      'credentials' => new Credentials('fsdf', 'sfsdf'),
    ]);
    $plugin = new S3($client, new Config($configuration));
    $this
      ->assertInstanceOf(AdapterInterface::class, $plugin
      ->getAdapter());
    $this
      ->assertSame('http://example.com/test%20prefix/foo%201.html', $plugin
      ->getExternalUrl('s3://foo 1.html'));
    $configuration['prefix'] = '';
    $plugin = new S3($client, new Config($configuration));
    $this
      ->assertSame('http://example.com/foo%201.html', $plugin
      ->getExternalUrl('s3://foo 1.html'));
  }

  /**
   * Tests merging defaults into configuration arrays.
   */
  public function testMergeConfiguration() {
    $container = new ContainerBuilder();
    $container
      ->set('request_stack', new RequestStack());
    $container
      ->get('request_stack')
      ->push(Request::create('https://example.com/'));
    $configuration = [
      'key' => 'fee',
      'secret' => 'fo',
      'region' => 'eu-west-1',
      'bucket' => 'example-bucket',
    ];
    $configuration = S3::mergeConfiguration($container, $configuration);
    $this
      ->assertSame('https', $configuration['protocol']);
    $client_config = S3::mergeClientConfiguration($container, $configuration);
    $this
      ->assertSame('eu-west-1', $client_config['region']);
    $this
      ->assertNull($client_config['endpoint']);
    $this
      ->assertInstanceOf(Credentials::class, $client_config['credentials']);
  }
  public function testCreate() {
    $container = new ContainerBuilder();
    $container
      ->set('request_stack', new RequestStack());
    $container
      ->get('request_stack')
      ->push(Request::create('https://example.com/'));
    $configuration = [
      'key' => 'fee',
      'secret' => 'fo',
      'region' => 'eu-west-1',
      'bucket' => 'example-bucket',
    ];
    $plugin = S3::create($container, $configuration, '', '');
    $this
      ->assertInstanceOf(S3::class, $plugin);
  }
  public function testCreateUsingNonAwsConfiguration() {
    $container = new ContainerBuilder();
    $container
      ->set('request_stack', new RequestStack());
    $container
      ->get('request_stack')
      ->push(Request::create('https://example.com/'));
    $configuration = [
      'key' => 'fee',
      'secret' => 'fo',
      'region' => 'eu-west-1',
      'cname' => 'something.somewhere.tld',
      'endpoint' => 'https://api.somewhere.tld',
    ];
    $plugin = S3::create($container, $configuration, '', '');
    $this
      ->assertSame('https://something.somewhere.tld/foo%201.html', $plugin
      ->getExternalUrl('s3://foo 1.html'));
    $this
      ->assertSame('https://api.somewhere.tld', (string) $plugin
      ->getAdapter()
      ->getClient()
      ->getEndpoint());
  }
  public function testCreateUsingNonAwsConfigurationWithBucket() {
    $container = new ContainerBuilder();
    $container
      ->set('request_stack', new RequestStack());
    $container
      ->get('request_stack')
      ->push(Request::create('http://example.com/'));
    $configuration = [
      'key' => 'foo',
      'secret' => 'bar',
      'cname' => 'storage.example.com',
      'cname_is_bucket' => FALSE,
      'bucket' => 'my-bucket',
      'endpoint' => 'https://api.somewhere.tld',
    ];
    $plugin = S3::create($container, $configuration, '', '');
    $this
      ->assertSame('http://storage.example.com/my-bucket/foo%201.html', $plugin
      ->getExternalUrl('s3://foo 1.html'));
    $this
      ->assertSame('https://api.somewhere.tld', (string) $plugin
      ->getAdapter()
      ->getClient()
      ->getEndpoint());
  }
  public function testEmptyCnameDoesNotBreakConfiguration() {
    $configuration = [
      'cname' => NULL,
      'bucket' => 'my-bucket',
    ];
    $plugin = new S3($this
      ->createMock(S3ClientInterface::class), new Config($configuration));
    $this
      ->assertSame('http://s3.amazonaws.com/my-bucket/foo.html', $plugin
      ->getExternalUrl('s3://foo.html'));
  }
  public function testEnsure() {
    $client = $this
      ->prophesize(S3ClientInterface::class);
    $client
      ->doesBucketExist(Argument::type('string'))
      ->willReturn(TRUE);
    $plugin = new S3($client
      ->reveal(), new Config([
      'bucket' => 'example-bucket',
    ]));
    $this
      ->assertSame([], $plugin
      ->ensure());
    $client
      ->doesBucketExist(Argument::type('string'))
      ->willReturn(FALSE);
    $plugin = new S3($client
      ->reveal(), new Config([
      'bucket' => 'example-bucket',
    ]));
    $result = $plugin
      ->ensure();
    $this
      ->assertSame(1, count($result));
    $this
      ->assertSame(RfcLogLevel::ERROR, $result[0]['severity']);
  }
  public function testIamAuth() {
    $container = new ContainerBuilder();
    $container
      ->set('request_stack', new RequestStack());
    $container
      ->get('request_stack')
      ->push(Request::create('https://example.com/'));
    $container
      ->set('cache.default', new MemoryBackend('bin'));
    $configuration = [
      'bucket' => 'example-bucket',
    ];
    $plugin = S3::create($container, $configuration, '', '');
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
S3Test::test public function
S3Test::testCreate public function
S3Test::testCreateUsingNonAwsConfiguration public function
S3Test::testCreateUsingNonAwsConfigurationWithBucket public function
S3Test::testEmptyCnameDoesNotBreakConfiguration public function
S3Test::testEnsure public function
S3Test::testIamAuth public function
S3Test::testMergeConfiguration public function Tests merging defaults into configuration arrays.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.
UnitTestCase::setUp protected function 340