Files
crm_ecosplay/tests/Controller/Admin/FactureControllerTest.php
Serreau Jovann 79c55ba0f9 test: ajout 163 tests unitaires (668->831) avec couverture 73%
Entites (76 tests) :
- PrestataireTest : constructeur, setters, getFullAddress, getTotalPaidHt
- FacturePrestataireTest : constructeur, getPeriodLabel 12 mois, Vich upload
- AdvertPaymentTest : constructeur, types constants, method
- AdvertEventTest : constructeur, getTypeLabel, 5 types + fallback
- FactureLineTest : constructeur, setters, optionnels nullable
- ActionLogTest : constructeur, 10 action constants, severity
- PaymentReminderTest : 8 steps, getStepLabel, getSeverity
- DocusealEventTest : constructeur, nullable fields

Commands (16 tests) :
- ReminderFacturesPrestataireCommandTest : 6 scenarios (aucun presta,
  tous OK, factures manquantes, SIRET vide, mois different)
- PaymentReminderCommandTest : 10 scenarios (skip recent, J+15 emails,
  suspension, termination, exception handling)

Services PDF (24 tests) :
- ComptaPdfTest : empty/FEC/multi-page, totaux Debit/Credit
- RapportFinancierPdfTest : recettes/depenses, bilan equilibre/deficit/excedent
- FacturePdfTest : lignes, TVA, customer address, paid badge, multi-page

Controllers (47 tests) :
- ComptabiliteControllerTest : 18 tests (index, 7 exports CSV, 2 JSON,
  4 PDF, 2 rapport financier)
- PrestatairesControllerTest : 19 tests (CRUD, factures, SIRET proxy)
- FactureControllerTest : 6 tests (search, send)
- FactureVerifyControllerTest : 4 tests (HMAC valid/invalid/not found)

Couverture : 51%->60% classes, 58%->73% methodes

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

191 lines
7.6 KiB
PHP

<?php
namespace App\Tests\Controller\Admin;
use App\Controller\Admin\FactureController;
use App\Entity\Customer;
use App\Entity\Facture;
use App\Service\MeilisearchService;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\EntityRepository;
use PHPUnit\Framework\TestCase;
use Psr\Container\ContainerInterface;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
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\Routing\RouterInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
use Twig\Environment;
class FactureControllerTest extends TestCase
{
private function buildController(?EntityManagerInterface $em = null, ?MeilisearchService $meilisearch = null): FactureController
{
$em ??= $this->createStub(EntityManagerInterface::class);
$meilisearch ??= $this->createStub(MeilisearchService::class);
$controller = new FactureController($em, $meilisearch);
$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>');
$router = $this->createStub(RouterInterface::class);
$router->method('generate')->willReturn('/redirect');
$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', $this->createStub(TokenStorageInterface::class)],
['request_stack', $stack],
['parameter_bag', $this->createStub(ParameterBagInterface::class)],
]);
$controller->setContainer($container);
return $controller;
}
// ---------------------------------------------------------------
// search
// ---------------------------------------------------------------
public function testSearchReturnsEmptyWhenQueryBlank(): void
{
$controller = $this->buildController();
$request = new Request(['q' => '']);
$response = $controller->search(1, $request);
$this->assertInstanceOf(JsonResponse::class, $response);
$this->assertSame(200, $response->getStatusCode());
$this->assertSame('[]', $response->getContent());
}
public function testSearchReturnsMeilisearchResults(): void
{
$hits = [['id' => 1, 'invoiceNumber' => 'F-2026-001']];
$meilisearch = $this->createStub(MeilisearchService::class);
$meilisearch->method('searchFactures')->willReturn($hits);
$controller = $this->buildController(null, $meilisearch);
$request = new Request(['q' => 'F-2026']);
$response = $controller->search(1, $request);
$this->assertInstanceOf(JsonResponse::class, $response);
$this->assertSame(200, $response->getStatusCode());
$data = json_decode($response->getContent(), true);
$this->assertCount(1, $data);
$this->assertSame('F-2026-001', $data[0]['invoiceNumber']);
}
public function testSearchPassesCustomerIdFilter(): void
{
$meilisearch = $this->createMock(MeilisearchService::class);
$meilisearch->expects($this->once())
->method('searchFactures')
->with('test', 20, 42)
->willReturn([]);
$controller = $this->buildController(null, $meilisearch);
$request = new Request(['q' => 'test']);
$response = $controller->search(42, $request);
$this->assertInstanceOf(JsonResponse::class, $response);
}
// ---------------------------------------------------------------
// send — facture with no PDF
// ---------------------------------------------------------------
public function testSendRedirectsWhenNoPdfGenerated(): void
{
$customer = $this->createStub(Customer::class);
$customer->method('getId')->willReturn(5);
$facture = $this->createStub(Facture::class);
$facture->method('getFacturePdf')->willReturn(null);
$facture->method('getCustomer')->willReturn($customer);
$factureRepo = $this->createStub(EntityRepository::class);
$factureRepo->method('find')->willReturn($facture);
$em = $this->createStub(EntityManagerInterface::class);
$em->method('getRepository')->willReturn($factureRepo);
$controller = $this->buildController($em);
$mailer = $this->createStub(\App\Service\MailerService::class);
$twig = $this->createStub(Environment::class);
$urlGenerator = $this->createStub(\Symfony\Component\Routing\Generator\UrlGeneratorInterface::class);
$urlGenerator->method('generate')->willReturn('http://localhost/facture/verify/1/abc');
$response = $controller->send(1, $mailer, $twig, $urlGenerator, '/tmp');
$this->assertSame(302, $response->getStatusCode());
}
public function testSendThrows404WhenFactureNotFound(): void
{
$factureRepo = $this->createStub(EntityRepository::class);
$factureRepo->method('find')->willReturn(null);
$em = $this->createStub(EntityManagerInterface::class);
$em->method('getRepository')->willReturn($factureRepo);
$controller = $this->buildController($em);
$mailer = $this->createStub(\App\Service\MailerService::class);
$twig = $this->createStub(Environment::class);
$urlGenerator = $this->createStub(\Symfony\Component\Routing\Generator\UrlGeneratorInterface::class);
$this->expectException(\Symfony\Component\HttpKernel\Exception\NotFoundHttpException::class);
$controller->send(999, $mailer, $twig, $urlGenerator, '/tmp');
}
public function testSendRedirectsWhenCustomerHasNoEmail(): void
{
$customer = $this->createStub(Customer::class);
$customer->method('getId')->willReturn(5);
$customer->method('getEmail')->willReturn(null);
$facture = $this->createStub(Facture::class);
$facture->method('getFacturePdf')->willReturn('facture.pdf');
$facture->method('getCustomer')->willReturn($customer);
$factureRepo = $this->createStub(EntityRepository::class);
$factureRepo->method('find')->willReturn($facture);
$em = $this->createStub(EntityManagerInterface::class);
$em->method('getRepository')->willReturn($factureRepo);
$controller = $this->buildController($em);
$mailer = $this->createStub(\App\Service\MailerService::class);
$twig = $this->createStub(Environment::class);
$urlGenerator = $this->createStub(\Symfony\Component\Routing\Generator\UrlGeneratorInterface::class);
$response = $controller->send(1, $mailer, $twig, $urlGenerator, '/tmp');
$this->assertSame(302, $response->getStatusCode());
}
}