Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Event/CollectTestResultSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
use Boundwize\Pyrameter\Detection\TestUsageScanner;
use Boundwize\Pyrameter\TestCollector;
use Boundwize\Pyrameter\TestKind;
use Boundwize\Pyrameter\TestRecord;
use Boundwize\Pyrameter\UsageClassifier;
use Boundwize\Pyrameter\ValueObject\TestRecord;
use PHPUnit\Event\Test\Finished;
use PHPUnit\Event\Test\FinishedSubscriber;

Expand Down
3 changes: 1 addition & 2 deletions src/Event/PrintReportSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Boundwize\Pyrameter\Event;

use Boundwize\Pyrameter\PyramidSummary;
use Boundwize\Pyrameter\Report\PyramidReporter;
use Boundwize\Pyrameter\Report\SuiteShapeResolver;
use Boundwize\Pyrameter\Target\TargetEvaluator;
Expand Down Expand Up @@ -38,7 +37,7 @@ public function __construct(

public function notify(ExecutionFinished $event): void
{
$pyramidSummary = PyramidSummary::fromRecords($this->testCollector->all());
$pyramidSummary = $this->testCollector->summary();
$targetEvaluation = $this->targetEvaluator->evaluate($pyramidSummary);
$suiteShape = $this->suiteShapeResolver->resolve($pyramidSummary, $targetEvaluation);

Expand Down
2 changes: 1 addition & 1 deletion src/Report/PyramidReporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

namespace Boundwize\Pyrameter\Report;

use Boundwize\Pyrameter\PyramidSummary;
use Boundwize\Pyrameter\Target\TargetEvaluation;
use Boundwize\Pyrameter\TestKind;
use Boundwize\Pyrameter\ValueObject\PyramidSummary;

use function array_fill;
use function array_reverse;
Expand Down
2 changes: 1 addition & 1 deletion src/Report/SuiteShapeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

namespace Boundwize\Pyrameter\Report;

use Boundwize\Pyrameter\PyramidSummary;
use Boundwize\Pyrameter\Target\TargetEvaluation;
use Boundwize\Pyrameter\TestKind;
use Boundwize\Pyrameter\ValueObject\PyramidSummary;

final readonly class SuiteShapeResolver
{
Expand Down
2 changes: 1 addition & 1 deletion src/Target/TargetEvaluator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

namespace Boundwize\Pyrameter\Target;

use Boundwize\Pyrameter\PyramidSummary;
use Boundwize\Pyrameter\TestKind;
use Boundwize\Pyrameter\ValueObject\PyramidSummary;

final readonly class TargetEvaluator
{
Expand Down
45 changes: 40 additions & 5 deletions src/TestCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,58 @@

namespace Boundwize\Pyrameter;

use function array_values;
use Boundwize\Pyrameter\ValueObject\PyramidSummary;
use Boundwize\Pyrameter\ValueObject\TestRecord;

use function explode;

final class TestCollector
{
/** @var array<string, TestRecord> */
private array $records = [];
/** @var array<string, TestKind> */
private array $kindByTestId = [];

/** @var array<string, int> */
private array $counts = [
'unit' => 0,
'functional' => 0,
'integration' => 0,
'e2e' => 0,
];

public function add(TestRecord $testRecord): void
{
$this->records[$testRecord->id()] = $testRecord;
$testId = $testRecord->id();
$existingKind = $this->kindByTestId[$testId] ?? null;

if ($existingKind === $testRecord->kind) {
return;
}

if ($existingKind instanceof TestKind) {
--$this->counts[$existingKind->value];
}

$this->kindByTestId[$testId] = $testRecord->kind;
++$this->counts[$testRecord->kind->value];
}

public function summary(): PyramidSummary
{
return PyramidSummary::fromCounts($this->counts);
}

/**
* @return list<TestRecord>
*/
public function all(): array
{
return array_values($this->records);
$records = [];

foreach ($this->kindByTestId as $testId => $testKind) {
[$testClassName, $testMethodName] = explode('::', $testId, 2);
$records[] = new TestRecord($testClassName, $testMethodName, [], $testKind);
}

return $records;
}
}
52 changes: 43 additions & 9 deletions src/PyramidSummary.php → src/ValueObject/PyramidSummary.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

declare(strict_types=1);

namespace Boundwize\Pyrameter;
namespace Boundwize\Pyrameter\ValueObject;

use Boundwize\Pyrameter\TestKind;

use function array_keys;
use function count;
use function array_sum;
use function intdiv;
use function usort;

Expand All @@ -27,18 +29,22 @@ public function __construct(
*/
public static function fromRecords(array $records): self
{
$counts = [
TestKind::Unit->value => 0,
TestKind::Functional->value => 0,
TestKind::Integration->value => 0,
TestKind::E2E->value => 0,
];
$counts = self::emptyCounts();

foreach ($records as $record) {
++$counts[$record->kind->value];
}

$total = count($records);
return self::fromCounts($counts);
}

/**
* @param array<string, int> $counts
*/
public static function fromCounts(array $counts): self
{
$counts = self::normalizeCounts($counts);
$total = array_sum($counts);
$percentages = self::percentagesFromCounts($counts, $total);

return new self($total, $counts, $percentages);
Expand All @@ -54,6 +60,34 @@ public function percentage(TestKind $testKind): float
return $this->percentages[$testKind->value] ?? 0.0;
}

/**
* @return array<string, int>
*/
private static function emptyCounts(): array
{
return [
TestKind::Unit->value => 0,
TestKind::Functional->value => 0,
TestKind::Integration->value => 0,
TestKind::E2E->value => 0,
];
}

/**
* @param array<string, int> $counts
* @return array<string, int>
*/
private static function normalizeCounts(array $counts): array
{
$normalizedCounts = self::emptyCounts();

foreach (TestKind::ordered() as $testKind) {
$normalizedCounts[$testKind->value] = $counts[$testKind->value] ?? 0;
}

return $normalizedCounts;
}

/**
* @param array<string, int> $counts
* @return array<string, float>
Expand Down
4 changes: 3 additions & 1 deletion src/TestRecord.php → src/ValueObject/TestRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

declare(strict_types=1);

namespace Boundwize\Pyrameter;
namespace Boundwize\Pyrameter\ValueObject;

use Boundwize\Pyrameter\TestKind;

final readonly class TestRecord
{
Expand Down
2 changes: 1 addition & 1 deletion tests/Event/PrintReportSubscriberTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use Boundwize\Pyrameter\Report\PyramidReporter;
use Boundwize\Pyrameter\TestCollector;
use Boundwize\Pyrameter\TestKind;
use Boundwize\Pyrameter\TestRecord;
use Boundwize\Pyrameter\ValueObject\TestRecord;
use PHPUnit\Event\Telemetry\Duration;
use PHPUnit\Event\Telemetry\GarbageCollectorStatus;
use PHPUnit\Event\Telemetry\HRTime;
Expand Down
4 changes: 2 additions & 2 deletions tests/PyramidSummaryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

namespace Boundwize\Pyrameter\Tests;

use Boundwize\Pyrameter\PyramidSummary;
use Boundwize\Pyrameter\TestKind;
use Boundwize\Pyrameter\TestRecord;
use Boundwize\Pyrameter\ValueObject\PyramidSummary;
use Boundwize\Pyrameter\ValueObject\TestRecord;
use PHPUnit\Framework\TestCase;

use function array_sum;
Expand Down
4 changes: 2 additions & 2 deletions tests/Report/PyramidReporterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
namespace Boundwize\Pyrameter\Tests\Report;

use Boundwize\Pyrameter\Config\PyrameterConfig;
use Boundwize\Pyrameter\PyramidSummary;
use Boundwize\Pyrameter\Report\PyramidReporter;
use Boundwize\Pyrameter\Report\SuiteShapeResolver;
use Boundwize\Pyrameter\Target\TargetEvaluator;
use Boundwize\Pyrameter\TestKind;
use Boundwize\Pyrameter\TestRecord;
use Boundwize\Pyrameter\ValueObject\PyramidSummary;
use Boundwize\Pyrameter\ValueObject\TestRecord;
use PHPUnit\Framework\TestCase;

use function strpos;
Expand Down
4 changes: 2 additions & 2 deletions tests/Report/SuiteShapeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
namespace Boundwize\Pyrameter\Tests\Report;

use Boundwize\Pyrameter\Config\PyrameterConfig;
use Boundwize\Pyrameter\PyramidSummary;
use Boundwize\Pyrameter\Report\SuiteShape;
use Boundwize\Pyrameter\Report\SuiteShapeResolver;
use Boundwize\Pyrameter\Target\TargetEvaluator;
use Boundwize\Pyrameter\TestKind;
use Boundwize\Pyrameter\TestRecord;
use Boundwize\Pyrameter\ValueObject\PyramidSummary;
use Boundwize\Pyrameter\ValueObject\TestRecord;
use PHPUnit\Framework\TestCase;

final class SuiteShapeResolverTest extends TestCase
Expand Down
4 changes: 2 additions & 2 deletions tests/Target/TargetEvaluationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

namespace Boundwize\Pyrameter\Tests\Target;

use Boundwize\Pyrameter\PyramidSummary;
use Boundwize\Pyrameter\Target\TargetEvaluator;
use Boundwize\Pyrameter\Target\TargetStatus;
use Boundwize\Pyrameter\TestKind;
use Boundwize\Pyrameter\TestRecord;
use Boundwize\Pyrameter\ValueObject\PyramidSummary;
use Boundwize\Pyrameter\ValueObject\TestRecord;
use PHPUnit\Framework\TestCase;

final class TargetEvaluationTest extends TestCase
Expand Down
21 changes: 20 additions & 1 deletion tests/TestCollectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use Boundwize\Pyrameter\TestCollector;
use Boundwize\Pyrameter\TestKind;
use Boundwize\Pyrameter\TestRecord;
use Boundwize\Pyrameter\ValueObject\TestRecord;
use PHPUnit\Framework\TestCase;

final class TestCollectorTest extends TestCase
Expand All @@ -19,6 +19,8 @@ public function testItDeduplicatesIdenticalRecords(): void
$testCollector->add(new TestRecord('PriceTest', 'testItCalculatesPrice', [], TestKind::Unit));

$this->assertCount(1, $testCollector->all());
$this->assertSame(1, $testCollector->summary()->total);
$this->assertSame(1, $testCollector->summary()->count(TestKind::Unit));
}

public function testItKeepsSeparateDataProviderExecutions(): void
Expand All @@ -39,5 +41,22 @@ public function testItKeepsSeparateDataProviderExecutions(): void
));

$this->assertCount(2, $testCollector->all());
$this->assertSame(2, $testCollector->summary()->total);
$this->assertSame(2, $testCollector->summary()->count(TestKind::Unit));
}

public function testItReplacesCountsWhenTheSameTestIdChangesKind(): void
{
$testCollector = new TestCollector();

$testCollector->add(new TestRecord('PriceTest', 'testItCalculatesPrice', [], TestKind::Unit));
$testCollector->add(new TestRecord('PriceTest', 'testItCalculatesPrice', [], TestKind::Integration));

$summary = $testCollector->summary();

$this->assertCount(1, $testCollector->all());
$this->assertSame(1, $summary->total);
$this->assertSame(0, $summary->count(TestKind::Unit));
$this->assertSame(1, $summary->count(TestKind::Integration));
}
}
Loading