diff --git a/spec/Gaufrette/Adapter/AwsS3Spec.php b/spec/Gaufrette/Adapter/AwsS3Spec.php index 6fa971cc..5cb07ed9 100644 --- a/spec/Gaufrette/Adapter/AwsS3Spec.php +++ b/spec/Gaufrette/Adapter/AwsS3Spec.php @@ -2,8 +2,13 @@ namespace spec\Gaufrette\Adapter; +use Aws\Result; use Aws\S3\S3Client; +use Gaufrette\Adapter; +use Gaufrette\Adapter\AwsS3; +use Gaufrette\Adapter\MetadataSupporter; use Gaufrette\Adapter\MimeTypeProvider; +use Gaufrette\Adapter\SizeCalculator; use PhpSpec\ObjectBehavior; class AwsS3Spec extends ObjectBehavior @@ -15,26 +20,66 @@ function let(S3Client $service) function it_is_initializable() { - $this->shouldHaveType('Gaufrette\Adapter\AwsS3'); + $this->shouldHaveType(AwsS3::class); } function it_is_adapter() { - $this->shouldHaveType('Gaufrette\Adapter'); + $this->shouldHaveType(Adapter::class); } function it_supports_metadata() { - $this->shouldHaveType('Gaufrette\Adapter\MetadataSupporter'); + $this->shouldHaveType(MetadataSupporter::class); } function it_supports_sizecalculator() { - $this->shouldHaveType('Gaufrette\Adapter\SizeCalculator'); + $this->shouldHaveType(SizeCalculator::class); } function it_provides_mime_type() { $this->shouldHaveType(MimeTypeProvider::class); } + + function it_creates_bucket_if_it_does_not_exists(S3Client $service) + { + $this->beConstructedWith( + $service, + 'bucketName', + ['create' => true] + ); + + $service + ->doesBucketExist('bucketName') + ->shouldBeCalledTimes(1) + ->willReturn(false) + ; + + $service + ->getRegion() + ->shouldBeCalledTimes(1) + ->willReturn('eu-west-3') + ; + + $service + ->createBucket( + [ + 'Bucket' => 'bucketName', + 'LocationConstraint' => 'eu-west-3', + ] + ) + ->shouldBeCalledTimes(1) + ->willReturn(new Result) + ; + + $service + ->getIterator('ListObjects', ['Bucket' => 'bucketName']) + ->shouldBeCalledTimes(1) + ->willReturn([]) + ; + + $this->listKeys(); + } } diff --git a/src/Gaufrette/Adapter/AwsS3.php b/src/Gaufrette/Adapter/AwsS3.php index 185446fd..f6d503c4 100644 --- a/src/Gaufrette/Adapter/AwsS3.php +++ b/src/Gaufrette/Adapter/AwsS3.php @@ -16,7 +16,7 @@ class AwsS3 implements Adapter, MetadataSupporter, ListKeysAware, SizeCalculator protected S3Client $service; protected string $bucket; protected array $options; - protected bool $bucketExists; + protected bool $bucketExists = false; protected array $metadata = []; protected bool $detectContentType; @@ -278,9 +278,8 @@ protected function ensureBucketExists(): bool 'Bucket' => $this->bucket, 'LocationConstraint' => $this->service->getRegion(), ]); - $this->bucketExists = true; - return true; + return $this->bucketExists = true; } protected function getOptions(string $key, array $options = []): array