Files
crm_ecosplay/tests/Service/TarificationServiceTest.php
Serreau Jovann 8bda02888c test: couverture 83% methodes (1046 tests, 2135 assertions)
Entites completes a 100% :
- AdvertTest : 12 nouveaux (state, customer, totals, hmac, lines, payments)
- CustomerTest : 3 nouveaux (isPendingDelete, revendeurCode, updatedAt)
- DevisTest : 6 nouveaux (customer, submissionId, lines, state constants)
- FactureTest : 10 nouveaux (state, totals, isPaid, lines, hmac, splitIndex)
- OrderNumberTest : 1 nouveau (markAsUnused)
- WebsiteTest : 1 nouveau (revendeurCode)

Services completes/ameliores :
- DocuSealServiceTest : 30 nouveaux (sendDevis, resendDevis, download, compta)
- AdvertServiceTest : 6 nouveaux (isTvaEnabled, getTvaRate, computeTotals)
- DevisServiceTest : 6 nouveaux (idem)
- FactureServiceTest : 8 nouveaux (idem + createPaidFactureFromAdvert)
- MailerServiceTest : 7 nouveaux (unsubscribe headers, VCF, formatFileSize)
- MeilisearchServiceTest : 42 nouveaux (index/remove/search tous types)
- RgpdServiceTest : 6 nouveaux (sendVerificationCode, verifyCode)
- OrderNumberServiceTest : 2 nouveaux (preview/generate unused)
- TarificationServiceTest : 1 nouveau (stripe error logger)
- ComptaPdfTest : 4 nouveaux (totaux, colonnes numeriques, signature)
- FacturePdfTest : 6 nouveaux (QR code, RIB, CGV Twig, footer skip)

Controllers ameliores :
- ComptabiliteControllerTest : 13 nouveaux (JSON, PDF, sign, callback)
- StatsControllerTest : 2 nouveaux (rich data, 6-month evolution)
- SyncControllerTest : 13 nouveaux (sync 6 types + purge)
- ClientsControllerTest : 7 nouveaux (show, delete, resendWelcome)
- FactureControllerTest : 2 nouveaux (generatePdf 404, send success)
- LegalControllerTest : 6 nouveaux (rgpdVerify GET/POST)
- TarificationControllerTest : 3 nouveaux (purge paths)
- AdminControllersTest : 9 nouveaux (dashboard search, services)
- WebhookStripeControllerTest : 3 nouveaux (invalid signatures)
- KeycloakAuthenticatorTest : 4 nouveaux (groups, domain check)

Commands :
- PaymentReminderCommandTest : 1 nouveau (formalNotice step)
- TestMailCommandTest : 2 nouveaux (force-dsn success/failure)

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

184 lines
6.1 KiB
PHP

<?php
namespace App\Tests\Service;
use App\Entity\PriceAutomatic;
use App\Repository\PriceAutomaticRepository;
use App\Service\MeilisearchService;
use App\Service\StripePriceService;
use App\Service\TarificationService;
use Doctrine\ORM\EntityManagerInterface;
use PHPUnit\Framework\TestCase;
class TarificationServiceTest extends TestCase
{
public function testEnsureDefaultPricesCreatesAll(): void
{
$repo = $this->createStub(PriceAutomaticRepository::class);
$repo->method('findAll')->willReturn([]);
$em = $this->createStub(EntityManagerInterface::class);
$service = new TarificationService($repo, $em);
$created = $service->ensureDefaultPrices();
$this->assertCount(19, $created);
$this->assertContains('esite_business', $created);
$this->assertContains('formation_heure', $created);
}
public function testEnsureDefaultPricesSkipsExisting(): void
{
$existing = new PriceAutomatic();
$existing->setType('esite_business');
$existing->setTitle('E-Site Basique');
$existing->setPriceHt('500.00');
$repo = $this->createStub(PriceAutomaticRepository::class);
$repo->method('findAll')->willReturn([$existing]);
$em = $this->createStub(EntityManagerInterface::class);
$service = new TarificationService($repo, $em);
$created = $service->ensureDefaultPrices();
$this->assertCount(18, $created);
$this->assertNotContains('esite_business', $created);
}
public function testEnsureDefaultPricesNoneCreated(): void
{
$allExisting = [];
foreach (TarificationService::getDefaultTypes() as $type => $data) {
$p = new PriceAutomatic();
$p->setType($type);
$p->setTitle($data['title']);
$p->setPriceHt($data['priceHt']);
$allExisting[] = $p;
}
$repo = $this->createStub(PriceAutomaticRepository::class);
$repo->method('findAll')->willReturn($allExisting);
$em = $this->createStub(EntityManagerInterface::class);
$service = new TarificationService($repo, $em);
$created = $service->ensureDefaultPrices();
$this->assertSame([], $created);
}
public function testEnsureDefaultPricesWithMeilisearchAndStripe(): void
{
$p = new PriceAutomatic();
$p->setType('esite_business');
$p->setTitle('T');
$p->setPriceHt('1.00');
$repo = $this->createStub(PriceAutomaticRepository::class);
$repo->method('findAll')->willReturnOnConsecutiveCalls([], [$p]);
$em = $this->createStub(EntityManagerInterface::class);
$meilisearch = $this->createStub(MeilisearchService::class);
$stripe = $this->createStub(StripePriceService::class);
$service = new TarificationService($repo, $em, null, $meilisearch, $stripe);
$created = $service->ensureDefaultPrices();
$this->assertCount(19, $created);
}
public function testEnsureDefaultPricesStripeError(): void
{
$price = new PriceAutomatic();
$price->setType('esyweb_business');
$price->setTitle('T');
$price->setPriceHt('1.00');
$repo = $this->createStub(PriceAutomaticRepository::class);
$repo->method('findAll')->willReturnOnConsecutiveCalls([], [$price]);
$em = $this->createStub(EntityManagerInterface::class);
$stripe = $this->createStub(StripePriceService::class);
$stripe->method('syncPrice')->willThrowException(new \RuntimeException('Stripe error'));
$service = new TarificationService($repo, $em, null, null, $stripe);
$created = $service->ensureDefaultPrices();
$this->assertCount(19, $created);
}
public function testGetAll(): void
{
$prices = [new PriceAutomatic(), new PriceAutomatic()];
$repo = $this->createStub(PriceAutomaticRepository::class);
$repo->method('findAll')->willReturn($prices);
$em = $this->createStub(EntityManagerInterface::class);
$service = new TarificationService($repo, $em);
$this->assertCount(2, $service->getAll());
}
public function testGetByType(): void
{
$price = new PriceAutomatic();
$price->setType('esyweb_business');
$price->setTitle('T');
$price->setPriceHt('1.00');
$repo = $this->createStub(PriceAutomaticRepository::class);
$repo->method('findOneBy')->willReturn($price);
$em = $this->createStub(EntityManagerInterface::class);
$service = new TarificationService($repo, $em);
$this->assertSame($price, $service->getByType('esite_business'));
}
public function testGetByTypeNotFound(): void
{
$repo = $this->createStub(PriceAutomaticRepository::class);
$repo->method('findOneBy')->willReturn(null);
$em = $this->createStub(EntityManagerInterface::class);
$service = new TarificationService($repo, $em);
$this->assertNull($service->getByType('nonexistent'));
}
public function testGetDefaultTypes(): void
{
$types = TarificationService::getDefaultTypes();
$this->assertCount(19, $types);
$this->assertArrayHasKey('esite_business', $types);
$this->assertArrayHasKey('title', $types['esite_business']);
}
public function testEnsureDefaultPricesStripeErrorWithLogger(): void
{
$price = new PriceAutomatic();
$price->setType('ndd_depot');
$price->setTitle('T');
$price->setPriceHt('1.00');
$repo = $this->createStub(PriceAutomaticRepository::class);
$repo->method('findAll')->willReturnOnConsecutiveCalls([], [$price]);
$em = $this->createStub(EntityManagerInterface::class);
$logger = $this->createMock(\Psr\Log\LoggerInterface::class);
$logger->expects($this->atLeastOnce())->method('error');
$stripe = $this->createStub(StripePriceService::class);
$stripe->method('syncPrice')->willThrowException(new \RuntimeException('Stripe error'));
$service = new TarificationService($repo, $em, $logger, null, $stripe);
$created = $service->ensureDefaultPrices();
$this->assertCount(19, $created);
}
}