AwsS3Test.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. static private $SDK_VERSION;
  10. /** @var string */
  11. private $bucket;
  12. /** @var S3Client */
  13. private $client;
  14. public 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. public 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. * @expectedException \RuntimeException
  66. */
  67. public function testThrowsExceptionIfBucketMissingAndNotCreating()
  68. {
  69. $this->createFilesystem();
  70. $this->filesystem->read('foo');
  71. }
  72. public function testWritesObjects()
  73. {
  74. $this->assertEquals(7, $this->filesystem->write('foo', 'testing'));
  75. }
  76. public function testChecksForObjectExistence()
  77. {
  78. $this->filesystem->write('foo', '');
  79. $this->assertTrue($this->filesystem->has('foo'));
  80. }
  81. public function testGetsObjectUrls()
  82. {
  83. $this->assertNotEmpty($this->filesystem->getAdapter()->getUrl('foo'));
  84. }
  85. public function testChecksForObjectExistenceWithDirectory()
  86. {
  87. $this->createFilesystem(['directory' => 'bar', 'create' => true]);
  88. $this->filesystem->write('foo', '');
  89. $this->assertTrue($this->filesystem->has('foo'));
  90. }
  91. public function testGetsObjectUrlsWithDirectory()
  92. {
  93. $this->createFilesystem(['directory' => 'bar']);
  94. $this->assertNotEmpty($this->filesystem->getAdapter()->getUrl('foo'));
  95. }
  96. public function testListKeysWithoutDirectory()
  97. {
  98. $this->assertEquals([], $this->filesystem->listKeys());
  99. $this->filesystem->write('test.txt', 'some content');
  100. $this->assertEquals(['test.txt'], $this->filesystem->listKeys());
  101. }
  102. public function testListKeysWithDirectory()
  103. {
  104. $this->createFilesystem(['create' => true, 'directory' => 'root/']);
  105. $this->filesystem->write('test.txt', 'some content');
  106. $this->assertEquals(['test.txt'], $this->filesystem->listKeys());
  107. $this->assertTrue($this->filesystem->has('test.txt'));
  108. }
  109. public function testKeysWithoutDirectory()
  110. {
  111. $this->filesystem->write('test.txt', 'some content');
  112. $this->assertEquals(['test.txt'], $this->filesystem->keys());
  113. }
  114. public function testKeysWithDirectory()
  115. {
  116. $this->createFilesystem(['create' => true, 'directory' => 'root/']);
  117. $this->filesystem->write('test.txt', 'some content');
  118. $this->assertEquals(['test.txt'], $this->filesystem->keys());
  119. }
  120. public function testUploadWithGivenContentType()
  121. {
  122. /** @var AwsS3 $adapter */
  123. $adapter = $this->filesystem->getAdapter();
  124. $adapter->setMetadata('foo', ['ContentType' => 'text/html']);
  125. $this->filesystem->write('foo', '<html></html>');
  126. $this->assertEquals('text/html', $this->filesystem->mimeType('foo'));
  127. }
  128. }