refactor(actions): deduplicate audit metadata and grid user count enrichment
This commit is contained in:
92
tests/Service/Audit/AuditMetadataEnricherTest.php
Normal file
92
tests/Service/Audit/AuditMetadataEnricherTest.php
Normal file
@@ -0,0 +1,92 @@
|
||||
<?php
|
||||
|
||||
namespace MintyPHP\Tests\Service\Audit;
|
||||
|
||||
use MintyPHP\Repository\User\UserReadRepositoryInterface;
|
||||
use MintyPHP\Service\Audit\AuditMetadataEnricher;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class AuditMetadataEnricherTest extends TestCase
|
||||
{
|
||||
private function newEnricher(UserReadRepositoryInterface $userReadRepository): AuditMetadataEnricher
|
||||
{
|
||||
return new AuditMetadataEnricher($userReadRepository);
|
||||
}
|
||||
|
||||
public function testEnrichResolvesCreatedByAndModifiedBy(): void
|
||||
{
|
||||
$repo = $this->createMock(UserReadRepositoryInterface::class);
|
||||
$repo->method('find')->willReturnMap([
|
||||
[10, ['first_name' => 'Alice', 'last_name' => 'Smith', 'email' => 'alice@example.com', 'uuid' => 'uuid-10']],
|
||||
[20, ['first_name' => 'Bob', 'last_name' => 'Jones', 'email' => 'bob@example.com', 'uuid' => 'uuid-20']],
|
||||
]);
|
||||
|
||||
$entity = ['created_by' => 10, 'modified_by' => 20];
|
||||
$this->newEnricher($repo)->enrich($entity);
|
||||
|
||||
$this->assertSame('Alice Smith', $entity['created_by_label']);
|
||||
$this->assertSame('uuid-10', $entity['created_by_uuid']);
|
||||
$this->assertSame('Bob Jones', $entity['modified_by_label']);
|
||||
$this->assertSame('uuid-20', $entity['modified_by_uuid']);
|
||||
}
|
||||
|
||||
public function testEnrichFallsBackToEmailWhenNameEmpty(): void
|
||||
{
|
||||
$repo = $this->createMock(UserReadRepositoryInterface::class);
|
||||
$repo->method('find')->willReturn(['first_name' => '', 'last_name' => '', 'email' => 'no-name@example.com', 'uuid' => 'uuid-x']);
|
||||
|
||||
$entity = ['created_by' => 5];
|
||||
$this->newEnricher($repo)->enrich($entity, ['created_by']);
|
||||
|
||||
$this->assertSame('no-name@example.com', $entity['created_by_label']);
|
||||
$this->assertSame('uuid-x', $entity['created_by_uuid']);
|
||||
}
|
||||
|
||||
public function testEnrichSkipsZeroAndNegativeIds(): void
|
||||
{
|
||||
$repo = $this->createMock(UserReadRepositoryInterface::class);
|
||||
$repo->expects($this->never())->method('find');
|
||||
|
||||
$entity = ['created_by' => 0, 'modified_by' => -1];
|
||||
$this->newEnricher($repo)->enrich($entity);
|
||||
|
||||
$this->assertArrayNotHasKey('created_by_label', $entity);
|
||||
$this->assertArrayNotHasKey('modified_by_label', $entity);
|
||||
}
|
||||
|
||||
public function testEnrichSkipsMissingFields(): void
|
||||
{
|
||||
$repo = $this->createMock(UserReadRepositoryInterface::class);
|
||||
$repo->expects($this->never())->method('find');
|
||||
|
||||
$entity = [];
|
||||
$this->newEnricher($repo)->enrich($entity, ['created_by', 'modified_by']);
|
||||
|
||||
$this->assertArrayNotHasKey('created_by_label', $entity);
|
||||
}
|
||||
|
||||
public function testEnrichHandlesUserNotFound(): void
|
||||
{
|
||||
$repo = $this->createMock(UserReadRepositoryInterface::class);
|
||||
$repo->method('find')->willReturn(null);
|
||||
|
||||
$entity = ['created_by' => 999];
|
||||
$this->newEnricher($repo)->enrich($entity, ['created_by']);
|
||||
|
||||
$this->assertArrayNotHasKey('created_by_label', $entity);
|
||||
}
|
||||
|
||||
public function testEnrichWithCustomFields(): void
|
||||
{
|
||||
$repo = $this->createMock(UserReadRepositoryInterface::class);
|
||||
$repo->method('find')->willReturn(['first_name' => 'Admin', 'last_name' => '', 'email' => 'admin@co.com', 'uuid' => 'uuid-a']);
|
||||
|
||||
$entity = ['status_changed_by' => 7, 'active_changed_by' => 7];
|
||||
$this->newEnricher($repo)->enrich($entity, ['status_changed_by', 'active_changed_by']);
|
||||
|
||||
$this->assertSame('Admin', $entity['status_changed_by_label']);
|
||||
$this->assertSame('uuid-a', $entity['status_changed_by_uuid']);
|
||||
$this->assertSame('Admin', $entity['active_changed_by_label']);
|
||||
$this->assertSame('uuid-a', $entity['active_changed_by_uuid']);
|
||||
}
|
||||
}
|
||||
106
tests/Service/Data/GridUserCountEnricherTest.php
Normal file
106
tests/Service/Data/GridUserCountEnricherTest.php
Normal file
@@ -0,0 +1,106 @@
|
||||
<?php
|
||||
|
||||
namespace MintyPHP\Tests\Service\Data;
|
||||
|
||||
use MintyPHP\Service\Data\GridUserCountEnricher;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class GridUserCountEnricherTest extends TestCase
|
||||
{
|
||||
private function enricher(): GridUserCountEnricher
|
||||
{
|
||||
return new GridUserCountEnricher();
|
||||
}
|
||||
|
||||
public function testComputeCountsWithTypicalRows(): void
|
||||
{
|
||||
$rows = [
|
||||
['id' => 1, 'description' => 'A'],
|
||||
['id' => 2, 'description' => 'B'],
|
||||
];
|
||||
$countTotal = static fn (array $ids): array => [1 => 5, 2 => 3];
|
||||
$countActive = static fn (array $ids): array => [1 => 3, 2 => 1];
|
||||
|
||||
$result = $this->enricher()->computeCounts($rows, $countTotal, $countActive);
|
||||
|
||||
$this->assertSame(['active_users' => 3, 'inactive_users' => 2], $result[1]);
|
||||
$this->assertSame(['active_users' => 1, 'inactive_users' => 2], $result[2]);
|
||||
}
|
||||
|
||||
public function testComputeCountsWithEmptyRows(): void
|
||||
{
|
||||
$called = false;
|
||||
$countTotal = static function () use (&$called): array {
|
||||
$called = true;
|
||||
return [];
|
||||
};
|
||||
$countActive = static fn (): array => [];
|
||||
|
||||
$result = $this->enricher()->computeCounts([], $countTotal, $countActive);
|
||||
|
||||
$this->assertSame([], $result);
|
||||
$this->assertFalse($called, 'Count callables should not be invoked for empty rows');
|
||||
}
|
||||
|
||||
public function testComputeCountsDeduplicatesIds(): void
|
||||
{
|
||||
$rows = [
|
||||
['id' => 1],
|
||||
['id' => 1],
|
||||
['id' => 2],
|
||||
];
|
||||
$receivedIds = null;
|
||||
$countTotal = static function (array $ids) use (&$receivedIds): array {
|
||||
$receivedIds = $ids;
|
||||
return [1 => 10, 2 => 5];
|
||||
};
|
||||
$countActive = static fn (array $ids): array => [1 => 8, 2 => 3];
|
||||
|
||||
$this->enricher()->computeCounts($rows, $countTotal, $countActive);
|
||||
|
||||
$this->assertSame([1, 2], $receivedIds);
|
||||
}
|
||||
|
||||
public function testComputeCountsSkipsZeroIds(): void
|
||||
{
|
||||
$rows = [
|
||||
['id' => 0],
|
||||
['id' => -1],
|
||||
['id' => 3],
|
||||
];
|
||||
$receivedIds = null;
|
||||
$countTotal = static function (array $ids) use (&$receivedIds): array {
|
||||
$receivedIds = $ids;
|
||||
return [3 => 2];
|
||||
};
|
||||
$countActive = static fn (array $ids): array => [3 => 1];
|
||||
|
||||
$result = $this->enricher()->computeCounts($rows, $countTotal, $countActive);
|
||||
|
||||
$this->assertSame([3], $receivedIds);
|
||||
$this->assertCount(1, $result);
|
||||
$this->assertSame(['active_users' => 1, 'inactive_users' => 1], $result[3]);
|
||||
}
|
||||
|
||||
public function testComputeCountsWithZeroCounts(): void
|
||||
{
|
||||
$rows = [['id' => 5]];
|
||||
$countTotal = static fn (array $ids): array => [];
|
||||
$countActive = static fn (array $ids): array => [];
|
||||
|
||||
$result = $this->enricher()->computeCounts($rows, $countTotal, $countActive);
|
||||
|
||||
$this->assertSame(['active_users' => 0, 'inactive_users' => 0], $result[5]);
|
||||
}
|
||||
|
||||
public function testInactiveNeverNegative(): void
|
||||
{
|
||||
$rows = [['id' => 1]];
|
||||
$countTotal = static fn (array $ids): array => [1 => 2];
|
||||
$countActive = static fn (array $ids): array => [1 => 5];
|
||||
|
||||
$result = $this->enricher()->computeCounts($rows, $countTotal, $countActive);
|
||||
|
||||
$this->assertSame(0, $result[1]['inactive_users']);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user