Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Latest
* [#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.
* [#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.
* [#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.
* [#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
-----
Expand Down
4 changes: 2 additions & 2 deletions docs/reference/tasks/input_file_reader_task.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
----------------
Expand Down
13 changes: 11 additions & 2 deletions src/Task/File/InputFileReaderTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,25 @@
*/
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<string, mixed>
*/
#[\Override]
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;
}
Expand Down
23 changes: 23 additions & 0 deletions tests/Task/File/InputFileReaderTaskTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,29 @@ public function testThrowsWhenInputFileIsNotReadable(): void
$this->read($task, $state, $filename);
}

/**
* @return iterable<string, array{mixed}>
*/
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();
Expand Down
Loading