From 418f00ed0e113475be5b9e072c606da7493f0234 Mon Sep 17 00:00:00 2001 From: Pierre PLAZANET Date: Tue, 27 Feb 2024 10:53:31 +0100 Subject: [PATCH] fix: typed property Gaufrette\Adapter\AwsS3:: must not be accessed before initialization (#716) --- spec/Gaufrette/Adapter/AwsS3Spec.php | 53 +++++++++++++++++++++++++--- src/Gaufrette/Adapter/AwsS3.php | 5 ++- 2 files changed, 51 insertions(+), 7 deletions(-) diff --git a/spec/Gaufrette/Adapter/AwsS3Spec.php b/spec/Gaufrette/Adapter/AwsS3Spec.php index 6fa971cc5..5cb07ed9d 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 decf8842c..4ad70fdb8 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