PageRenderTime 41ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/Gaufrette/Functional/Adapter/AwsS3Test.php

http://github.com/knplabs/Gaufrette
PHP | 191 lines | 120 code | 32 blank | 39 comment | 8 complexity | 24fda799b0df0480949eb284d5c2c813 MD5 | raw file
  1. <?php
  2. namespace Gaufrette\Functional\Adapter;
  3. use Aws\S3\S3Client;
  4. use Gaufrette\Adapter\AwsS3;
  5. use Gaufrette\Filesystem;
  6. class AwsS3Test extends FunctionalTestCase
  7. {
  8. /** @var int */
  9. private static $SDK_VERSION;
  10. /** @var string */
  11. private $bucket;
  12. /** @var S3Client */
  13. private $client;
  14. protected function setUp()
  15. {
  16. $key = getenv('AWS_KEY');
  17. $secret = getenv('AWS_SECRET');
  18. if (empty($key) || empty($secret)) {
  19. $this->markTestSkipped('Either AWS_KEY and/or AWS_SECRET env variables are not defined.');
  20. }
  21. if (self::$SDK_VERSION === null) {
  22. self::$SDK_VERSION = method_exists(S3Client::class, 'getArguments') ? 3 : 2;
  23. }
  24. $this->bucket = uniqid(getenv('AWS_BUCKET'));
  25. if (self::$SDK_VERSION === 3) {
  26. // New way of instantiating S3Client for aws-sdk-php v3
  27. $this->client = new S3Client([
  28. 'region' => 'eu-west-1',
  29. 'version' => 'latest',
  30. 'credentials' => [
  31. 'key' => $key,
  32. 'secret' => $secret,
  33. ],
  34. ]);
  35. } else {
  36. $this->client = S3Client::factory([
  37. 'region' => 'eu-west-1',
  38. 'version' => '2006-03-01',
  39. 'key' => $key,
  40. 'secret' => $secret,
  41. ]);
  42. }
  43. $this->createFilesystem(['create' => true]);
  44. }
  45. protected function tearDown()
  46. {
  47. if ($this->client === null || !$this->client->doesBucketExist($this->bucket)) {
  48. return;
  49. }
  50. $result = $this->client->listObjects(['Bucket' => $this->bucket]);
  51. if (!$result->hasKey('Contents')) {
  52. $this->client->deleteBucket(['Bucket' => $this->bucket]);
  53. return;
  54. }
  55. foreach ($result->get('Contents') as $staleObject) {
  56. $this->client->deleteObject(['Bucket' => $this->bucket, 'Key' => $staleObject['Key']]);
  57. }
  58. $this->client->deleteBucket(['Bucket' => $this->bucket]);
  59. }
  60. private function createFilesystem(array $adapterOptions = [])
  61. {
  62. $this->filesystem = new Filesystem(new AwsS3($this->client, $this->bucket, $adapterOptions));
  63. }
  64. /**
  65. * @test
  66. * @expectedException \RuntimeException
  67. */
  68. public function shouldThrowExceptionIfBucketMissingAndNotCreating()
  69. {
  70. $this->createFilesystem();
  71. $this->filesystem->read('foo');
  72. }
  73. /**
  74. * @test
  75. */
  76. public function shouldWriteObjects()
  77. {
  78. $this->assertEquals(7, $this->filesystem->write('foo', 'testing'));
  79. }
  80. /**
  81. * @test
  82. */
  83. public function shouldCheckForObjectExistence()
  84. {
  85. $this->filesystem->write('foo', '');
  86. $this->assertTrue($this->filesystem->has('foo'));
  87. }
  88. /**
  89. * @test
  90. */
  91. public function shouldGetObjectUrls()
  92. {
  93. $this->assertNotEmpty($this->filesystem->getAdapter()->getUrl('foo'));
  94. }
  95. /**
  96. * @test
  97. */
  98. public function shouldCheckForObjectExistenceWithDirectory()
  99. {
  100. $this->createFilesystem(['directory' => 'bar', 'create' => true]);
  101. $this->filesystem->write('foo', '');
  102. $this->assertTrue($this->filesystem->has('foo'));
  103. }
  104. /**
  105. * @test
  106. */
  107. public function shouldGetObjectUrlsWithDirectory()
  108. {
  109. $this->createFilesystem(['directory' => 'bar']);
  110. $this->assertNotEmpty($this->filesystem->getAdapter()->getUrl('foo'));
  111. }
  112. /**
  113. * @test
  114. */
  115. public function shouldListKeysWithoutDirectory()
  116. {
  117. $this->assertEquals([], $this->filesystem->listKeys());
  118. $this->filesystem->write('test.txt', 'some content');
  119. $this->assertEquals(['test.txt'], $this->filesystem->listKeys());
  120. }
  121. /**
  122. * @test
  123. */
  124. public function shouldListKeysWithDirectory()
  125. {
  126. $this->createFilesystem(['create' => true, 'directory' => 'root/']);
  127. $this->filesystem->write('test.txt', 'some content');
  128. $this->assertEquals(['test.txt'], $this->filesystem->listKeys());
  129. $this->assertTrue($this->filesystem->has('test.txt'));
  130. }
  131. /**
  132. * @test
  133. */
  134. public function shouldGetKeysWithoutDirectory()
  135. {
  136. $this->filesystem->write('test.txt', 'some content');
  137. $this->assertEquals(['test.txt'], $this->filesystem->keys());
  138. }
  139. /**
  140. * @test
  141. */
  142. public function shouldGetKeysWithDirectory()
  143. {
  144. $this->createFilesystem(['create' => true, 'directory' => 'root/']);
  145. $this->filesystem->write('test.txt', 'some content');
  146. $this->assertEquals(['test.txt'], $this->filesystem->keys());
  147. }
  148. /**
  149. * @test
  150. */
  151. public function shouldUploadWithGivenContentType()
  152. {
  153. /** @var AwsS3 $adapter */
  154. $adapter = $this->filesystem->getAdapter();
  155. $adapter->setMetadata('foo', ['ContentType' => 'text/html']);
  156. $this->filesystem->write('foo', '<html></html>');
  157. $this->assertEquals('text/html', $this->filesystem->mimeType('foo'));
  158. }
  159. }