Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Catch notfound exception on deleting non existing file #122

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/GoogleStorageAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
50 changes: 50 additions & 0 deletions tests/GoogleStorageAdapterTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down