From f283e9bd30fe2fb747bde936553d0c2983817aef Mon Sep 17 00:00:00 2001 From: michalsn Date: Sun, 19 Jan 2025 09:28:40 +0100 Subject: [PATCH 1/4] fix: unify the logged output of the task result --- src/TaskLog.php | 22 +++++++++++++++++++++- tests/unit/TaskLogTest.php | 13 ++++++++----- 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/src/TaskLog.php b/src/TaskLog.php index 8dcddae..bdd9cf0 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 + */ + protected 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, From 27851eab9a4b2fa4afa782c51480f618375b2bcd Mon Sep 17 00:00:00 2001 From: michalsn Date: Mon, 20 Jan 2025 14:24:11 +0100 Subject: [PATCH 2/4] update return type --- src/TaskLog.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/TaskLog.php b/src/TaskLog.php index bdd9cf0..9958caf 100644 --- a/src/TaskLog.php +++ b/src/TaskLog.php @@ -75,8 +75,10 @@ public function __get(string $key) * Unify output to string. * * @param array|bool|int|string|null $value + * + * @return string|null */ - protected function setOutput($value): ?string + protected function setOutput($value) { if (is_string($value) || $value === null) { return $value; From 818311596f04bb35fd11c4009192ffa33c97fd7b Mon Sep 17 00:00:00 2001 From: michalsn Date: Mon, 20 Jan 2025 16:02:00 +0100 Subject: [PATCH 3/4] apply code suggestions Co-authored-by: John Paul E. Balandan, CPA --- src/TaskLog.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/TaskLog.php b/src/TaskLog.php index 9958caf..6cd207c 100644 --- a/src/TaskLog.php +++ b/src/TaskLog.php @@ -74,11 +74,11 @@ public function __get(string $key) /** * Unify output to string. * - * @param array|bool|int|string|null $value + * @param array|bool|int|string|null $value * * @return string|null */ - protected function setOutput($value) + private function setOutput($value): ?string { if (is_string($value) || $value === null) { return $value; From 349c451b26366905e3daf053050e037ffeef04b9 Mon Sep 17 00:00:00 2001 From: michalsn Date: Mon, 20 Jan 2025 16:03:07 +0100 Subject: [PATCH 4/4] cs-fix --- src/TaskLog.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/TaskLog.php b/src/TaskLog.php index 6cd207c..a763e08 100644 --- a/src/TaskLog.php +++ b/src/TaskLog.php @@ -75,8 +75,6 @@ public function __get(string $key) * Unify output to string. * * @param array|bool|int|string|null $value - * - * @return string|null */ private function setOutput($value): ?string {