From 1d6f39e102fce0fff028bb57aba1e8f762a7ff8c Mon Sep 17 00:00:00 2001 From: PedroTroller Date: Mon, 26 Feb 2024 17:37:56 +0100 Subject: [PATCH] fix: typed property Gaufrette\Adapter\AwsS3:: must not be accessed before initialization --- spec/Gaufrette/Adapter/AwsS3Spec.php | 54 +++++++++++++++++++++++++--- src/Gaufrette/Adapter/AwsS3.php | 5 ++- 2 files changed, 52 insertions(+), 7 deletions(-) diff --git a/spec/Gaufrette/Adapter/AwsS3Spec.php b/spec/Gaufrette/Adapter/AwsS3Spec.php index 6fa971cc5..4a59a6411 100644 --- a/spec/Gaufrette/Adapter/AwsS3Spec.php +++ b/spec/Gaufrette/Adapter/AwsS3Spec.php @@ -2,8 +2,14 @@ namespace spec\Gaufrette\Adapter; +use ArrayIterator; +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 +21,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 185446fd7..f6d503c42 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