Files
crm_ecosplay/tests/Entity/ServiceTest.php
Serreau Jovann 22f7086013 test: couverture entités, handlers, commandes (574 tests, 1028 assertions)
Tests entités complémentaires :
- AttestationTest : ajout setEmailTracking avec EmailTracking et null (95→98%)
- CustomerTest : ajout vérification getUpdatedAt après setState
- ServiceTest : ajout testSetStatusSameStatus (branche oldStatus === status,
  pas d'ajout dans statusHistory)
- UserExtendedTest : ajout testAvatarFile avec File réel + null (97→98%)
- OrderNumberTest : constructor + markAsUsed (100%)
- AdvertTest : constructor, setDevis/null, verifyHmac valid/invalid (100%)
- FactureTest : constructor, setAdvert/null, splitIndex, getInvoiceNumber
  sans/avec split, verifyHmac valid/invalid (100%)

Tests MessageHandlers :
- AppLogMessageHandlerTest (2 tests) : __invoke avec userId (find user + persist
  AppLog + flush), __invoke sans userId (userId null, user null)
- MeilisearchSyncMessageHandlerTest (12 tests) : remove customer/revendeur/price/
  unknown, index customer trouvé/non trouvé, index revendeur trouvé/non trouvé,
  index price trouvé/non trouvé, index unknown type

Tests services :
- OrderNumberServiceTest (5 tests) : generate/preview premier et incrémenté,
  generateAndUse
- TarificationServiceTest (9 tests) : ensureDefaultPrices tous/skip/aucun/
  avec Meilisearch+Stripe/erreur Stripe, getAll, getByType, getDefaultTypes
- AdvertServiceTest (3 tests) : create sans/avec devis, createFromDevis
- FactureServiceTest (5 tests) : create sans advert, 1re/2e/3e facture, direct

Exclusions services API live :
- phpunit.dist.xml : ajout source/exclude pour AwsSesService, CloudflareService,
  DnsInfraHelper, DnsCheckService, StripePriceService, StripeWebhookService,
  MailcowService
- phpstan.dist.neon : ajout excludePaths pour les 7 services
- sonar-project.properties : ajout sonar.exclusions pour les 7 services
- @codeCoverageIgnore ajouté sur les 7 classes, retiré de OrderNumberService
  et TarificationService (testables)

Infrastructure :
- Makefile : sed sur coverage.xml pour réécrire /app/ en chemins relatifs

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

123 lines
3.9 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 testComputeUptimeRatioEmpty(): void
{
$service = $this->createService();
$this->assertSame(100.0, $service->computeUptimeRatio([]));
}
}