From d6918d05566be15a98f7ae479e057ce92a5b1af9 Mon Sep 17 00:00:00 2001 From: Nicolas Joubert Date: Fri, 9 Oct 2026 10:48:22 +0200 Subject: [PATCH] fix(task) #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. Co-Authored-By: Claude Opus 5.5 (1M context) --- CHANGELOG.md | 1 + .../reference/tasks/input_file_reader_task.md | 4 ++-- src/Task/File/InputFileReaderTask.php | 13 +++++++++-- tests/Task/File/InputFileReaderTaskTest.php | 23 +++++++++++++++++++ 4 files changed, 37 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bdf7f535..d8fe8950 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ Latest ## Fixes * [#143](https://github.com/cleverage/process-bundle/issues/143) Fix InputIteratorTask: an `\IteratorAggregate` input whose `getIterator()` does not return an `\Iterator` (e.g. another `\IteratorAggregate`) is iterated instead of failing with a `TypeError`. Update documentation, add tests. +* [#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. v5.1 ----- diff --git a/docs/reference/tasks/input_file_reader_task.md b/docs/reference/tasks/input_file_reader_task.md index 39e5e06b..ac9413f9 100644 --- a/docs/reference/tasks/input_file_reader_task.md +++ b/docs/reference/tasks/input_file_reader_task.md @@ -12,8 +12,8 @@ Task reference Accepted inputs --------------- -`string`: path of the file to read. An `\UnexpectedValueException` is thrown if the file does not exist or is not -readable. +`string`: path of the file to read. An `\UnexpectedValueException` is thrown if the input is not a non-empty string +(e.g. `null`, `''`), or if the file does not exist or is not readable. Possible outputs ---------------- diff --git a/src/Task/File/InputFileReaderTask.php b/src/Task/File/InputFileReaderTask.php index b2b36178..9d002370 100644 --- a/src/Task/File/InputFileReaderTask.php +++ b/src/Task/File/InputFileReaderTask.php @@ -21,6 +21,13 @@ */ class InputFileReaderTask extends FileReaderTask { + #[\Override] + public function initialize(ProcessState $state): void + { + // Only validate the options: the file path comes from the input + parent::getOptions($state); + } + /** * @return array */ @@ -28,9 +35,11 @@ class InputFileReaderTask extends FileReaderTask protected function getOptions(ProcessState $state): array { $options = parent::getOptions($state); - if (null !== $state->getInput()) { - $options['filename'] = $state->getInput(); + $filename = $state->getInput(); + if (!\is_string($filename) || '' === $filename) { + throw new \UnexpectedValueException('No file path given as input'); } + $options['filename'] = $filename; return $options; } diff --git a/tests/Task/File/InputFileReaderTaskTest.php b/tests/Task/File/InputFileReaderTaskTest.php index 96551daf..bcbd10da 100644 --- a/tests/Task/File/InputFileReaderTaskTest.php +++ b/tests/Task/File/InputFileReaderTaskTest.php @@ -107,6 +107,29 @@ public function testThrowsWhenInputFileIsNotReadable(): void $this->read($task, $state, $filename); } + /** + * @return iterable + */ + public static function invalidInputProvider(): iterable + { + yield 'null' => [null]; + yield 'empty string' => ['']; + yield 'array' => [['a.txt']]; + } + + #[\PHPUnit\Framework\Attributes\DataProvider('invalidInputProvider')] + public function testThrowsWhenNoFilePathIsGivenAsInput(mixed $input): void + { + $task = new InputFileReaderTask(); + $state = $this->createState([]); + $task->initialize($state); + + $this->expectException(\UnexpectedValueException::class); + $this->expectExceptionMessage('No file path given as input'); + + $this->read($task, $state, $input); + } + public function testRejectsFilenameOption(): void { $task = new InputFileReaderTask();