Skip to content

Commit

Permalink
Merge pull request #182 from michalsn/fix/task-log
Browse files Browse the repository at this point in the history
fix: unify the logged output of the task result
  • Loading branch information
michalsn authored Jan 20, 2025
2 parents ec717c4 + 349c451 commit e5ca83d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
22 changes: 21 additions & 1 deletion src/TaskLog.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand All @@ -68,4 +70,22 @@ public function __get(string $key)
return $this->{$key};
}
}

/**
* Unify output to string.
*
* @param array<int, string>|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;
}
}
13 changes: 8 additions & 5 deletions tests/unit/TaskLogTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,35 +28,38 @@ 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,
],
];
}

/**
* @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,
Expand Down

0 comments on commit e5ca83d

Please sign in to comment.