Skip to content

Commit 08f156d

Browse files
Merge pull request #22 from cleverage/18
fix(task) #18 ZipTask and UnzipTask: explicit error on a non-array input
2 parents 73eddc5 + cd9b060 commit 08f156d

7 files changed

Lines changed: 33 additions & 2 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ Latest
55
* [#15](https://github.com/cleverage/archive-process-bundle/issues/15) Fix ZipTask and UnzipTask: resolve the options on every execution, so that each input is used (the options of the first input were reused for the following ones). Update documentation, add tests.
66
* [#16](https://github.com/cleverage/archive-process-bundle/issues/16) Fix ZipTask: only remove the leading `files_base_path` from the file paths (every occurrence was removed), and read the files at the given path when `files_base_path` is empty (relative paths were resolved from the filesystem root). Update documentation, add tests.
77
* [#17](https://github.com/cleverage/archive-process-bundle/issues/17) Fix ZipTask and UnzipTask: throw a `\RuntimeException` when the archive cannot be written (ZipTask) or extracted (UnzipTask), instead of outputting the path anyway; add the `ZipArchive` error code to the UnzipTask open failure message. Update documentation, add tests.
8+
* [#18](https://github.com/cleverage/archive-process-bundle/issues/18) Fix ZipTask and UnzipTask: throw an explicit `\UnexpectedValueException` on a non-array input (e.g. a `string` path) instead of an unrelated warning or `TypeError`. Update documentation, add tests.
89

910
v2.1
1011
------

‎docs/reference/tasks/unzip_task.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ configured ones. It may only contain the `filename` and/or `destination` keys (a
1717
`UndefinedOptionsException`). A `null` input is handled as an empty array, so the options come from the task
1818
configuration only.
1919

20-
Any other input type (e.g. a `string` file path) is not supported: convert it into an array first, for instance with a
20+
Any other input type (e.g. a `string` file path) is not supported and throws an `\UnexpectedValueException`: convert it into an array first, for instance with a
2121
[TransformerTask](https://github.com/cleverage/process-bundle/blob/main/docs/reference/tasks/transformer_task.md) and
2222
the [wrapper](https://github.com/cleverage/process-bundle/blob/main/docs/reference/transformers/wrapper_transformer.md)
2323
transformer (`wrapper_key: filename`).

‎docs/reference/tasks/zip_task.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ configured ones. It may only contain the `filename`, `files` and/or `files_base_
1717
`UndefinedOptionsException`). A `null` input is handled as an empty array, so the options come from the task
1818
configuration only.
1919

20-
Any other input type (e.g. a `string` file path) is not supported: convert it into an array first, for instance with a
20+
Any other input type (e.g. a `string` file path) is not supported and throws an `\UnexpectedValueException`: convert it into an array first, for instance with a
2121
[TransformerTask](https://github.com/cleverage/process-bundle/blob/main/docs/reference/tasks/transformer_task.md) and
2222
the [wrapper](https://github.com/cleverage/process-bundle/blob/main/docs/reference/transformers/wrapper_transformer.md)
2323
transformer (`wrapper_key: files`).

‎src/Task/UnzipTask.php‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ public function execute(ProcessState $state): void
2727
if (null === $state->getInput()) {
2828
$state->setInput([]);
2929
}
30+
if (!\is_array($state->getInput())) {
31+
throw new \UnexpectedValueException(\sprintf('UnzipTask expects an array or null input, %s given', get_debug_type($state->getInput())));
32+
}
3033
/**
3134
* @var array{filename: string, destination: string} $options
3235
*/

‎src/Task/ZipTask.php‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ public function execute(ProcessState $state): void
2727
if (null === $state->getInput()) {
2828
$state->setInput([]);
2929
}
30+
if (!\is_array($state->getInput())) {
31+
throw new \UnexpectedValueException(\sprintf('ZipTask expects an array or null input, %s given', get_debug_type($state->getInput())));
32+
}
3033
/**
3134
* @var array{filename: string, files: array<string>|string, files_base_path: string} $options
3235
*/

‎tests/Task/UnzipTaskTest.php‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,18 @@ public function testOpenFailureGivesTheErrorCode(): void
9999
}
100100
}
101101

102+
public function testNonArrayInput(): void
103+
{
104+
[$task, $state] = $this->createTask([]);
105+
106+
try {
107+
$this->execute($task, $state, 'file.zip');
108+
self::fail('An \UnexpectedValueException should have been thrown');
109+
} catch (\UnexpectedValueException $e) {
110+
self::assertSame('UnzipTask expects an array or null input, string given', $e->getMessage());
111+
}
112+
}
113+
102114
/**
103115
* @param array<string, mixed> $options
104116
*

‎tests/Task/ZipTaskTest.php‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,18 @@ public function testWriteFailure(): void
116116
self::assertNull($state->getOutput());
117117
}
118118

119+
public function testNonArrayInput(): void
120+
{
121+
[$task, $state] = $this->createTask([]);
122+
123+
try {
124+
$this->execute($task, $state, 'file.zip');
125+
self::fail('An \UnexpectedValueException should have been thrown');
126+
} catch (\UnexpectedValueException $e) {
127+
self::assertSame('ZipTask expects an array or null input, string given', $e->getMessage());
128+
}
129+
}
130+
119131
/**
120132
* @param array<string, mixed> $options
121133
*

0 commit comments

Comments
 (0)