Skip to content

Commit

Permalink
Add creation date to resultset of metric collector to know when the r…
Browse files Browse the repository at this point in the history
…esult was created
  • Loading branch information
DZunke committed Apr 6, 2024
1 parent fd651f2 commit 7383a68
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 6 deletions.
7 changes: 6 additions & 1 deletion src/Collector/Collector.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
use Panaly\Result\Metric;
use Panaly\Result\Result;

use function assert;

readonly class Collector
{
public function __construct(
Expand All @@ -22,7 +24,10 @@ public function collect(): Result
{
$result = new Result();
foreach ($this->configurationFile->metricGroups as $executingGroup) {
$group = new Group($executingGroup->title);
$title = $executingGroup->title;
assert($title !== ''); // Ensured by validation of the object

$group = new Group($title);

foreach ($executingGroup->metrics as $executingMetric) {
$metricHandler = $this->runtimeConfiguration->getMetric($executingMetric->identifier);
Expand Down
7 changes: 5 additions & 2 deletions src/Result/Group.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@

class Group
{
/** @param list<Metric> $metrics */
/**
* @param non-empty-string $title
* @param list<Metric> $metrics
*/
public function __construct(
private readonly string $title,
private array $metrics = [],
Expand All @@ -29,7 +32,7 @@ public function addMetric(Metric $metric): void
$this->metrics[] = $metric;
}

/** @return array{title: string, metrics: list<array{title: string, value: mixed}>} */
/** @return array{title: non-empty-string, metrics: list<array{title: string, value: mixed}>} */
public function toArray(): array
{
$metricsAsArray = [];
Expand Down
32 changes: 29 additions & 3 deletions src/Result/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,25 @@

namespace Panaly\Result;

use DateTimeImmutable;
use DateTimeInterface;

class Result
{
private readonly DateTimeImmutable $createAt;
/** @var list<Group> */
private array $groups = [];

public function __construct()
{
$this->createAt = new DateTimeImmutable();
}

public function getCreateAt(): DateTimeImmutable
{
return $this->createAt;
}

/** @return list<Group> */
public function getGroups(): array
{
Expand All @@ -20,12 +34,24 @@ public function addGroup(Group $group): void
$this->groups[] = $group;
}

/** @return list<array{title: string, metrics: list<array{title: string, value: mixed}>}> */
/**
* @return array{
* createdAt: non-falsy-string,
* groups: list<array{
* title: string,
* metrics: list<array{title: string, value: mixed}>
* }>
* }
*/
public function toArray(): array
{
$asArray = [];
$asArray = [
'createdAt' => $this->createAt->format(DateTimeInterface::ATOM),
'groups' => [],
];

foreach ($this->groups as $group) {
$asArray[] = $group->toArray();
$asArray['groups'][] = $group->toArray();
}

return $asArray;
Expand Down
22 changes: 22 additions & 0 deletions tests/Result/ResultTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace Panaly\Test\Result;

use DateTimeInterface;
use Panaly\Result\Result;
use PHPUnit\Framework\TestCase;

class ResultTest extends TestCase
{
public function testTheResultConvertedToArrayContainsACreationDateTime(): void
{
$result = new Result();
$resultAsArray = $result->toArray();

self::assertArrayHasKey('createdAt', $resultAsArray);
self::assertIsString($resultAsArray['createdAt']);
self::assertSame($result->getCreateAt()->format(DateTimeInterface::ATOM), $resultAsArray['createdAt']);
}
}

0 comments on commit 7383a68

Please sign in to comment.