Files
git-contribution-graph/tests/Unit/Service/ProviderHealthCheckerTest.php
T

89 lines
2.9 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Tests\Unit\Service;
use App\Service\ProviderErrorCode;
use App\Service\ProviderHealthChecker;
use App\Service\ProviderInterface;
use App\Service\ProviderStatus;
use App\Service\ProviderStatusType;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;
#[CoversClass(ProviderHealthChecker::class)]
final class ProviderHealthCheckerTest extends TestCase
{
#[Test]
public function it_returns_ok_when_all_providers_are_healthy(): void
{
$provider = $this->createStub(ProviderInterface::class);
$provider->method('probe')->willReturn(new ProviderStatus('test', ProviderStatusType::Ok));
$result = (new ProviderHealthChecker([$provider]))->check();
$this->assertSame('ok', $result['status']);
}
#[Test]
public function it_returns_degraded_when_a_provider_has_an_error(): void
{
$provider = $this->createStub(ProviderInterface::class);
$provider->method('probe')->willReturn(
new ProviderStatus('test', ProviderStatusType::Error, ProviderErrorCode::AuthFailed, 'Invalid token'),
);
$result = (new ProviderHealthChecker([$provider]))->check();
$this->assertSame('degraded', $result['status']);
}
#[Test]
public function it_does_not_degrade_when_a_provider_is_not_configured(): void
{
$provider = $this->createStub(ProviderInterface::class);
$provider->method('probe')->willReturn(new ProviderStatus('test', ProviderStatusType::NotConfigured));
$result = (new ProviderHealthChecker([$provider]))->check();
$this->assertSame('ok', $result['status']);
}
#[Test]
public function it_indexes_provider_statuses_by_name(): void
{
$provider = $this->createStub(ProviderInterface::class);
$provider->method('probe')->willReturn(new ProviderStatus('github', ProviderStatusType::Ok));
$result = (new ProviderHealthChecker([$provider]))->check();
$this->assertArrayHasKey('github', $result['providers']);
}
#[Test]
public function it_returns_ok_with_no_providers(): void
{
$result = (new ProviderHealthChecker([]))->check();
$this->assertSame('ok', $result['status']);
$this->assertSame([], $result['providers']);
}
#[Test]
public function it_includes_all_provider_statuses_in_output(): void
{
$github = $this->createStub(ProviderInterface::class);
$github->method('probe')->willReturn(new ProviderStatus('github', ProviderStatusType::Ok));
$gitlab = $this->createStub(ProviderInterface::class);
$gitlab->method('probe')->willReturn(new ProviderStatus('gitlab', ProviderStatusType::NotConfigured));
$result = (new ProviderHealthChecker([$github, $gitlab]))->check();
$this->assertArrayHasKey('github', $result['providers']);
$this->assertArrayHasKey('gitlab', $result['providers']);
}
}