clusev/tests/Feature/CertServiceTest.php

49 lines
1.7 KiB
PHP

<?php
namespace Tests\Feature;
use App\Services\CertService;
use Tests\TestCase;
class CertServiceTest extends TestCase
{
/** A self-signed cert valid for $days days, so certInfo can be tested without a live network. */
private function makeCert(string $cn, int $days): \OpenSSLCertificate
{
$pk = openssl_pkey_new(['private_key_bits' => 2048, 'private_key_type' => OPENSSL_KEYTYPE_RSA]);
$csr = openssl_csr_new(['commonName' => $cn, 'organizationName' => 'Clusev Test'], $pk);
return openssl_csr_sign($csr, null, $pk, $days);
}
public function test_cert_info_extracts_subject_and_expiry(): void
{
$info = (new CertService)->certInfo($this->makeCert('svc.example.com', 30));
$this->assertSame('svc.example.com', $info['subject']);
$this->assertNotNull($info['expiresAt']);
$this->assertGreaterThanOrEqual(28, $info['daysLeft']); // ~30 days out
$this->assertLessThanOrEqual(30, $info['daysLeft']);
}
public function test_status_maps_days_left_to_the_triad(): void
{
$svc = new CertService;
$this->assertSame('expired', $svc->status(-1));
$this->assertSame('critical', $svc->status(3));
$this->assertSame('warning', $svc->status(20));
$this->assertSame('ok', $svc->status(60));
$this->assertSame('unknown', $svc->status(null));
}
public function test_check_returns_an_error_for_an_unreachable_endpoint(): void
{
// 127.0.0.1:1 is refused immediately → a fast, network-free failure.
$res = (new CertService)->check('127.0.0.1', 1, 1);
$this->assertFalse($res['ok']);
$this->assertArrayHasKey('error', $res);
}
}