85428826a0
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
129 lines
4.4 KiB
PHP
129 lines
4.4 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Tests\Unit\Service;
|
|
|
|
use App\Service\ProbeTrait;
|
|
use App\Service\ProviderErrorCode;
|
|
use App\Service\ProviderInterface;
|
|
use App\Service\ProviderStatusType;
|
|
use PHPUnit\Framework\Attributes\CoversClass;
|
|
use PHPUnit\Framework\Attributes\Test;
|
|
use PHPUnit\Framework\TestCase;
|
|
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
|
|
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
|
|
use Symfony\Contracts\HttpClient\ResponseInterface;
|
|
|
|
#[CoversClass(ProbeTrait::class)]
|
|
final class ProbeTraitTest extends TestCase
|
|
{
|
|
private function makeProvider(bool $configured, \Closure $ping): ProviderInterface
|
|
{
|
|
return new class($configured, $ping) implements ProviderInterface {
|
|
use ProbeTrait;
|
|
|
|
public function __construct(
|
|
private bool $configured,
|
|
private \Closure $ping,
|
|
) {}
|
|
|
|
public function isConfigured(): bool { return $this->configured; }
|
|
public function getName(): string { return 'test'; }
|
|
public function ping(): void { ($this->ping)(); }
|
|
public function fetch(): array { return []; }
|
|
};
|
|
}
|
|
|
|
#[Test]
|
|
public function it_returns_not_configured_when_provider_is_not_configured(): void
|
|
{
|
|
$provider = $this->makeProvider(false, fn() => null);
|
|
|
|
$status = $provider->probe();
|
|
|
|
$this->assertSame(ProviderStatusType::NotConfigured, $status->status);
|
|
$this->assertNull($status->error);
|
|
}
|
|
|
|
#[Test]
|
|
public function it_returns_ok_when_ping_succeeds(): void
|
|
{
|
|
$provider = $this->makeProvider(true, fn() => null);
|
|
|
|
$status = $provider->probe();
|
|
|
|
$this->assertSame(ProviderStatusType::Ok, $status->status);
|
|
$this->assertNull($status->error);
|
|
}
|
|
|
|
#[Test]
|
|
public function it_returns_url_unreachable_on_transport_failure(): void
|
|
{
|
|
$exception = $this->createStub(TransportExceptionInterface::class);
|
|
$provider = $this->makeProvider(true, fn() => throw $exception);
|
|
|
|
$status = $provider->probe();
|
|
|
|
$this->assertSame(ProviderStatusType::Error, $status->status);
|
|
$this->assertSame(ProviderErrorCode::UrlUnreachable, $status->error);
|
|
}
|
|
|
|
#[Test]
|
|
public function it_returns_auth_failed_on_401(): void
|
|
{
|
|
$response = $this->createStub(ResponseInterface::class);
|
|
$response->method('getStatusCode')->willReturn(401);
|
|
|
|
$exception = $this->createStub(ClientExceptionInterface::class);
|
|
$exception->method('getResponse')->willReturn($response);
|
|
|
|
$status = $this->makeProvider(true, fn() => throw $exception)->probe();
|
|
|
|
$this->assertSame(ProviderStatusType::Error, $status->status);
|
|
$this->assertSame(ProviderErrorCode::AuthFailed, $status->error);
|
|
}
|
|
|
|
#[Test]
|
|
public function it_returns_auth_failed_on_403(): void
|
|
{
|
|
$response = $this->createStub(ResponseInterface::class);
|
|
$response->method('getStatusCode')->willReturn(403);
|
|
|
|
$exception = $this->createStub(ClientExceptionInterface::class);
|
|
$exception->method('getResponse')->willReturn($response);
|
|
|
|
$status = $this->makeProvider(true, fn() => throw $exception)->probe();
|
|
|
|
$this->assertSame(ProviderStatusType::Error, $status->status);
|
|
$this->assertSame(ProviderErrorCode::AuthFailed, $status->error);
|
|
}
|
|
|
|
#[Test]
|
|
public function it_returns_url_unreachable_on_404(): void
|
|
{
|
|
$response = $this->createStub(ResponseInterface::class);
|
|
$response->method('getStatusCode')->willReturn(404);
|
|
|
|
$exception = $this->createStub(ClientExceptionInterface::class);
|
|
$exception->method('getResponse')->willReturn($response);
|
|
|
|
$status = $this->makeProvider(true, fn() => throw $exception)->probe();
|
|
|
|
$this->assertSame(ProviderStatusType::Error, $status->status);
|
|
$this->assertSame(ProviderErrorCode::UrlUnreachable, $status->error);
|
|
}
|
|
|
|
#[Test]
|
|
public function it_returns_unknown_error_on_unexpected_exception(): void
|
|
{
|
|
$provider = $this->makeProvider(true, fn() => throw new \RuntimeException('Something went wrong'));
|
|
|
|
$status = $provider->probe();
|
|
|
|
$this->assertSame(ProviderStatusType::Error, $status->status);
|
|
$this->assertSame(ProviderErrorCode::Unknown, $status->error);
|
|
$this->assertSame('Something went wrong', $status->message);
|
|
}
|
|
}
|