diff --git a/src/Result/Group.php b/src/Result/Group.php index 2581c2a..571e14c 100644 --- a/src/Result/Group.php +++ b/src/Result/Group.php @@ -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(), ]; } diff --git a/src/Result/Metric/IntegerValue.php b/src/Result/Metric/IntegerValue.php index cbe623c..d9ec612 100644 --- a/src/Result/Metric/IntegerValue.php +++ b/src/Result/Metric/IntegerValue.php @@ -4,6 +4,8 @@ namespace Panaly\Result\Metric; +use function number_format; + final readonly class IntegerValue implements Value { public function __construct(public int $value) @@ -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); } } diff --git a/tests/Collector/CollectorTest.php b/tests/Collector/CollectorTest.php index 3a64e4d..8391bb8 100644 --- a/tests/Collector/CollectorTest.php +++ b/tests/Collector/CollectorTest.php @@ -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 diff --git a/tests/Result/GroupTest.php b/tests/Result/GroupTest.php new file mode 100644 index 0000000..91b88e1 --- /dev/null +++ b/tests/Result/GroupTest.php @@ -0,0 +1,97 @@ +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]); + } +} diff --git a/tests/Result/ResultTest.php b/tests/Result/ResultTest.php index c475e6a..72f6f64 100644 --- a/tests/Result/ResultTest.php +++ b/tests/Result/ResultTest.php @@ -5,6 +5,7 @@ namespace Panaly\Test\Result; use DateTimeInterface; +use Panaly\Result\Group; use Panaly\Result\Result; use PHPUnit\Framework\TestCase; @@ -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']); + } }