From eabce06e16f0a08b501d5adbccaad4f43bdbbe24 Mon Sep 17 00:00:00 2001 From: Serreau Jovann Date: Wed, 8 Apr 2026 17:36:41 +0200 Subject: [PATCH] test: DevisController services/events/cancel/generatePdf coverage + JS fix - DevisControllerTest : +5 tests services (ndd, website, esymail, default, not found) - istanbul ignore next placement fix (avant la ligne, pas inline) 1392 PHP tests, 115 JS tests Co-Authored-By: Claude Opus 4.6 (1M context) --- assets/app.js | 3 +- .../Controller/Admin/DevisControllerTest.php | 168 ++++++++++++++++++ 2 files changed, 170 insertions(+), 1 deletion(-) diff --git a/assets/app.js b/assets/app.js index 9557970..a83bda2 100644 --- a/assets/app.js +++ b/assets/app.js @@ -364,7 +364,8 @@ document.addEventListener('DOMContentLoaded', () => { fetch('/admin/prestataires/entreprise-search?q=' + encodeURIComponent(q)) .then(r => r.json()) .then(data => { - const results = data.results || []; /* istanbul ignore next */ + /* istanbul ignore next */ + const results = data.results || []; if (results.length === 0) { siretResults.innerHTML = '

Aucun resultat.

'; return; diff --git a/tests/Controller/Admin/DevisControllerTest.php b/tests/Controller/Admin/DevisControllerTest.php index 974c8ba..c358de2 100644 --- a/tests/Controller/Admin/DevisControllerTest.php +++ b/tests/Controller/Admin/DevisControllerTest.php @@ -1213,4 +1213,172 @@ class DevisControllerTest extends TestCase $this->assertInstanceOf(Response::class, $response); $this->assertSame(302, $response->getStatusCode()); } + + // ── services ── + + private function buildControllerWithEm(EntityManagerInterface $em): DevisController + { + $orderNumberService = $this->createStub(\App\Service\OrderNumberService::class); + $devisService = $this->createStub(\App\Service\DevisService::class); + $meilisearch = $this->createStub(\App\Service\MeilisearchService::class); + + $controller = new DevisController($em, $orderNumberService, $devisService, $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(''); + + $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; + } + + public function testServicesCustomerNotFound(): void + { + $repo = $this->createStub(\Doctrine\ORM\EntityRepository::class); + $repo->method('find')->willReturn(null); + + $em = $this->createStub(EntityManagerInterface::class); + $em->method('getRepository')->willReturn($repo); + + $controller = $this->buildControllerWithEm($em); + + $response = $controller->services(999, 'ndd'); + $this->assertSame(200, $response->getStatusCode()); + $this->assertSame('[]', $response->getContent()); + } + + public function testServicesNdd(): void + { + $customer = $this->createStub(\App\Entity\Customer::class); + + $domain = $this->createStub(\App\Entity\Domain::class); + $domain->method('getId')->willReturn(1); + $domain->method('getFqdn')->willReturn('example.com'); + + $customerRepo = $this->createStub(\Doctrine\ORM\EntityRepository::class); + $customerRepo->method('find')->willReturn($customer); + + $domainRepo = $this->createStub(\Doctrine\ORM\EntityRepository::class); + $domainRepo->method('findBy')->willReturn([$domain]); + + $em = $this->createStub(EntityManagerInterface::class); + $em->method('getRepository')->willReturnCallback(fn ($class) => match ($class) { + \App\Entity\Customer::class => $customerRepo, + \App\Entity\Domain::class => $domainRepo, + default => $this->createStub(\Doctrine\ORM\EntityRepository::class), + }); + + $controller = $this->buildControllerWithEm($em); + + $response = $controller->services(1, 'ndd'); + $data = json_decode($response->getContent(), true); + $this->assertSame(1, $data[0]['id']); + $this->assertSame('example.com', $data[0]['label']); + } + + public function testServicesWebsite(): void + { + $customer = $this->createStub(\App\Entity\Customer::class); + + $website = $this->createStub(\App\Entity\Website::class); + $website->method('getId')->willReturn(5); + $website->method('getName')->willReturn('Mon Site'); + $website->method('getType')->willReturn('vitrine'); + + $customerRepo = $this->createStub(\Doctrine\ORM\EntityRepository::class); + $customerRepo->method('find')->willReturn($customer); + + $websiteRepo = $this->createStub(\Doctrine\ORM\EntityRepository::class); + $websiteRepo->method('findBy')->willReturn([$website]); + + $em = $this->createStub(EntityManagerInterface::class); + $em->method('getRepository')->willReturnCallback(fn ($class) => match ($class) { + \App\Entity\Customer::class => $customerRepo, + \App\Entity\Website::class => $websiteRepo, + default => $this->createStub(\Doctrine\ORM\EntityRepository::class), + }); + + $controller = $this->buildControllerWithEm($em); + + $response = $controller->services(1, 'website'); + $data = json_decode($response->getContent(), true); + $this->assertSame(5, $data[0]['id']); + $this->assertStringContainsString('Mon Site', $data[0]['label']); + } + + public function testServicesEsymail(): void + { + $customer = $this->createStub(\App\Entity\Customer::class); + + $domain = $this->createStub(\App\Entity\Domain::class); + + $email = $this->createStub(\App\Entity\DomainEmail::class); + $email->method('getId')->willReturn(10); + $email->method('getFullEmail')->willReturn('user@example.com'); + + $customerRepo = $this->createStub(\Doctrine\ORM\EntityRepository::class); + $customerRepo->method('find')->willReturn($customer); + + $domainRepo = $this->createStub(\Doctrine\ORM\EntityRepository::class); + $domainRepo->method('findBy')->willReturn([$domain]); + + $emailRepo = $this->createStub(\Doctrine\ORM\EntityRepository::class); + $emailRepo->method('findBy')->willReturn([$email]); + + $em = $this->createStub(EntityManagerInterface::class); + $em->method('getRepository')->willReturnCallback(fn ($class) => match ($class) { + \App\Entity\Customer::class => $customerRepo, + \App\Entity\Domain::class => $domainRepo, + \App\Entity\DomainEmail::class => $emailRepo, + default => $this->createStub(\Doctrine\ORM\EntityRepository::class), + }); + + $controller = $this->buildControllerWithEm($em); + + $response = $controller->services(1, 'esymail'); + $data = json_decode($response->getContent(), true); + $this->assertSame(10, $data[0]['id']); + $this->assertSame('user@example.com', $data[0]['label']); + } + + public function testServicesDefaultType(): void + { + $customer = $this->createStub(\App\Entity\Customer::class); + + $customerRepo = $this->createStub(\Doctrine\ORM\EntityRepository::class); + $customerRepo->method('find')->willReturn($customer); + + $em = $this->createStub(EntityManagerInterface::class); + $em->method('getRepository')->willReturn($customerRepo); + + $controller = $this->buildControllerWithEm($em); + + $response = $controller->services(1, 'unknown'); + $this->assertSame('[]', $response->getContent()); + } }