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

Fix exception handling in FlysystemStorage #1473

Merged
merged 5 commits into from
Oct 31, 2024
Merged
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
3 changes: 2 additions & 1 deletion src/Storage/FlysystemStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use League\Flysystem\MountManager;
use Psr\Container\ContainerInterface;
use Symfony\Component\HttpFoundation\File\Exception\CannotWriteFileException;
use Symfony\Component\ErrorHandler\Error\UndefinedMethodError;
use Symfony\Component\HttpFoundation\File\File;
use Vich\UploaderBundle\Mapping\PropertyMapping;
use Vich\UploaderBundle\Mapping\PropertyMappingFactory;
Expand Down Expand Up @@ -97,7 +98,7 @@ public function resolveUri(object|array $obj, ?string $fieldName = null, ?string

try {
return $fs->publicUrl($path);
} catch (FilesystemException) {
} catch (FilesystemException|UndefinedMethodError) {
return $mapping->getUriPrefix().'/'.$path;
}
}
Expand Down
33 changes: 33 additions & 0 deletions tests/Storage/Flysystem/AbstractFlysystemStorageTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Vich\UploaderBundle\Tests\Storage\Flysystem;

use Error;
use League\Flysystem\Filesystem;
use League\Flysystem\FilesystemAdapter;
use League\Flysystem\FilesystemOperator;
Expand All @@ -12,6 +13,7 @@
use Vich\UploaderBundle\Storage\FlysystemStorage;
use Vich\UploaderBundle\Storage\StorageInterface;
use Vich\UploaderBundle\Tests\Storage\StorageTestCase;
use Symfony\Component\ErrorHandler\Error\UndefinedMethodError;

/**
* @author Markus Bachmann <[email protected]>
Expand Down Expand Up @@ -209,4 +211,35 @@ public function testResolveUriThroughFlysystem(): void

self::assertEquals('example.com/file.txt', $path);
}

public function testResolveUriHandlesUndefinedMethodError(): void
{
$this->useFlysystemToResolveUri = true;

$this->filesystem
->expects(self::once())
->method('publicUrl')
->with('file.txt')
->will($this->throwException(new UndefinedMethodError('Undefined method', new Error('An error occurred'))));

$this->mapping
->expects(self::once())
->method('getFileName')
->willReturn('file.txt');

$this->mapping
->expects(self::once())
->method('getUriPrefix')
->willReturn('/uploads');

$this->factory
->expects(self::exactly(2))
->method('fromField')
->with($this->object, 'file_field')
->willReturn($this->mapping);

$path = $this->getStorage()->resolveUri($this->object, 'file_field');

self::assertEquals('/uploads/file.txt', $path);
}
}
Loading