Skip to content

Commit

Permalink
Utilize raw result for result collection to array conversion instead …
Browse files Browse the repository at this point in the history
…of formatted
  • Loading branch information
DZunke committed Aug 13, 2024
1 parent b0b2668 commit bb21dad
Show file tree
Hide file tree
Showing 5 changed files with 151 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/Result/Group.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function toArray(): array
foreach ($this->metrics as $metric) {
$metricsAsArray[$metric->identifier] = [
'title' => $metric->title,
'value' => $metric->value->format(),
'value' => $metric->value->getRaw(),
];
}

Expand Down
8 changes: 5 additions & 3 deletions src/Result/Metric/IntegerValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Panaly\Result\Metric;

use function number_format;

final readonly class IntegerValue implements Value
{
public function __construct(public int $value)
Expand All @@ -12,11 +14,11 @@ public function __construct(public int $value)

public function getRaw(): int
{
return $this->format();
return $this->value;
}

public function format(): int
public function format(): string
{
return $this->value;
return number_format(num: $this->value);
}
}
6 changes: 4 additions & 2 deletions tests/Collector/CollectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,12 @@ public function testCollectingMetricsWithResults(): void
self::assertCount(2, $metrics);

self::assertSame('I am a default title', $metrics[0]->title);
self::assertSame(12, $metrics[0]->value->format());
self::assertSame(12, $metrics[0]->value->getRaw());
self::assertSame('12', $metrics[0]->value->format());

self::assertSame('I am a default title', $metrics[1]->title);
self::assertSame(12, $metrics[1]->value->format());
self::assertSame(12, $metrics[1]->value->getRaw());
self::assertSame('12', $metrics[1]->value->format());
}

public function testInvalidConfigurationFile(): void
Expand Down
97 changes: 97 additions & 0 deletions tests/Result/GroupTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php

declare(strict_types=1);

namespace Panaly\Test\Result;

use Panaly\Result\Group;
use Panaly\Result\Metric;
use Panaly\Result\Metric\IntegerValue;
use PHPUnit\Framework\TestCase;

class GroupTest extends TestCase
{
public function testGetTitle(): void
{
$group = new Group('identifier', 'title', []);
self::assertSame('title', $group->getTitle());
}

public function testGetIdentifier(): void
{
$group = new Group('identifier', 'title', []);
self::assertSame('identifier', $group->getIdentifier());
}

public function testAddMetric(): void
{
$group = new Group('identifier', 'title', []);
$metric = new Metric('metric1', 'Metric 1', new IntegerValue(100));
$group->addMetric($metric);

self::assertCount(1, $group->getMetrics());
self::assertSame($metric, $group->getMetrics()[0]);
}

public function testToArrayWithEmptyMetrics(): void
{
$group = new Group('identifier', 'title', []);
$expected = [
'title' => 'title',
'metrics' => [],
];

self::assertSame($expected, $group->toArray());
}

public function testToArrayWithSingleMetric(): void
{
$metric = new Metric('metric1', 'Metric 1', new IntegerValue(100));
$group = new Group('identifier', 'title', [$metric]);
$expected = [
'title' => 'title',
'metrics' => [
'metric1' => [
'title' => 'Metric 1',
'value' => 100,
],
],
];

self::assertSame($expected, $group->toArray());
}

public function testToArrayWithMultipleMetrics(): void
{
$metric1 = new Metric('metric1', 'Metric 1', new IntegerValue(10000));
$metric2 = new Metric('metric2', 'Metric 2', new IntegerValue(20000));
$group = new Group('identifier', 'title', [$metric1, $metric2]);
$expected = [
'title' => 'title',
'metrics' => [
'metric1' => [
'title' => 'Metric 1',
'value' => 10000,
],
'metric2' => [
'title' => 'Metric 2',
'value' => 20000,
],
],
];

self::assertSame($expected, $group->toArray());
}

public function testGetMetrics(): void
{
$metric1 = new Metric('metric1', 'Metric 1', new IntegerValue(100));
$metric2 = new Metric('metric2', 'Metric 2', new IntegerValue(200));
$group = new Group('identifier', 'title', [$metric1, $metric2]);

$metrics = $group->getMetrics();
self::assertCount(2, $metrics);
self::assertSame($metric1, $metrics[0]);
self::assertSame($metric2, $metrics[1]);
}
}
44 changes: 44 additions & 0 deletions tests/Result/ResultTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Panaly\Test\Result;

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

Expand All @@ -19,4 +20,47 @@ public function testTheResultConvertedToArrayContainsACreationDateTime(): void
self::assertIsString($resultAsArray['createdAt']);
self::assertSame($result->getCreateAt()->format(DateTimeInterface::ATOM), $resultAsArray['createdAt']);
}

public function testAddGroup(): void
{
$result = new Result();
$group = new Group('identifier', 'title', []);
$result->addGroup($group);

self::assertCount(1, $result->getGroups());
self::assertSame($group, $result->getGroups()[0]);
}

public function testGetGroups(): void
{
$group1 = new Group('identifier1', 'title1', []);
$group2 = new Group('identifier2', 'title2', []);
$result = new Result();
$result->addGroup($group1);
$result->addGroup($group2);

$groups = $result->getGroups();
self::assertCount(2, $groups);
self::assertSame($group1, $groups[0]);
self::assertSame($group2, $groups[1]);
}

public function testToArrayWithGroups(): void
{
$group1 = new Group('identifier1', 'title1', []);
$group2 = new Group('identifier2', 'title2', []);
$result = new Result();
$result->addGroup($group1);
$result->addGroup($group2);

$resultAsArray = $result->toArray();
self::assertArrayHasKey('groups', $resultAsArray);
self::assertCount(2, $resultAsArray['groups']);

self::assertArrayHasKey('identifier1', $resultAsArray['groups']);
self::assertArrayHasKey('identifier2', $resultAsArray['groups']);

self::assertSame($group1->toArray(), $resultAsArray['groups']['identifier1']);
self::assertSame($group2->toArray(), $resultAsArray['groups']['identifier2']);
}
}

0 comments on commit bb21dad

Please sign in to comment.