Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 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,24 @@ public function __get(string $key)
return $this->{$key};
}
}

/**
* Unify output to string.
*
* @param array|bool|int|string|null $value
Comment thread
paulbalandan marked this conversation as resolved.
Outdated
*
* @return string|null
*/
protected function setOutput($value)
Comment thread
paulbalandan marked this conversation as resolved.
Outdated
{
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