From 8e3478105b6eacf9e26a6b04c7d29820ade93cf5 Mon Sep 17 00:00:00 2001 From: Regis Martini Date: Thu, 31 Oct 2024 03:28:13 -0300 Subject: [PATCH] Fix exception handling in FlysystemStorage (#1473) * Fix exception handling in FlysystemStorage Related to #1445 Add handling for UndefinedMethodError exception in FlysystemStorage. * Catch both `FilesystemException` and `UndefinedMethodError` exceptions in the `publicUrl` method of `src/Storage/FlysystemStorage.php`. * Add a test case in `tests/Storage/Flysystem/AbstractFlysystemStorageTestCase.php` to check for `UndefinedMethodError` exception handling in the `resolveUri` method. * Ensure the test case covers both exceptions to maintain compatibility with different Flysystem versions. --- src/Storage/FlysystemStorage.php | 3 +- .../AbstractFlysystemStorageTestCase.php | 33 +++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/src/Storage/FlysystemStorage.php b/src/Storage/FlysystemStorage.php index 6175071a..a21ef96e 100644 --- a/src/Storage/FlysystemStorage.php +++ b/src/Storage/FlysystemStorage.php @@ -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; @@ -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; } } diff --git a/tests/Storage/Flysystem/AbstractFlysystemStorageTestCase.php b/tests/Storage/Flysystem/AbstractFlysystemStorageTestCase.php index 1950757d..fc2f17a8 100644 --- a/tests/Storage/Flysystem/AbstractFlysystemStorageTestCase.php +++ b/tests/Storage/Flysystem/AbstractFlysystemStorageTestCase.php @@ -2,6 +2,7 @@ namespace Vich\UploaderBundle\Tests\Storage\Flysystem; +use Error; use League\Flysystem\Filesystem; use League\Flysystem\FilesystemAdapter; use League\Flysystem\FilesystemOperator; @@ -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 @@ -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); + } }