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 @@ -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
-----
Expand Down
1 change: 1 addition & 0 deletions docs/reference/tasks/csv_writer_task.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
1 change: 1 addition & 0 deletions src/Task/File/Csv/AbstractCsvResourceTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public function finalize(ProcessState $state): void
{
if ($this->csv instanceof CsvResource) {
$this->csv->close();
$this->csv = null;
}
}

Expand Down
7 changes: 6 additions & 1 deletion src/Task/File/Csv/CsvWriterTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
24 changes: 24 additions & 0 deletions tests/Task/File/Csv/CsvWriterTaskTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Loading