From 6995be889962f4c49c75ce3b055b45f020baf25f Mon Sep 17 00:00:00 2001 From: Stefan van Essen Date: Wed, 6 Oct 2021 14:15:58 +0200 Subject: [PATCH 1/2] Add test that demonstrates the uncaught exception --- tests/GoogleStorageAdapterTests.php | 50 +++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/tests/GoogleStorageAdapterTests.php b/tests/GoogleStorageAdapterTests.php index 450dc51..9aa2337 100755 --- a/tests/GoogleStorageAdapterTests.php +++ b/tests/GoogleStorageAdapterTests.php @@ -2,6 +2,7 @@ namespace Tests; +use Google\Cloud\Core\Exception\NotFoundException; use Google\Cloud\Storage\Acl; use Google\Cloud\Storage\Bucket; use Google\Cloud\Storage\StorageClient; @@ -391,6 +392,55 @@ public function testDeleteDir() $adapter->deleteDir('dir_name'); } + public function testDeleteDirThatDoesNotExist() + { + $storageClient = Mockery::mock(StorageClient::class); + $bucket = Mockery::mock(Bucket::class); + + $storageObject = Mockery::mock(StorageObject::class); + $storageObject->shouldReceive('delete') + ->times(2); + $storageObject->shouldReceive('delete') + ->once() + ->andThrow(new NotFoundException('Directory not found')); + + $storageObject->shouldReceive('name') + ->once() + ->andReturn('prefix/dir_name/directory1/file1.txt'); + $storageObject->shouldReceive('info') + ->once() + ->andReturn([ + 'updated' => '2016-09-26T14:44:42+00:00', + 'contentType' => 'text/plain', + 'size' => 5, + ]); + + $bucket->shouldReceive('object') + ->with('prefix/dir_name/directory1/file1.txt') + ->once() + ->andReturn($storageObject); + + $bucket->shouldReceive('object') + ->with('prefix/dir_name/directory1/') + ->once() + ->andReturn($storageObject); + + $bucket->shouldReceive('object') + ->with('prefix/dir_name/') + ->once() + ->andReturn($storageObject); + + $bucket->shouldReceive('objects') + ->with([ + 'prefix' => 'prefix/dir_name/' + ])->once() + ->andReturn([$storageObject]); + + $adapter = new GoogleStorageAdapter($storageClient, $bucket, 'prefix'); + + $adapter->deleteDir('dir_name'); + } + public function testDeleteDirWithTrailingSlash() { $storageClient = Mockery::mock(StorageClient::class); From ec2f343a1adc8c6e525cadbe9c36c9cbf4188468 Mon Sep 17 00:00:00 2001 From: Stefan van Essen Date: Wed, 6 Oct 2021 15:27:02 +0200 Subject: [PATCH 2/2] Implement the fix for the uncaught NotFoundException --- src/GoogleStorageAdapter.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/GoogleStorageAdapter.php b/src/GoogleStorageAdapter.php index d5e6c9d..7c5a6f8 100755 --- a/src/GoogleStorageAdapter.php +++ b/src/GoogleStorageAdapter.php @@ -273,7 +273,12 @@ public function deleteDir($dirname) // Execute deletion for each object. foreach ($filtered_objects as $object) { - $this->delete($object['path']); + try { + $this->delete($object['path']); + } catch (NotFoundException $e) { + // If the file does not exist, the Google Cloud PHP API throws an exception. + // This exception should be caught here and ignored. + } } return true;