diff --git a/src/TaskLog.php b/src/TaskLog.php index 8dcddae..a763e08 100644 --- a/src/TaskLog.php +++ b/src/TaskLog.php @@ -41,7 +41,9 @@ class TaskLog public function __construct(array $data) { foreach ($data as $key => $value) { - if (property_exists($this, $key)) { + if ($key === 'output') { + $this->output = $this->setOutput($value); + } elseif (property_exists($this, $key)) { $this->{$key} = $value; } } @@ -68,4 +70,22 @@ public function __get(string $key) return $this->{$key}; } } + + /** + * Unify output to string. + * + * @param array|bool|int|string|null $value + */ + private function setOutput($value): ?string + { + if (is_string($value) || $value === null) { + return $value; + } + + if (is_array($value)) { + return implode(PHP_EOL, $value); + } + + return (string) $value; + } } diff --git a/tests/unit/TaskLogTest.php b/tests/unit/TaskLogTest.php index ebe8d1d..7abb63c 100644 --- a/tests/unit/TaskLogTest.php +++ b/tests/unit/TaskLogTest.php @@ -28,16 +28,19 @@ public static function provideDuration(): iterable '2021-01-21 12:00:00', '2021-01-21 12:00:00', '0.00', + ['first item', 'second item'], ], [ '2021-01-21 12:00:00', '2021-01-21 12:00:01', '1.00', + true, ], [ '2021-01-21 12:00:00', '2021-01-21 12:05:12', '312.00', + null, ], ]; } @@ -45,18 +48,18 @@ public static function provideDuration(): iterable /** * @dataProvider provideDuration * - * @param mixed $start - * @param mixed $end - * @param mixed $expected + * @param array|bool|int|string|null $output + * + * @throws Exception */ - public function testDuration($start, $end, $expected) + public function testDuration(string $start, string $end, string $expected, $output) { $start = new Time($start); $end = new Time($end); $log = new TaskLog([ 'task' => new Task('closure', static function () {}), - 'output' => '', + 'output' => $output, 'runStart' => $start, 'runEnd' => $end, 'error' => null,