144 lines
5.3 KiB
PHP
144 lines
5.3 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Tests\Unit\Application\Health;
|
|
|
|
use Application\Health\Dto\CacheCheckResult;
|
|
use Application\Health\Dto\DatabaseCheckResult;
|
|
use Application\Health\Dto\HealthCheckResult;
|
|
use Application\Health\Dto\HealthChecks;
|
|
use Application\Health\Dto\MailCheckResult;
|
|
use Application\Health\Enums\ComponentStatus;
|
|
use Application\Health\Enums\HealthStatus;
|
|
|
|
describe('Health Check DTOs & Enums Unit Tests', function (): void {
|
|
it('instantiates DatabaseCheckResult as a pure DTO', function (): void {
|
|
// GIVEN
|
|
$dbUp = new DatabaseCheckResult(
|
|
status: ComponentStatus::UP,
|
|
connection: 'mysql',
|
|
);
|
|
|
|
$dbDown = new DatabaseCheckResult(
|
|
status: ComponentStatus::DOWN,
|
|
connection: 'mysql',
|
|
message: 'Connection refused',
|
|
);
|
|
|
|
// WHEN & THEN
|
|
expect($dbUp->status)->toBe(ComponentStatus::UP)
|
|
->and($dbUp->connection)->toBe('mysql')
|
|
->and($dbUp->message)->toBeNull()
|
|
->and($dbDown->status)->toBe(ComponentStatus::DOWN)
|
|
->and($dbDown->connection)->toBe('mysql')
|
|
->and($dbDown->message)->toBe('Connection refused');
|
|
});
|
|
|
|
it('instantiates CacheCheckResult as a pure DTO', function (): void {
|
|
// GIVEN
|
|
$cacheUp = new CacheCheckResult(
|
|
status: ComponentStatus::UP,
|
|
store: 'redis',
|
|
);
|
|
|
|
$cacheDown = new CacheCheckResult(
|
|
status: ComponentStatus::DOWN,
|
|
store: 'redis',
|
|
message: 'Redis server down',
|
|
);
|
|
|
|
// WHEN & THEN
|
|
expect($cacheUp->status)->toBe(ComponentStatus::UP)
|
|
->and($cacheUp->store)->toBe('redis')
|
|
->and($cacheUp->message)->toBeNull()
|
|
->and($cacheDown->status)->toBe(ComponentStatus::DOWN)
|
|
->and($cacheDown->store)->toBe('redis')
|
|
->and($cacheDown->message)->toBe('Redis server down');
|
|
});
|
|
|
|
it('instantiates MailCheckResult as a pure DTO', function (): void {
|
|
// GIVEN
|
|
$mailSmtp = new MailCheckResult(
|
|
status: ComponentStatus::UP,
|
|
transport: 'smtp',
|
|
);
|
|
|
|
$mailFailed = new MailCheckResult(
|
|
status: ComponentStatus::DOWN,
|
|
transport: 'smtp',
|
|
message: 'Connection refused',
|
|
);
|
|
|
|
// WHEN & THEN
|
|
expect($mailSmtp->status)->toBe(ComponentStatus::UP)
|
|
->and($mailSmtp->transport)->toBe('smtp')
|
|
->and($mailSmtp->message)->toBeNull()
|
|
->and($mailFailed->status)->toBe(ComponentStatus::DOWN)
|
|
->and($mailFailed->transport)->toBe('smtp')
|
|
->and($mailFailed->message)->toBe('Connection refused');
|
|
});
|
|
|
|
it('evaluates HealthChecks allUp correctly', function (): void {
|
|
// GIVEN
|
|
$checksUp = new HealthChecks(
|
|
databaseDefault: new DatabaseCheckResult(ComponentStatus::UP, 'sqlite'),
|
|
databaseArcadia: new DatabaseCheckResult(ComponentStatus::UP, 'arcadia'),
|
|
cache: new CacheCheckResult(ComponentStatus::UP, 'database'),
|
|
mailSmtp: new MailCheckResult(ComponentStatus::UP, 'log'),
|
|
);
|
|
|
|
$checksDown = new HealthChecks(
|
|
databaseDefault: new DatabaseCheckResult(ComponentStatus::DOWN, 'sqlite', 'Failed'),
|
|
databaseArcadia: new DatabaseCheckResult(ComponentStatus::UP, 'arcadia'),
|
|
cache: new CacheCheckResult(ComponentStatus::UP, 'database'),
|
|
mailSmtp: new MailCheckResult(ComponentStatus::UP, 'log'),
|
|
);
|
|
|
|
// WHEN & THEN
|
|
expect($checksUp->isAllUp())->toBeTrue()
|
|
->and($checksDown->isAllUp())->toBeFalse();
|
|
});
|
|
|
|
it('centralizes complete mapping in HealthCheckResult', function (): void {
|
|
// GIVEN
|
|
$date = new \DateTimeImmutable('2026-08-21 14:30:00 UTC');
|
|
$checks = new HealthChecks(
|
|
databaseDefault: new DatabaseCheckResult(ComponentStatus::UP, 'sqlite'),
|
|
databaseArcadia: new DatabaseCheckResult(ComponentStatus::DOWN, 'arcadia', 'Timeout connecting to DB'),
|
|
cache: new CacheCheckResult(ComponentStatus::UP, 'database'),
|
|
mailSmtp: new MailCheckResult(ComponentStatus::UP, 'smtp'),
|
|
);
|
|
|
|
$result = new HealthCheckResult(
|
|
status: HealthStatus::UNHEALTHY,
|
|
timestamp: $date,
|
|
checks: $checks,
|
|
);
|
|
|
|
// WHEN
|
|
$array = $result->toArray();
|
|
|
|
// THEN
|
|
expect($result->isHealthy())->toBeFalse()
|
|
->and($array['status'])->toBe('unhealthy')
|
|
->and($array['timestamp'])->toBe('2026-08-21T14:30:00+00:00')
|
|
->and($array['checks']['database_default'])->toBe([
|
|
'status' => 'up',
|
|
'connection' => 'sqlite',
|
|
])
|
|
->and($array['checks']['database_arcadia'])->toBe([
|
|
'status' => 'down',
|
|
'connection' => 'arcadia',
|
|
'message' => 'Timeout connecting to DB',
|
|
])
|
|
->and($array['checks']['cache'])->toBe([
|
|
'status' => 'up',
|
|
'store' => 'database',
|
|
])
|
|
->and($array['checks']['mail_smtp'])->toBe([
|
|
'status' => 'up',
|
|
'transport' => 'smtp',
|
|
]);
|
|
});
|
|
});
|