From 133d5bf77c958eb6fef6678c4c3cb9ddb280b8a7 Mon Sep 17 00:00:00 2001 From: Regis Martini Date: Wed, 30 Oct 2024 17:41:29 -0300 Subject: [PATCH 1/5] 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. --- For more details, open the [Copilot Workspace session](https://copilot-workspace.githubnext.com/dustin10/VichUploaderBundle/issues/1445?shareId=XXXX-XXXX-XXXX-XXXX). --- src/Storage/FlysystemStorage.php | 4 ++- .../AbstractFlysystemStorageTestCase.php | 32 +++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/src/Storage/FlysystemStorage.php b/src/Storage/FlysystemStorage.php index 6175071a..b524b562 100644 --- a/src/Storage/FlysystemStorage.php +++ b/src/Storage/FlysystemStorage.php @@ -7,10 +7,12 @@ 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; + /** * @author Markus Bachmann * @author Titouan Galopin @@ -97,7 +99,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..af0a89e6 100644 --- a/tests/Storage/Flysystem/AbstractFlysystemStorageTestCase.php +++ b/tests/Storage/Flysystem/AbstractFlysystemStorageTestCase.php @@ -12,6 +12,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 +210,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'))); + + $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); + } } From fce28c3cb51ec9e5694619380e3220d1777de41a Mon Sep 17 00:00:00 2001 From: Regis Martini Date: Wed, 30 Oct 2024 21:42:15 +0100 Subject: [PATCH 2/5] Update FlysystemStorage.php --- src/Storage/FlysystemStorage.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Storage/FlysystemStorage.php b/src/Storage/FlysystemStorage.php index b524b562..3ce7ffe4 100644 --- a/src/Storage/FlysystemStorage.php +++ b/src/Storage/FlysystemStorage.php @@ -12,7 +12,6 @@ use Vich\UploaderBundle\Mapping\PropertyMapping; use Vich\UploaderBundle\Mapping\PropertyMappingFactory; - /** * @author Markus Bachmann * @author Titouan Galopin From b15bb9e71f9ec94d1b8d489551d8bb33c9d844e5 Mon Sep 17 00:00:00 2001 From: Regis Martini Date: Wed, 30 Oct 2024 21:43:55 +0100 Subject: [PATCH 3/5] Adjusting style --- src/Storage/FlysystemStorage.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Storage/FlysystemStorage.php b/src/Storage/FlysystemStorage.php index 3ce7ffe4..a21ef96e 100644 --- a/src/Storage/FlysystemStorage.php +++ b/src/Storage/FlysystemStorage.php @@ -98,7 +98,7 @@ public function resolveUri(object|array $obj, ?string $fieldName = null, ?string try { return $fs->publicUrl($path); - } catch (FilesystemException | UndefinedMethodError) { + } catch (FilesystemException|UndefinedMethodError) { return $mapping->getUriPrefix().'/'.$path; } } From e89c8bb6c4d6eaae668c9f9fdb7a9eac6e495d62 Mon Sep 17 00:00:00 2001 From: Regis Martini Date: Wed, 30 Oct 2024 22:23:31 +0100 Subject: [PATCH 4/5] Updated constructor call that requires 2 parameters --- tests/Storage/Flysystem/AbstractFlysystemStorageTestCase.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/Storage/Flysystem/AbstractFlysystemStorageTestCase.php b/tests/Storage/Flysystem/AbstractFlysystemStorageTestCase.php index af0a89e6..daef9e77 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; @@ -219,7 +220,7 @@ public function testResolveUriHandlesUndefinedMethodError(): void ->expects(self::once()) ->method('publicUrl') ->with('file.txt') - ->will($this->throwException(new UndefinedMethodError('Undefined method'))); + ->will($this->throwException(new UndefinedMethodError('Undefined method'), new Error('An error occurred'))); $this->mapping ->expects(self::once()) From 88c6db05f72539c796761a29d41fa6d64b3f5d34 Mon Sep 17 00:00:00 2001 From: Regis Martini Date: Wed, 30 Oct 2024 23:02:00 +0100 Subject: [PATCH 5/5] ::Facepalm:: updating --- tests/Storage/Flysystem/AbstractFlysystemStorageTestCase.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Storage/Flysystem/AbstractFlysystemStorageTestCase.php b/tests/Storage/Flysystem/AbstractFlysystemStorageTestCase.php index daef9e77..fc2f17a8 100644 --- a/tests/Storage/Flysystem/AbstractFlysystemStorageTestCase.php +++ b/tests/Storage/Flysystem/AbstractFlysystemStorageTestCase.php @@ -220,7 +220,7 @@ public function testResolveUriHandlesUndefinedMethodError(): void ->expects(self::once()) ->method('publicUrl') ->with('file.txt') - ->will($this->throwException(new UndefinedMethodError('Undefined method'), new Error('An error occurred'))); + ->will($this->throwException(new UndefinedMethodError('Undefined method', new Error('An error occurred')))); $this->mapping ->expects(self::once())