93 lines
3.6 KiB
PHP
93 lines
3.6 KiB
PHP
|
|
<?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']);
|
||
|
|
}
|
||
|
|
}
|