From ac433b7dde9e777ac818ee41e618171207fe20db Mon Sep 17 00:00:00 2001 From: Nicolas Joubert Date: Fri, 9 Oct 2026 10:45:45 +0200 Subject: [PATCH] fix(task) #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. Co-Authored-By: Claude Opus 5.5 (1M context) --- CHANGELOG.md | 1 + docs/reference/tasks/csv_writer_task.md | 1 + src/Task/File/Csv/AbstractCsvResourceTask.php | 1 + src/Task/File/Csv/CsvWriterTask.php | 7 +++++- tests/Task/File/Csv/CsvWriterTaskTest.php | 24 +++++++++++++++++++ 5 files changed, 33 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3828793f..359fb330 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ Latest * [#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. * [#143](https://github.com/cleverage/process-bundle/issues/143) Fix level 8 edge cases failing with a `TypeError` or an `Error`: `CsvResource` rejects a blank header line and checks `ftell()`, XpathEvaluatorTransformer throws an explicit `\UnexpectedValueException` on an invalid query (with the libxml messages), a namespace node to unwrap or a node without document, SlugifyTransformer on a value that cannot be transliterated, `ProcessState::getContext()` returns `[]` until the context is defined, GenericTransformer and TransformerTrait throw a `\LogicException` when not initialized, IterableBatchTask and JsonStreamReaderTask can be flushed / iterated before their first execution, RegistryCompilerPass checks its configuration, SubprocessInstance checks the PHP executable. Update documentation, add tests. * [#239](https://github.com/cleverage/process-bundle/issues/239) Fix PropertyGetterTask: catch any `\Throwable`, so that the `TypeError` of the property accessor on a scalar input is set on the state (with the `property` error context) and handled according to the task `error_strategy` instead of escaping it. Update documentation, add tests. +* [#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. v5.1 ----- diff --git a/docs/reference/tasks/csv_writer_task.md b/docs/reference/tasks/csv_writer_task.md index 22d1d762..22c08eb6 100644 --- a/docs/reference/tasks/csv_writer_task.md +++ b/docs/reference/tasks/csv_writer_task.md @@ -76,5 +76,6 @@ Notes * `{date}` is replaced by `Ymd`, `{date_time}` by `Ymd_His`, `{timestamp}` by the Unix timestamp and `{unique_token}` by a `uniqid()` value. * The parent directory of the file is created if needed. +* An input that is not an `array` throws an `\UnexpectedValueException`. * Each input must contain every header key (extra keys are not allowed: the number of columns must match the number of headers), otherwise an `\UnexpectedValueException` is thrown. Columns are written in the headers order. diff --git a/src/Task/File/Csv/AbstractCsvResourceTask.php b/src/Task/File/Csv/AbstractCsvResourceTask.php index a4c8be16..24766052 100644 --- a/src/Task/File/Csv/AbstractCsvResourceTask.php +++ b/src/Task/File/Csv/AbstractCsvResourceTask.php @@ -30,6 +30,7 @@ public function finalize(ProcessState $state): void { if ($this->csv instanceof CsvResource) { $this->csv->close(); + $this->csv = null; } } diff --git a/src/Task/File/Csv/CsvWriterTask.php b/src/Task/File/Csv/CsvWriterTask.php index 691bde92..8c150a12 100644 --- a/src/Task/File/Csv/CsvWriterTask.php +++ b/src/Task/File/Csv/CsvWriterTask.php @@ -96,8 +96,13 @@ protected function getHeaders(ProcessState $state, array $options): ?array { $headers = $options['headers']; if (null === $headers) { + $input = $state->getInput(); + if (!\is_array($input)) { + throw new \UnexpectedValueException('Input value is not an array'); + } + // Keys of a list are integers: the CSV output is the same with their string value - return array_map(strval(...), array_keys($state->getInput())); + return array_map(strval(...), array_keys($input)); } return $headers; diff --git a/tests/Task/File/Csv/CsvWriterTaskTest.php b/tests/Task/File/Csv/CsvWriterTaskTest.php index 11d02383..39496d75 100644 --- a/tests/Task/File/Csv/CsvWriterTaskTest.php +++ b/tests/Task/File/Csv/CsvWriterTaskTest.php @@ -212,6 +212,30 @@ public function testThrowsWhenInputIsNotAnArrayWithStaticHeaders(): void $task->execute($state); } + public function testThrowsWhenInputIsNotAnArrayWithoutStaticHeaders(): void + { + $task = new CsvWriterTask(); + $state = $this->createState(['file_path' => $this->tmpDir.'/out.csv']); + $task->initialize($state); + $state->setInput('not an array'); + + $this->expectException(\UnexpectedValueException::class); + $this->expectExceptionMessage('Input value is not an array'); + $task->execute($state); + } + + public function testCanWriteAgainAfterFinalize(): void + { + $filePath = $this->tmpDir.'/out.csv'; + $task = new CsvWriterTask(); + $state = $this->createState(['file_path' => $filePath]); + + $this->write($task, $state, [['id' => 1]]); + $this->write($task, $state, [['id' => 2]]); + + self::assertSame("id\n2\n", file_get_contents($filePath)); + } + public function testThrowsWhenInputHasMissingColumn(): void { $task = new CsvWriterTask();