Skip to content

Commit 12e9867

Browse files
Merge pull request #76 from cleverage/75
#75 - FileFetchTask: explicit failure for an unknown storage
2 parents 4b4196c + 44da2a4 commit 12e9867

5 files changed

Lines changed: 60 additions & 10 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
Latest
22
------
33

4+
### Fixes
5+
* [#75](https://github.com/cleverage/flysystem-process-bundle/issues/75) `FileFetchTask` (`flysystem_file_fetch`): an unknown storage fails at initialization with an explicit message (`Unknown storage "…" (option "…")`, with the known storages) instead of `Service "…" not found`, When the task is executed after a failed initialization (process-bundle v5 goes on), it fails with an explicit message instead of `Typed property … must not be accessed before initialization`. Update documentation, add tests.
6+
47
v3.3
58
------
69

‎docs/reference/tasks/file_fetch_task.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,9 @@ Notes
9898
-----
9999

100100
* Both storages are resolved when the task is initialized, at the start of the process: an unknown storage name makes
101-
the process fail before any task is executed.
101+
the initialization fail (`Unknown storage "…"`, logged at the `critical` level). With process-bundle v5, the process
102+
goes on and fails when the task is first executed (deprecated, the process will fail before executing any task in
103+
process-bundle v6).
102104
* `file_pattern` is only tested on the files located at the root of `source_filesystem` (the listing is not
103105
recursive and directories are ignored). The pattern is tested on the path relative to the storage root: to target
104106
a sub-directory, configure a dedicated storage whose root is this directory.

‎src/Task/FileFetchTask.php‎

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use CleverAge\ProcessBundle\Model\ProcessState;
2020
use League\Flysystem\FilesystemException;
2121
use League\Flysystem\FilesystemOperator;
22+
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
2223
use Symfony\Component\DependencyInjection\ServiceLocator;
2324
use Symfony\Component\OptionsResolver\OptionsResolver;
2425

@@ -50,19 +51,16 @@ public function __construct(protected readonly ServiceLocator $storages)
5051

5152
/**
5253
* @throws \InvalidArgumentException
54+
* @throws UnexpectedValueException
5355
*/
5456
#[\Override]
5557
public function initialize(ProcessState $state): void
5658
{
5759
// Configure options
5860
parent::initialize($state);
5961

60-
/** @var string $sourceFilesystemOption */
61-
$sourceFilesystemOption = $this->getOption($state, 'source_filesystem');
62-
$this->sourceFS = $this->storages->get($sourceFilesystemOption);
63-
/** @var string $destinationFilesystemOption */
64-
$destinationFilesystemOption = $this->getOption($state, 'destination_filesystem');
65-
$this->destinationFS = $this->storages->get($destinationFilesystemOption);
62+
$this->sourceFS = $this->getStorage($state, 'source_filesystem');
63+
$this->destinationFS = $this->getStorage($state, 'destination_filesystem');
6664
}
6765

6866
/**
@@ -72,6 +70,11 @@ public function initialize(ProcessState $state): void
7270
*/
7371
public function execute(ProcessState $state): void
7472
{
73+
// With process-bundle v5, a task is executed even if its initialization has failed (the error is logged)
74+
if (!isset($this->sourceFS, $this->destinationFS)) {
75+
throw new UnexpectedValueException('The storages of the task are not available: its initialization has failed, see the previous error');
76+
}
77+
7578
// The files are listed once per input, when its iteration starts
7679
$this->matchingFiles ??= $this->findMatchingFiles($state);
7780

@@ -104,6 +107,20 @@ public function next(ProcessState $state): bool
104107
return false;
105108
}
106109

110+
/**
111+
* @throws UnexpectedValueException
112+
*/
113+
protected function getStorage(ProcessState $state, string $option): FilesystemOperator
114+
{
115+
/** @var string $storage */
116+
$storage = $this->getOption($state, $option);
117+
try {
118+
return $this->storages->get($storage);
119+
} catch (ServiceNotFoundException $e) {
120+
throw new UnexpectedValueException(\sprintf('Unknown storage "%s" (option "%s"), known storages: "%s"', $storage, $option, implode('", "', array_keys($this->storages->getProvidedServices()))), 0, $e);
121+
}
122+
}
123+
107124
/**
108125
* @return list<string>
109126
*

‎tests/Task/FileFetchTaskStorageTest.php‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
namespace CleverAge\FlysystemProcessBundle\Tests\Task;
1515

16+
use CleverAge\FlysystemProcessBundle\Exception\UnexpectedValueException;
1617
use CleverAge\FlysystemProcessBundle\Task\FileFetchTask;
1718
use CleverAge\ProcessBundle\Model\ProcessState;
1819
use PHPUnit\Framework\Attributes\CoversClass;
@@ -143,7 +144,8 @@ public function testNextBeforeExecute(): void
143144

144145
public function testUnknownStorage(): void
145146
{
146-
$this->expectException(\Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException::class);
147+
$this->expectException(UnexpectedValueException::class);
148+
$this->expectExceptionMessage('Unknown storage "unknown" (option "source_filesystem")');
147149
$this->createTask(['source_filesystem' => 'unknown']);
148150
}
149151

‎tests/Task/FileFetchTaskTest.php‎

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
namespace CleverAge\FlysystemProcessBundle\Tests\Task;
1515

16+
use CleverAge\FlysystemProcessBundle\Exception\UnexpectedValueException;
1617
use CleverAge\FlysystemProcessBundle\Task\FileFetchTask;
1718
use CleverAge\ProcessBundle\Model\ProcessState;
1819
use League\Flysystem\FilesystemOperator;
@@ -64,6 +65,31 @@ public function testWriteFailureKeepsSourceAndFails(): void
6465
$task->execute($state);
6566
}
6667

68+
public function testUnknownStorageFailsAtInitialization(): void
69+
{
70+
$task = $this->createTask();
71+
72+
$this->expectException(UnexpectedValueException::class);
73+
$this->expectExceptionMessage('Unknown storage "unknown" (option "destination_filesystem"), known storages: "source", "destination"');
74+
$task->initialize($this->createState('unknown'));
75+
}
76+
77+
public function testTaskFailsExplicitlyWhenExecutedAfterAFailedInitialization(): void
78+
{
79+
$state = $this->createState('unknown');
80+
$task = $this->createTask();
81+
try {
82+
$task->initialize($state);
83+
self::fail('The initialization should have failed');
84+
} catch (UnexpectedValueException) {
85+
}
86+
87+
$this->destination->expects($this->never())->method('writeStream');
88+
$this->expectException(UnexpectedValueException::class);
89+
$this->expectExceptionMessage('The storages of the task are not available: its initialization has failed, see the previous error');
90+
$task->execute($state);
91+
}
92+
6793
private function createTask(): FileFetchTask
6894
{
6995
/** @var ServiceLocator<FilesystemOperator> $storages */
@@ -75,12 +101,12 @@ private function createTask(): FileFetchTask
75101
return new FileFetchTask($storages);
76102
}
77103

78-
private function createState(): ProcessState&MockObject
104+
private function createState(string $destination = 'destination'): ProcessState&MockObject
79105
{
80106
$state = $this->createMock(ProcessState::class);
81107
$state->method('getContextualizedOptions')->willReturn([
82108
'source_filesystem' => 'source',
83-
'destination_filesystem' => 'destination',
109+
'destination_filesystem' => $destination,
84110
'remove_source' => true,
85111
]);
86112
$state->method('getInput')->willReturn('file.txt');

0 commit comments

Comments
 (0)