-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(TimeWardenSummary): Summary in array or json format
To be able to obtain a summary of the actions recorded by TimeWarden, both in array and JSON format.
- Loading branch information
Showing
8 changed files
with
203 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tomloprod\TimeWarden; | ||
|
||
final class TimeWardenSummary | ||
{ | ||
/** @return array<string, mixed> */ | ||
public function toArray(): array | ||
{ | ||
/** @var array<string, mixed> $tasksInfo */ | ||
$tasksInfo = []; | ||
|
||
if (timeWarden()->getTasks() !== []) { | ||
$tasksInfo[] = timeWarden()->toArray(); | ||
} | ||
|
||
/** @var Group $group */ | ||
foreach (timeWarden()->getGroups() as $group) { | ||
$tasksInfo[] = $group->toArray(); | ||
} | ||
|
||
return $tasksInfo; | ||
} | ||
|
||
public function toJson(): string | ||
{ | ||
$json = json_encode($this->toArray()); | ||
|
||
return ($json === false) ? '[]' : $json; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Tomloprod\TimeWarden\TimeWardenSummary; | ||
|
||
it('can obtain an array/json', function (): void { | ||
timeWarden()->reset(); | ||
|
||
timeWarden()->task('Generic Task')->start()->stop(); | ||
|
||
$task = timeWarden()->getTasks()[0]; | ||
$task->setTestStartTimestamp(dateTimeToTimestamp(new DateTimeImmutable('2017-06-05 12:00:00.0000000'))); | ||
$task->setTestEndTimestamp(dateTimeToTimestamp(new DateTimeImmutable('2017-06-05 12:00:00.0000000'))); | ||
|
||
timeWarden()->group('Group1')->task('TaskName1')->start()->stop(); | ||
|
||
$groupTask = timeWarden()->getGroups()[0]->getTasks()[0]; | ||
$groupTask->setTestStartTimestamp(dateTimeToTimestamp(new DateTimeImmutable('2017-06-05 12:00:00.0000000'))); | ||
$groupTask->setTestEndTimestamp(dateTimeToTimestamp(new DateTimeImmutable('2017-06-05 12:00:00.0320000'))); | ||
|
||
/** @var TimeWardenSummary $summary */ | ||
$summary = timeWarden()->getSummary(); | ||
|
||
$summaryArray = [ | ||
[ | ||
'name' => 'default', | ||
'duration' => 0.0, | ||
'tasks' => [ | ||
[ | ||
'name' => 'Generic Task', | ||
'duration' => 0.0, | ||
'friendly_duration' => '0ms', | ||
'start_timestamp' => 1496664000.0, | ||
'end_timestamp' => 1496664000.0, | ||
'start_datetime' => '2017-06-05T12:00:00+00:00', | ||
'end_datetime' => '2017-06-05T12:00:00+00:00', | ||
], | ||
], | ||
], | ||
[ | ||
'name' => 'Group1', | ||
'duration' => 32.0, | ||
'tasks' => [ | ||
[ | ||
'name' => 'TaskName1', | ||
'duration' => 32.0, | ||
'friendly_duration' => '32ms', | ||
'start_timestamp' => 1496664000.0, | ||
'end_timestamp' => 1496664000.032, | ||
'start_datetime' => '2017-06-05T12:00:00+00:00', | ||
'end_datetime' => '2017-06-05T12:00:00+00:00', | ||
], | ||
], | ||
], | ||
]; | ||
|
||
expect($summary->toArray())->toBe($summaryArray); | ||
|
||
expect($summary->toJson())->toBe(json_encode($summaryArray)); | ||
}); |