Skip to content

Commit e5ca83d

Browse files
authored
Merge pull request #182 from michalsn/fix/task-log
fix: unify the logged output of the task result
2 parents ec717c4 + 349c451 commit e5ca83d

File tree

2 files changed

+29
-6
lines changed

2 files changed

+29
-6
lines changed

src/TaskLog.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ class TaskLog
4141
public function __construct(array $data)
4242
{
4343
foreach ($data as $key => $value) {
44-
if (property_exists($this, $key)) {
44+
if ($key === 'output') {
45+
$this->output = $this->setOutput($value);
46+
} elseif (property_exists($this, $key)) {
4547
$this->{$key} = $value;
4648
}
4749
}
@@ -68,4 +70,22 @@ public function __get(string $key)
6870
return $this->{$key};
6971
}
7072
}
73+
74+
/**
75+
* Unify output to string.
76+
*
77+
* @param array<int, string>|bool|int|string|null $value
78+
*/
79+
private function setOutput($value): ?string
80+
{
81+
if (is_string($value) || $value === null) {
82+
return $value;
83+
}
84+
85+
if (is_array($value)) {
86+
return implode(PHP_EOL, $value);
87+
}
88+
89+
return (string) $value;
90+
}
7191
}

tests/unit/TaskLogTest.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,35 +28,38 @@ public static function provideDuration(): iterable
2828
'2021-01-21 12:00:00',
2929
'2021-01-21 12:00:00',
3030
'0.00',
31+
['first item', 'second item'],
3132
],
3233
[
3334
'2021-01-21 12:00:00',
3435
'2021-01-21 12:00:01',
3536
'1.00',
37+
true,
3638
],
3739
[
3840
'2021-01-21 12:00:00',
3941
'2021-01-21 12:05:12',
4042
'312.00',
43+
null,
4144
],
4245
];
4346
}
4447

4548
/**
4649
* @dataProvider provideDuration
4750
*
48-
* @param mixed $start
49-
* @param mixed $end
50-
* @param mixed $expected
51+
* @param array|bool|int|string|null $output
52+
*
53+
* @throws Exception
5154
*/
52-
public function testDuration($start, $end, $expected)
55+
public function testDuration(string $start, string $end, string $expected, $output)
5356
{
5457
$start = new Time($start);
5558
$end = new Time($end);
5659

5760
$log = new TaskLog([
5861
'task' => new Task('closure', static function () {}),
59-
'output' => '',
62+
'output' => $output,
6063
'runStart' => $start,
6164
'runEnd' => $end,
6265
'error' => null,

0 commit comments

Comments
 (0)