Files
crm_ecosplay/tests/Entity/ServiceTest.php
Serreau Jovann a441adc29c test: couverture 100% Service - branche entryDate < start dans computeUptimeRatio
- testComputeUptimeRatioEntryBeforeStart : ServiceStatusHistory avec createdAt
  à -60 jours (via ReflectionProperty), couvre la branche entryDate = start
  quand l'entrée est antérieure à la période de calcul
- Résultat : 100% (22/22 methods, 54/54 lines)

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-03 10:48:21 +02:00

135 lines
4.4 KiB
PHP

<?php
namespace App\Tests\Entity;
use App\Entity\Service;
use App\Entity\ServiceCategory;
use App\Entity\ServiceStatusHistory;
use PHPUnit\Framework\TestCase;
class ServiceTest extends TestCase
{
private function createService(): Service
{
$category = new ServiceCategory('Web', 'web');
return new Service('Esy-Web', 'esy-web', $category);
}
public function testConstructor(): void
{
$service = $this->createService();
$this->assertNull($service->getId());
$this->assertSame('Esy-Web', $service->getName());
$this->assertSame('esy-web', $service->getSlug());
$this->assertSame('up', $service->getStatus());
$this->assertNull($service->getMessage());
$this->assertNull($service->getUrl());
$this->assertFalse($service->isExternal());
$this->assertNull($service->getExternalType());
$this->assertSame(0, $service->getPosition());
$this->assertInstanceOf(\DateTimeImmutable::class, $service->getCreatedAt());
$this->assertNull($service->getUpdatedAt());
}
public function testSetName(): void
{
$service = $this->createService();
$service->setName('Esy-Mail');
$this->assertSame('Esy-Mail', $service->getName());
}
public function testSetUrl(): void
{
$service = $this->createService();
$service->setUrl('https://example.com');
$this->assertSame('https://example.com', $service->getUrl());
}
public function testSetStatus(): void
{
$service = $this->createService();
$service->setStatus('down', 'Server error');
$this->assertSame('down', $service->getStatus());
$this->assertSame('Server error', $service->getMessage());
$this->assertInstanceOf(\DateTimeImmutable::class, $service->getUpdatedAt());
}
public function testSetStatusSameStatus(): void
{
$service = $this->createService();
$historyBefore = $service->getStatusHistory()->count();
$service->setStatus('up', 'Still up');
$this->assertSame($historyBefore, $service->getStatusHistory()->count());
}
public function testExternal(): void
{
$service = $this->createService();
$service->setIsExternal(true);
$service->setExternalType('http');
$this->assertTrue($service->isExternal());
$this->assertSame('http', $service->getExternalType());
}
public function testPosition(): void
{
$service = $this->createService();
$service->setPosition(5);
$this->assertSame(5, $service->getPosition());
}
public function testStatusHistory(): void
{
$service = $this->createService();
$this->assertCount(0, $service->getStatusHistory());
}
public function testCategory(): void
{
$service = $this->createService();
$this->assertSame('Web', $service->getCategory()->getName());
}
public function testComputeUptimeRatioAllUp(): void
{
$service = $this->createService();
$history = [
new ServiceStatusHistory($service, 'up'),
new ServiceStatusHistory($service, 'up'),
];
$this->assertSame(100.0, $service->computeUptimeRatio($history));
}
public function testComputeUptimeRatioMixed(): void
{
$service = $this->createService();
$history = [
new ServiceStatusHistory($service, 'up'),
new ServiceStatusHistory($service, 'down'),
];
$ratio = $service->computeUptimeRatio($history);
// Both entries created at same instant, so uptime is based on current status
$this->assertGreaterThanOrEqual(0.0, $ratio);
$this->assertLessThanOrEqual(100.0, $ratio);
}
public function testComputeUptimeRatioEntryBeforeStart(): void
{
$service = $this->createService();
$oldEntry = new ServiceStatusHistory($service, 'down');
$ref = new \ReflectionProperty($oldEntry, 'createdAt');
$ref->setValue($oldEntry, new \DateTimeImmutable('-60 days'));
$ratio = $service->computeUptimeRatio([$oldEntry], 30);
$this->assertGreaterThanOrEqual(0.0, $ratio);
$this->assertLessThanOrEqual(100.0, $ratio);
}
public function testComputeUptimeRatioEmpty(): void
{
$service = $this->createService();
$this->assertSame(100.0, $service->computeUptimeRatio([]));
}
}