Skip to content

Commit

Permalink
Split Raw and formatted output for metric values
Browse files Browse the repository at this point in the history
  • Loading branch information
DZunke committed Aug 12, 2024
1 parent c703cf6 commit f0c9917
Show file tree
Hide file tree
Showing 8 changed files with 135 additions and 7 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->compute(),
'value' => $metric->value->format(),
];
}

Expand Down
7 changes: 6 additions & 1 deletion src/Result/Metric/IntegerValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ public function __construct(public int $value)
{
}

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

public function format(): int
{
return $this->value;
}
Expand Down
8 changes: 7 additions & 1 deletion src/Result/Metric/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,13 @@ public function __construct(
}

/** @return list<list<mixed>> */
public function compute(): array
public function getRaw(): array
{
return $this->format();
}

/** @return list<list<mixed>> */
public function format(): array
{
return array_merge([$this->columns], $this->rows);
}
Expand Down
4 changes: 3 additions & 1 deletion src/Result/Metric/Value.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@

interface Value
{
public function compute(): mixed;
public function getRaw(): mixed;

public function format(): mixed;
}
53 changes: 51 additions & 2 deletions tests/Collector/CollectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Panaly\Result\Result;
use Panaly\Test\Double\MemoryFileProvider;
use PHPUnit\Framework\TestCase;
use Throwable;

class CollectorTest extends TestCase
{
Expand Down Expand Up @@ -46,10 +47,58 @@ public function testCollectingMetricsWithResults(): void
self::assertCount(2, $metrics);

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

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

public function testInvalidConfigurationFile(): void
{
$this->expectException(Throwable::class);

$fixtureFile = __DIR__ . '/../Fixtures/invalid-config.yaml';
$memoryFileProvider = new MemoryFileProvider();
$memoryFileProvider->addFixture($fixtureFile);

$this->getResultFromConfigFile($memoryFileProvider, $fixtureFile);
}

public function testNoFixtures(): void
{
$memoryFileProvider = new MemoryFileProvider();

$this->expectException(Throwable::class);

$this->getResultFromConfigFile($memoryFileProvider, 'non-existent-file.yaml');
}

public function testMultipleGroups(): void
{
$fixtureFile = __DIR__ . '/../Fixtures/valid-config-multiple-groups.yaml';
$memoryFileProvider = new MemoryFileProvider();
$memoryFileProvider->addFixture($fixtureFile);

$collectionResult = $this->getResultFromConfigFile($memoryFileProvider, $fixtureFile);

$groups = $collectionResult->getGroups();
self::assertCount(2, $groups);
self::assertSame('Group 1', $groups[0]->getTitle());
self::assertSame('Group 2', $groups[1]->getTitle());
}

public function testNoMetrics(): void
{
$fixtureFile = __DIR__ . '/../Fixtures/valid-config-no-metrics.yaml';
$memoryFileProvider = new MemoryFileProvider();
$memoryFileProvider->addFixture($fixtureFile);

$collectionResult = $this->getResultFromConfigFile($memoryFileProvider, $fixtureFile);

$groups = $collectionResult->getGroups();
self::assertCount(1, $groups);
self::assertSame('Group with no metrics', $groups[0]->getTitle());
self::assertCount(0, $groups[0]->getMetrics());
}

private function getResultFromConfigFile(MemoryFileProvider $fileProvider, string $file): Result
Expand Down
19 changes: 19 additions & 0 deletions tests/Fixtures/valid-config-multiple-groups.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
plugins:
Panaly\Test\Fixtures\Plugin\TestPlugin: ~

groups:
foo:
title: "Group 1"
metrics:
a_static_integer: ~
bar:
title: "Group 2"
metrics:
a_static_integer: ~

storage:
json:
path: var/metric_storage

reporting:
html_report: ~
14 changes: 14 additions & 0 deletions tests/Fixtures/valid-config-no-metrics.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
plugins:
Panaly\Test\Fixtures\Plugin\TestPlugin: ~

groups:
empty_group:
title: "Group with no metrics"
metrics: []

storage:
json:
path: var/metric_storage

reporting:
html_report: ~
35 changes: 34 additions & 1 deletion tests/Result/Metric/TableResultTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Panaly\Result\Metric\Table;
use PHPUnit\Framework\TestCase;
use Throwable;

class TableResultTest extends TestCase
{
Expand All @@ -18,7 +19,39 @@ public function testTableResultLooksFine(): void

self::assertSame(
[['foo', 'bar'], ['bar', 12], ['baz', 13]],
$metric->compute(),
$metric->format(),
);
}

public function testEmptyColumnsAndRows(): void
{
$metric = new Table([], []);
self::assertSame([[]], $metric->format());
}

public function testSingleColumnAndRow(): void
{
$metric = new Table(['foo'], [['bar']]);
self::assertSame([['foo'], ['bar']], $metric->format());
}

public function testMixedDataTypes(): void
{
$metric = new Table(['foo', 'bar'], [['bar', 12], ['baz', 13.5], ['qux', true]]);
self::assertSame(
[['foo', 'bar'], ['bar', 12], ['baz', 13.5], ['qux', true]],
$metric->format(),
);
}

public function testImmutability(): void
{
$metric = new Table(['foo'], [['bar']]);
try {
$metric->columns = ['new']; // @phpstan-ignore-line because it is readonly
self::fail('Expected Error due to readonly property');
} catch (Throwable $e) {
self::assertSame('Cannot modify readonly property Panaly\Result\Metric\Table::$columns', $e->getMessage());
}
}
}

0 comments on commit f0c9917

Please sign in to comment.