Files
crm_ecosplay/tests/Controller/Admin/ComptabiliteControllerTest.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

473 lines
20 KiB
PHP

<?php
namespace App\Tests\Controller\Admin;
use App\Controller\Admin\ComptabiliteController;
use App\Entity\User;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Query;
use Doctrine\ORM\QueryBuilder;
use PHPUnit\Framework\TestCase;
use Psr\Container\ContainerInterface;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
use Twig\Environment;
class ComptabiliteControllerTest extends TestCase
{
private function buildEmWithQueryBuilder(): EntityManagerInterface
{
$stubEm = $this->createStub(EntityManagerInterface::class);
$query = $this->getMockBuilder(Query::class)
->setConstructorArgs([$stubEm])
->onlyMethods(['getResult', '_doExecute', 'getSQL'])
->getMock();
$query->method('getResult')->willReturn([]);
$qb = $this->createStub(QueryBuilder::class);
$qb->method('select')->willReturnSelf();
$qb->method('from')->willReturnSelf();
$qb->method('where')->willReturnSelf();
$qb->method('andWhere')->willReturnSelf();
$qb->method('setParameter')->willReturnSelf();
$qb->method('orderBy')->willReturnSelf();
$qb->method('getQuery')->willReturn($query);
$em = $this->createStub(EntityManagerInterface::class);
$em->method('createQueryBuilder')->willReturn($qb);
return $em;
}
private function buildKernel(): KernelInterface
{
$tmpDir = sys_get_temp_dir().'/comptabilite_test_'.uniqid();
mkdir($tmpDir.'/public', 0777, true);
// logo.jpg is optional — ComptaPdf checks file_exists before using it
$kernel = $this->createStub(KernelInterface::class);
$kernel->method('getProjectDir')->willReturn($tmpDir);
return $kernel;
}
/**
* Build a controller wired with a user token and a router that returns non-empty paths.
* Required for methods that call getUser() and generateUrl()/redirectToRoute().
*/
private function buildSignController(): \App\Controller\Admin\ComptabiliteController
{
$em = $this->buildEmWithQueryBuilder();
$kernel = $this->buildKernel();
$controller = new \App\Controller\Admin\ComptabiliteController($em, $kernel, false, 'http://docuseal.example');
$session = new Session(new MockArraySessionStorage());
$stack = $this->createStub(RequestStack::class);
$stack->method('getSession')->willReturn($session);
$twig = $this->createStub(\Twig\Environment::class);
$twig->method('render')->willReturn('<html></html>');
$router = $this->createStub(\Symfony\Component\Routing\RouterInterface::class);
$router->method('generate')->willReturn('/admin/comptabilite');
$user = new User();
$user->setEmail('admin@e-cosplay.fr');
$user->setFirstName('Admin');
$user->setLastName('Test');
$user->setPassword('h');
$token = $this->createStub(TokenInterface::class);
$token->method('getUser')->willReturn($user);
$tokenStorage = $this->createStub(TokenStorageInterface::class);
$tokenStorage->method('getToken')->willReturn($token);
$container = $this->createStub(ContainerInterface::class);
$container->method('has')->willReturnMap([
['twig', true],
['router', true],
['security.authorization_checker', true],
['security.token_storage', true],
['request_stack', true],
['parameter_bag', true],
['serializer', false],
]);
$container->method('get')->willReturnMap([
['twig', $twig],
['router', $router],
['security.authorization_checker', $this->createStub(AuthorizationCheckerInterface::class)],
['security.token_storage', $tokenStorage],
['request_stack', $stack],
['parameter_bag', $this->createStub(ParameterBagInterface::class)],
]);
$controller->setContainer($container);
return $controller;
}
private function buildController(): ComptabiliteController
{
$em = $this->buildEmWithQueryBuilder();
$kernel = $this->buildKernel();
$controller = new ComptabiliteController($em, $kernel, false, 'http://docuseal.example');
$session = new Session(new MockArraySessionStorage());
$stack = $this->createStub(RequestStack::class);
$stack->method('getSession')->willReturn($session);
$twig = $this->createStub(Environment::class);
$twig->method('render')->willReturn('<html></html>');
$container = $this->createStub(ContainerInterface::class);
$container->method('has')->willReturnMap([
['twig', true],
['router', true],
['security.authorization_checker', true],
['security.token_storage', true],
['request_stack', true],
['parameter_bag', true],
['serializer', false],
]);
$container->method('get')->willReturnMap([
['twig', $twig],
['router', $this->createStub(RouterInterface::class)],
['security.authorization_checker', $this->createStub(AuthorizationCheckerInterface::class)],
['security.token_storage', $this->createStub(TokenStorageInterface::class)],
['request_stack', $stack],
['parameter_bag', $this->createStub(ParameterBagInterface::class)],
]);
$controller->setContainer($container);
return $controller;
}
public function testIndexReturns200(): void
{
$controller = $this->buildController();
$response = $controller->index();
$this->assertSame(200, $response->getStatusCode());
}
public function testExportJournalVentesCsv(): void
{
$controller = $this->buildController();
$request = new Request(['period' => 'current', 'format' => 'csv']);
$response = $controller->exportJournalVentes($request);
$this->assertSame(200, $response->getStatusCode());
$contentType = $response->headers->get('Content-Type') ?? '';
$this->assertStringContainsString('text/csv', $contentType);
}
public function testExportJournalVentesJson(): void
{
$controller = $this->buildController();
$request = new Request(['period' => 'current', 'format' => 'json']);
$response = $controller->exportJournalVentes($request);
$this->assertSame(200, $response->getStatusCode());
$contentType = $response->headers->get('Content-Type') ?? '';
$this->assertStringContainsString('application/json', $contentType);
}
public function testExportJournalVentesPreviousPeriod(): void
{
$controller = $this->buildController();
$request = new Request(['period' => 'previous', 'format' => 'csv']);
$response = $controller->exportJournalVentes($request);
$this->assertSame(200, $response->getStatusCode());
}
public function testExportJournalVentesCustomPeriod(): void
{
$controller = $this->buildController();
$request = new Request(['period' => 'custom', 'from' => '2026-01-01', 'to' => '2026-03-31', 'format' => 'csv']);
$response = $controller->exportJournalVentes($request);
$this->assertSame(200, $response->getStatusCode());
}
public function testExportCommissionsStripeCsv(): void
{
$controller = $this->buildController();
$request = new Request(['period' => 'current', 'format' => 'csv']);
$response = $controller->exportCommissionsStripe($request);
$this->assertSame(200, $response->getStatusCode());
$contentType = $response->headers->get('Content-Type') ?? '';
$this->assertStringContainsString('text/csv', $contentType);
}
public function testExportCommissionsStripeJson(): void
{
$controller = $this->buildController();
$request = new Request(['period' => 'current', 'format' => 'json']);
$response = $controller->exportCommissionsStripe($request);
$this->assertSame(200, $response->getStatusCode());
$contentType = $response->headers->get('Content-Type') ?? '';
$this->assertStringContainsString('application/json', $contentType);
}
public function testExportCoutsServicesCsv(): void
{
$controller = $this->buildController();
$request = new Request(['period' => 'current', 'format' => 'csv']);
$response = $controller->exportCoutsServices($request);
$this->assertSame(200, $response->getStatusCode());
$contentType = $response->headers->get('Content-Type') ?? '';
$this->assertStringContainsString('text/csv', $contentType);
}
public function testExportPdfJournalVentes(): void
{
$controller = $this->buildController();
$request = new Request(['period' => 'current']);
$response = $controller->exportPdf('journal-ventes', $request);
$this->assertSame(200, $response->getStatusCode());
$this->assertSame('application/pdf', $response->headers->get('Content-Type'));
}
public function testExportPdfCommissionsStripe(): void
{
$controller = $this->buildController();
$request = new Request(['period' => 'current']);
$response = $controller->exportPdf('commissions-stripe', $request);
$this->assertSame(200, $response->getStatusCode());
$this->assertSame('application/pdf', $response->headers->get('Content-Type'));
}
public function testExportPdfCoutsServices(): void
{
$controller = $this->buildController();
$request = new Request(['period' => 'current']);
$response = $controller->exportPdf('couts-services', $request);
$this->assertSame(200, $response->getStatusCode());
$this->assertSame('application/pdf', $response->headers->get('Content-Type'));
}
public function testExportPdfBalanceAgee(): void
{
$controller = $this->buildController();
$request = new Request(['period' => 'current']);
$response = $controller->exportPdf('balance-agee', $request);
$this->assertSame(200, $response->getStatusCode());
$this->assertSame('application/pdf', $response->headers->get('Content-Type'));
}
public function testRapportFinancierReturnsPdf(): void
{
$controller = $this->buildController();
$request = new Request(['period' => 'current']);
$response = $controller->rapportFinancier($request);
$this->assertSame(200, $response->getStatusCode());
$this->assertSame('application/pdf', $response->headers->get('Content-Type'));
}
public function testRapportFinancierPreviousPeriod(): void
{
$controller = $this->buildController();
$request = new Request(['period' => 'previous']);
$response = $controller->rapportFinancier($request);
$this->assertSame(200, $response->getStatusCode());
$this->assertSame('application/pdf', $response->headers->get('Content-Type'));
}
public function testExportGrandLivreCsv(): void
{
$controller = $this->buildController();
$request = new Request(['period' => 'current', 'format' => 'csv']);
$response = $controller->exportGrandLivre($request);
$this->assertSame(200, $response->getStatusCode());
$contentType = $response->headers->get('Content-Type') ?? '';
$this->assertStringContainsString('text/csv', $contentType);
}
public function testExportFecCsv(): void
{
$controller = $this->buildController();
$request = new Request(['period' => 'current', 'format' => 'csv']);
$response = $controller->exportFec($request);
$this->assertSame(200, $response->getStatusCode());
$contentType = $response->headers->get('Content-Type') ?? '';
$this->assertStringContainsString('text/csv', $contentType);
}
public function testExportBalanceAgeeCsv(): void
{
$controller = $this->buildController();
$request = new Request(['format' => 'csv']);
$response = $controller->exportBalanceAgee($request);
$this->assertSame(200, $response->getStatusCode());
$contentType = $response->headers->get('Content-Type') ?? '';
$this->assertStringContainsString('text/csv', $contentType);
}
public function testExportReglementsCsv(): void
{
$controller = $this->buildController();
$request = new Request(['period' => 'current', 'format' => 'csv']);
$response = $controller->exportReglements($request);
$this->assertSame(200, $response->getStatusCode());
$contentType = $response->headers->get('Content-Type') ?? '';
$this->assertStringContainsString('text/csv', $contentType);
}
public function testExportGrandLivreJson(): void
{
$controller = $this->buildController();
$request = new Request(['period' => 'current', 'format' => 'json']);
$response = $controller->exportGrandLivre($request);
$this->assertSame(200, $response->getStatusCode());
$contentType = $response->headers->get('Content-Type') ?? '';
$this->assertStringContainsString('application/json', $contentType);
}
public function testExportFecJson(): void
{
$controller = $this->buildController();
$request = new Request(['period' => 'current', 'format' => 'json']);
$response = $controller->exportFec($request);
$this->assertSame(200, $response->getStatusCode());
$contentType = $response->headers->get('Content-Type') ?? '';
$this->assertStringContainsString('application/json', $contentType);
}
public function testExportBalanceAgeeJson(): void
{
$controller = $this->buildController();
$request = new Request(['format' => 'json']);
$response = $controller->exportBalanceAgee($request);
$this->assertSame(200, $response->getStatusCode());
$contentType = $response->headers->get('Content-Type') ?? '';
$this->assertStringContainsString('application/json', $contentType);
}
public function testExportReglementsJson(): void
{
$controller = $this->buildController();
$request = new Request(['period' => 'current', 'format' => 'json']);
$response = $controller->exportReglements($request);
$this->assertSame(200, $response->getStatusCode());
$contentType = $response->headers->get('Content-Type') ?? '';
$this->assertStringContainsString('application/json', $contentType);
}
public function testExportPdfFec(): void
{
$controller = $this->buildController();
$request = new Request(['period' => 'current']);
$response = $controller->exportPdf('fec', $request);
$this->assertSame(200, $response->getStatusCode());
$this->assertSame('application/pdf', $response->headers->get('Content-Type'));
}
public function testExportPdfGrandLivre(): void
{
$controller = $this->buildController();
$request = new Request(['period' => 'current']);
$response = $controller->exportPdf('grand-livre', $request);
$this->assertSame(200, $response->getStatusCode());
$this->assertSame('application/pdf', $response->headers->get('Content-Type'));
}
public function testExportPdfReglements(): void
{
$controller = $this->buildController();
$request = new Request(['period' => 'current']);
$response = $controller->exportPdf('reglements', $request);
$this->assertSame(200, $response->getStatusCode());
$this->assertSame('application/pdf', $response->headers->get('Content-Type'));
}
public function testExportPdfSignRedirectsToDocuSeal(): void
{
$docuSeal = $this->createStub(\App\Service\DocuSealService::class);
$docuSeal->method('sendComptaForSignature')->willReturn(42);
$docuSeal->method('getSubmitterSlug')->willReturn('abc123');
$controller = $this->buildSignController();
$request = new Request(['period' => 'current']);
$session = new Session(new MockArraySessionStorage());
$request->setSession($session);
$response = $controller->exportPdfSign('journal-ventes', $request, $docuSeal);
$this->assertSame(302, $response->getStatusCode());
$this->assertStringContainsString('docuseal.example', $response->headers->get('Location') ?? '');
}
public function testExportPdfSignDocuSealNoSlug(): void
{
$docuSeal = $this->createStub(\App\Service\DocuSealService::class);
$docuSeal->method('sendComptaForSignature')->willReturn(42);
$docuSeal->method('getSubmitterSlug')->willReturn(null);
$controller = $this->buildSignController();
$request = new Request(['period' => 'current']);
$response = $controller->exportPdfSign('journal-ventes', $request, $docuSeal);
// No slug -> redirect to index
$this->assertSame(302, $response->getStatusCode());
}
public function testSignCallbackWithNoSession(): void
{
$controller = $this->buildSignController();
$request = new Request();
$session = new Session(new MockArraySessionStorage());
$request->setSession($session);
$docuSeal = $this->createStub(\App\Service\DocuSealService::class);
$mailer = $this->createStub(\App\Service\MailerService::class);
$twig = $this->createStub(\Twig\Environment::class);
// No submitter_id in session -> "Session de signature expiree"
$response = $controller->signCallback('journal-ventes', $request, $docuSeal, $mailer, $twig);
$this->assertSame(302, $response->getStatusCode());
}
public function testSignCallbackWithSessionNoPdf(): void
{
$controller = $this->buildSignController();
$request = new Request();
$session = new Session(new MockArraySessionStorage());
$session->set('compta_submitter_id', 99);
$request->setSession($session);
$docuSeal = $this->createStub(\App\Service\DocuSealService::class);
$docuSeal->method('getSubmitterData')->willReturn([
'documents' => [],
'audit_log_url' => null,
'metadata' => [],
]);
$mailer = $this->createStub(\App\Service\MailerService::class);
$twig = $this->createStub(\Twig\Environment::class);
$twig->method('render')->willReturn('<html></html>');
$response = $controller->signCallback('journal-ventes', $request, $docuSeal, $mailer, $twig);
$this->assertSame(302, $response->getStatusCode());
}
public function testRapportFinancierSignRedirects(): void
{
$docuSeal = $this->createStub(\App\Service\DocuSealService::class);
$docuSeal->method('sendComptaForSignature')->willReturn(10);
$docuSeal->method('getSubmitterSlug')->willReturn('slug-rap');
$controller = $this->buildSignController();
$request = new Request(['period' => 'current']);
$session = new Session(new MockArraySessionStorage());
$request->setSession($session);
$response = $controller->rapportFinancierSign($request, $docuSeal);
$this->assertSame(302, $response->getStatusCode());
}
}