Skip to content

Commit e37556c

Browse files
Merge pull request #250 from cleverage/242
#242 - Fix InputFileReaderTask: explicit exception when no file path is given as input
2 parents ad6a8b5 + b876add commit e37556c

4 files changed

Lines changed: 37 additions & 4 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Latest
1313
* [#241](https://github.com/cleverage/process-bundle/issues/241) Fix CsvWriterTask: a non-array input throws the documented `\UnexpectedValueException` instead of a `TypeError` when `headers` is `null`, and the CSV tasks release their file on `finalize()` so that the task can be executed again. Update documentation, add tests.
1414
* [#240](https://github.com/cleverage/process-bundle/issues/240) Fix FileMoverTask: with `autoincrement`, the suffix was computed on the whole path (a dot in a directory name was taken as the extension, making the move fail for a file without extension) and an existing numeric suffix of the file name was replaced (`report-2024.csv` became `report-1.csv`). The suffix is now appended to the file name only (`report-2024-1.csv`, `file-1`, `.env-1`). Update documentation, add tests.
1515
* [#243](https://github.com/cleverage/process-bundle/issues/243) Fix RecursivePropertySetterTransformer: a `\stdClass` item without the property was replaced by a copy in the output, so the input object was not modified; the property is now added to the item itself. Update documentation, add tests.
16+
* [#242](https://github.com/cleverage/process-bundle/issues/242) Fix InputFileReaderTask: an input that is not a non-empty string (e.g. `null`) throws an explicit `\UnexpectedValueException` (`No file path given as input`) instead of a PHP warning followed by a `TypeError`. Update documentation, add tests.
1617

1718
v5.1
1819
-----

‎docs/reference/tasks/input_file_reader_task.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ Task reference
1212
Accepted inputs
1313
---------------
1414

15-
`string`: path of the file to read. An `\UnexpectedValueException` is thrown if the file does not exist or is not
16-
readable.
15+
`string`: path of the file to read. An `\UnexpectedValueException` is thrown if the input is not a non-empty string
16+
(e.g. `null`, `''`), or if the file does not exist or is not readable.
1717

1818
Possible outputs
1919
----------------

‎src/Task/File/InputFileReaderTask.php‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,25 @@
2121
*/
2222
class InputFileReaderTask extends FileReaderTask
2323
{
24+
#[\Override]
25+
public function initialize(ProcessState $state): void
26+
{
27+
// Only validate the options: the file path comes from the input
28+
parent::getOptions($state);
29+
}
30+
2431
/**
2532
* @return array<string, mixed>
2633
*/
2734
#[\Override]
2835
protected function getOptions(ProcessState $state): array
2936
{
3037
$options = parent::getOptions($state);
31-
if (null !== $state->getInput()) {
32-
$options['filename'] = $state->getInput();
38+
$filename = $state->getInput();
39+
if (!\is_string($filename) || '' === $filename) {
40+
throw new \UnexpectedValueException('No file path given as input');
3341
}
42+
$options['filename'] = $filename;
3443

3544
return $options;
3645
}

‎tests/Task/File/InputFileReaderTaskTest.php‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,29 @@ public function testThrowsWhenInputFileIsNotReadable(): void
107107
$this->read($task, $state, $filename);
108108
}
109109

110+
/**
111+
* @return iterable<string, array{mixed}>
112+
*/
113+
public static function invalidInputProvider(): iterable
114+
{
115+
yield 'null' => [null];
116+
yield 'empty string' => [''];
117+
yield 'array' => [['a.txt']];
118+
}
119+
120+
#[\PHPUnit\Framework\Attributes\DataProvider('invalidInputProvider')]
121+
public function testThrowsWhenNoFilePathIsGivenAsInput(mixed $input): void
122+
{
123+
$task = new InputFileReaderTask();
124+
$state = $this->createState([]);
125+
$task->initialize($state);
126+
127+
$this->expectException(\UnexpectedValueException::class);
128+
$this->expectExceptionMessage('No file path given as input');
129+
130+
$this->read($task, $state, $input);
131+
}
132+
110133
public function testRejectsFilenameOption(): void
111134
{
112135
$task = new InputFileReaderTask();

0 commit comments

Comments
 (0)