Skip to content

Commit

Permalink
Fix exception handling in FlysystemStorage
Browse files Browse the repository at this point in the history
Related to dustin10#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).
  • Loading branch information
regis-martini committed Oct 30, 2024
1 parent 5eb6c3a commit 133d5bf
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/Storage/FlysystemStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]>
* @author Titouan Galopin <[email protected]>
Expand Down Expand Up @@ -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;
}
}
Expand Down
32 changes: 32 additions & 0 deletions tests/Storage/Flysystem/AbstractFlysystemStorageTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]>
Expand Down Expand Up @@ -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);
}
}

0 comments on commit 133d5bf

Please sign in to comment.