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

555 lines
22 KiB
PHP

<?php
namespace App\Tests\Controller\Admin;
use App\Controller\Admin\ClientsController;
use App\Entity\Customer;
use App\Entity\User;
use App\Repository\CustomerRepository;
use App\Service\MailerService;
use App\Service\MeilisearchService;
use App\Service\UserManagementService;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Symfony\Contracts\HttpClient\ResponseInterface;
use Doctrine\ORM\EntityManagerInterface;
use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
use Symfony\Component\Routing\RouterInterface;
use Twig\Environment;
class ClientsControllerTest extends TestCase
{
private function createController(?Request $request = null): ClientsController
{
$twig = $this->createStub(Environment::class);
$twig->method('render')->willReturn('<html></html>');
$router = $this->createStub(RouterInterface::class);
$router->method('generate')->willReturn('/admin/clients');
$requestStack = new RequestStack();
if (null !== $request) {
$requestStack->push($request);
}
$container = $this->createStub(ContainerInterface::class);
$container->method('has')->willReturn(true);
$container->method('get')->willReturnMap([
['twig', $twig],
['router', $router],
['request_stack', $requestStack],
]);
$controller = new ClientsController();
$controller->setContainer($container);
return $controller;
}
public function testIndex(): void
{
$repo = $this->createStub(CustomerRepository::class);
$repo->method('findBy')->willReturn([]);
$em = $this->createStub(EntityManagerInterface::class);
$controller = $this->createController();
$response = $controller->index($repo, $em);
$this->assertInstanceOf(Response::class, $response);
}
public function testCreateGet(): void
{
$controller = $this->createController();
$request = new Request();
$request->setMethod('GET');
$repo = $this->createStub(CustomerRepository::class);
$em = $this->createStub(EntityManagerInterface::class);
$meilisearch = $this->createStub(MeilisearchService::class);
$userService = $this->createStub(UserManagementService::class);
$logger = $this->createStub(LoggerInterface::class);
$response = $controller->create($request, $repo, $this->createStub(\App\Repository\RevendeurRepository::class), $em, $meilisearch, $userService, $logger, $this->createStub(HttpClientInterface::class), $this->createStub(MailerService::class), $this->createStub(Environment::class), 'sk_test_***');
$this->assertInstanceOf(Response::class, $response);
}
public function testCreatePostInvalidData(): void
{
$request = new Request([], ['firstName' => '', 'lastName' => '', 'email' => '']);
$request->setMethod('POST');
$request->setSession(new Session(new MockArraySessionStorage()));
$controller = $this->createController($request);
$repo = $this->createStub(CustomerRepository::class);
$em = $this->createStub(EntityManagerInterface::class);
$meilisearch = $this->createStub(MeilisearchService::class);
$userService = $this->createStub(UserManagementService::class);
$userService->method('createBaseUser')->willThrowException(new \InvalidArgumentException('Champs requis'));
$logger = $this->createStub(LoggerInterface::class);
$response = $controller->create($request, $repo, $this->createStub(\App\Repository\RevendeurRepository::class), $em, $meilisearch, $userService, $logger, $this->createStub(HttpClientInterface::class), $this->createStub(MailerService::class), $this->createStub(Environment::class), 'sk_test_***');
$this->assertInstanceOf(Response::class, $response);
}
public function testCreatePostThrowsGenericError(): void
{
$request = new Request([], ['firstName' => 'Test', 'lastName' => 'User', 'email' => 'test@test.com']);
$request->setMethod('POST');
$request->setSession(new Session(new MockArraySessionStorage()));
$controller = $this->createController($request);
$repo = $this->createStub(CustomerRepository::class);
$em = $this->createStub(EntityManagerInterface::class);
$meilisearch = $this->createStub(MeilisearchService::class);
$userService = $this->createStub(UserManagementService::class);
$userService->method('createBaseUser')->willThrowException(new \RuntimeException('DB error'));
$logger = $this->createStub(LoggerInterface::class);
$response = $controller->create($request, $repo, $this->createStub(\App\Repository\RevendeurRepository::class), $em, $meilisearch, $userService, $logger, $this->createStub(HttpClientInterface::class), $this->createStub(MailerService::class), $this->createStub(Environment::class), 'sk_test_***');
$this->assertInstanceOf(Response::class, $response);
}
public function testSearchEmpty(): void
{
$meilisearch = $this->createStub(MeilisearchService::class);
$controller = $this->createController();
$request = new Request(['q' => '']);
$response = $controller->search($request, $meilisearch);
$this->assertInstanceOf(JsonResponse::class, $response);
$this->assertSame('[]', $response->getContent());
}
public function testSearchWithQuery(): void
{
$meilisearch = $this->createStub(MeilisearchService::class);
$meilisearch->method('searchCustomers')->willReturn([['id' => 1, 'fullName' => 'John Doe']]);
$controller = $this->createController();
$request = new Request(['q' => 'John']);
$response = $controller->search($request, $meilisearch);
$this->assertInstanceOf(JsonResponse::class, $response);
$this->assertStringContainsString('John Doe', $response->getContent());
}
public function testEntrepriseSearchTooShort(): void
{
$controller = new ClientsController();
$request = new Request(['q' => 'a']);
$response = $controller->entrepriseSearch($request, $this->createStub(HttpClientInterface::class));
$this->assertInstanceOf(JsonResponse::class, $response);
$this->assertStringContainsString('"total_results":0', $response->getContent());
}
public function testEntrepriseSearchSuccess(): void
{
$apiResponse = $this->createStub(ResponseInterface::class);
$apiResponse->method('toArray')->willReturn(['results' => [['siren' => '123456789']], 'total_results' => 1]);
$httpClient = $this->createStub(HttpClientInterface::class);
$httpClient->method('request')->willReturn($apiResponse);
$controller = new ClientsController();
$request = new Request(['q' => 'siteconseil']);
$response = $controller->entrepriseSearch($request, $httpClient);
$this->assertInstanceOf(JsonResponse::class, $response);
$this->assertStringContainsString('123456789', $response->getContent());
}
public function testEntrepriseSearchApiError(): void
{
$httpClient = $this->createStub(HttpClientInterface::class);
$httpClient->method('request')->willThrowException(new \RuntimeException('API down'));
$controller = new ClientsController();
$request = new Request(['q' => 'test']);
$response = $controller->entrepriseSearch($request, $httpClient);
$this->assertSame(502, $response->getStatusCode());
$this->assertStringContainsString('Service indisponible', $response->getContent());
}
public function testToggle(): void
{
$user = new User();
$user->setEmail('t@t.com');
$user->setFirstName('T');
$user->setLastName('T');
$user->setPassword('h');
$customer = new Customer($user);
$request = new Request();
$request->setSession(new Session(new MockArraySessionStorage()));
$em = $this->createMock(EntityManagerInterface::class);
$em->expects($this->once())->method('flush');
$meilisearch = $this->createStub(MeilisearchService::class);
$logger = $this->createStub(LoggerInterface::class);
$controller = $this->createController($request);
$response = $controller->toggle($customer, $em, $meilisearch, $logger);
$this->assertSame(302, $response->getStatusCode());
$this->assertFalse($customer->isActive());
}
public function testToggleSuspendedToActive(): void
{
$user = new User();
$user->setEmail('t@t.com');
$user->setFirstName('T');
$user->setLastName('T');
$user->setPassword('h');
$customer = new Customer($user);
$customer->setState(Customer::STATE_SUSPENDED);
$request = new Request();
$request->setSession(new Session(new MockArraySessionStorage()));
$controller = $this->createController($request);
$response = $controller->toggle($customer, $this->createStub(EntityManagerInterface::class), $this->createStub(MeilisearchService::class), $this->createStub(LoggerInterface::class));
$this->assertSame(302, $response->getStatusCode());
$this->assertTrue($customer->isActive());
}
public function testToggleMeilisearchError(): void
{
$user = new User();
$user->setEmail('t@t.com');
$user->setFirstName('T');
$user->setLastName('T');
$user->setPassword('h');
$customer = new Customer($user);
$meilisearch = $this->createStub(MeilisearchService::class);
$meilisearch->method('indexCustomer')->willThrowException(new \RuntimeException('Meili down'));
$request = new Request();
$request->setSession(new Session(new MockArraySessionStorage()));
$controller = $this->createController($request);
$response = $controller->toggle($customer, $this->createStub(EntityManagerInterface::class), $meilisearch, $this->createStub(LoggerInterface::class));
$this->assertSame(302, $response->getStatusCode());
}
public function testCreatePostSuccessNoStripe(): void
{
$user = new User();
$user->setEmail('new@test.com');
$user->setFirstName('Jean');
$user->setLastName('Client');
$user->setPassword('h');
$userService = $this->createStub(UserManagementService::class);
$userService->method('createBaseUser')->willReturn($user);
$repo = $this->createStub(CustomerRepository::class);
$repo->method('generateUniqueCodeComptable')->willReturn('CLI-00001');
$request = new Request([], [
'firstName' => 'Jean',
'lastName' => 'Client',
'email' => 'new@test.com',
'phone' => '0612345678',
'raisonSociale' => 'Ma SARL',
'siret' => '12345678901234',
'rcs' => 'RCS Paris',
'numTva' => 'FR12345678901',
'address' => '1 rue Test',
'address2' => 'Bat A',
'zipCode' => '75001',
'city' => 'Paris',
'typeCompany' => 'SARL',
]);
$request->setMethod('POST');
$request->setSession(new Session(new MockArraySessionStorage()));
$controller = $this->createController($request);
$response = $controller->create(
$request,
$repo,
$this->createStub(\App\Repository\RevendeurRepository::class),
$this->createStub(EntityManagerInterface::class),
$this->createStub(MeilisearchService::class),
$userService,
$this->createStub(LoggerInterface::class),
$this->createStub(HttpClientInterface::class),
$this->createStub(MailerService::class),
$this->createStub(Environment::class),
'',
);
$this->assertSame(302, $response->getStatusCode());
}
public function testCreatePostSuccessStripeBypass(): void
{
$user = new User();
$user->setEmail('a@b.com');
$user->setFirstName('A');
$user->setLastName('B');
$user->setPassword('h');
$userService = $this->createStub(UserManagementService::class);
$userService->method('createBaseUser')->willReturn($user);
$repo = $this->createStub(CustomerRepository::class);
$repo->method('generateUniqueCodeComptable')->willReturn('CLI-00002');
$request = new Request([], [
'firstName' => 'A', 'lastName' => 'B', 'email' => 'a@b.com',
'phone' => '', 'raisonSociale' => '', 'siret' => '', 'rcs' => '',
'numTva' => '', 'address' => '', 'address2' => '', 'zipCode' => '',
'city' => '', 'typeCompany' => '',
]);
$request->setMethod('POST');
$request->setSession(new Session(new MockArraySessionStorage()));
$controller = $this->createController($request);
$response = $controller->create(
$request,
$repo,
$this->createStub(\App\Repository\RevendeurRepository::class),
$this->createStub(EntityManagerInterface::class),
$this->createStub(MeilisearchService::class),
$userService,
$this->createStub(LoggerInterface::class),
$this->createStub(HttpClientInterface::class),
$this->createStub(MailerService::class),
$this->createStub(Environment::class),
'sk_test_***',
);
$this->assertSame(302, $response->getStatusCode());
}
private function buildCustomer(int $id = 1): Customer
{
$user = new User();
$user->setEmail('show@test.com');
$user->setFirstName('Show');
$user->setLastName('User');
$user->setPassword('h');
$customer = new Customer($user);
$ref = new \ReflectionProperty(Customer::class, 'id');
$ref->setAccessible(true);
$ref->setValue($customer, $id);
return $customer;
}
public function testShowReturnsResponse(): void
{
$customer = $this->buildCustomer();
$entityRepo = $this->createStub(\Doctrine\ORM\EntityRepository::class);
$entityRepo->method('findBy')->willReturn([]);
$entityRepo->method('count')->willReturn(0);
$entityRepo->method('findOneBy')->willReturn(null);
$entityRepo->method('find')->willReturn(null);
$em = $this->createStub(\Doctrine\ORM\EntityManagerInterface::class);
$em->method('getRepository')->willReturn($entityRepo);
$request = new Request();
$request->setMethod('GET');
$controller = $this->createController($request);
$response = $controller->show(
$customer,
$request,
$em,
$this->createStub(\App\Service\OvhService::class),
$this->createStub(\App\Service\CloudflareService::class),
$this->createStub(\App\Service\DnsCheckService::class),
$this->createStub(\App\Service\EsyMailService::class),
$this->createStub(\Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface::class),
$this->createStub(MailerService::class),
$this->createStub(Environment::class),
);
$this->assertInstanceOf(Response::class, $response);
}
public function testShowPostInfo(): void
{
$customer = $this->buildCustomer();
$entityRepo = $this->createStub(\Doctrine\ORM\EntityRepository::class);
$entityRepo->method('findBy')->willReturn([]);
$entityRepo->method('count')->willReturn(0);
$entityRepo->method('findOneBy')->willReturn(null);
$em = $this->createMock(\Doctrine\ORM\EntityManagerInterface::class);
$em->method('getRepository')->willReturn($entityRepo);
$em->expects($this->once())->method('flush');
$request = new Request(['tab' => 'info'], ['firstName' => 'Updated', 'lastName' => 'Name', 'email' => 'u@t.com']);
$request->setMethod('POST');
$request->setSession(new Session(new MockArraySessionStorage()));
$controller = $this->createController($request);
$response = $controller->show(
$customer,
$request,
$em,
$this->createStub(\App\Service\OvhService::class),
$this->createStub(\App\Service\CloudflareService::class),
$this->createStub(\App\Service\DnsCheckService::class),
$this->createStub(\App\Service\EsyMailService::class),
$this->createStub(\Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface::class),
$this->createStub(MailerService::class),
$this->createStub(Environment::class),
);
$this->assertSame(302, $response->getStatusCode());
}
public function testDeleteMarksForDeletion(): void
{
$customer = $this->buildCustomer();
$em = $this->createMock(\Doctrine\ORM\EntityManagerInterface::class);
$em->expects($this->once())->method('flush');
$request = new Request();
$request->setSession(new Session(new MockArraySessionStorage()));
$controller = $this->createController($request);
$response = $controller->delete($customer, $em);
$this->assertSame(302, $response->getStatusCode());
$this->assertTrue($customer->isPendingDelete());
}
public function testDeleteAlreadyPendingDelete(): void
{
$user = new User();
$user->setEmail('p@t.com');
$user->setFirstName('P');
$user->setLastName('D');
$user->setPassword('h');
$customer = new Customer($user);
$customer->setState(Customer::STATE_PENDING_DELETE);
$em = $this->createMock(\Doctrine\ORM\EntityManagerInterface::class);
$em->expects($this->never())->method('flush');
$request = new Request();
$request->setSession(new Session(new MockArraySessionStorage()));
$controller = $this->createController($request);
$response = $controller->delete($customer, $em);
$this->assertSame(302, $response->getStatusCode());
}
public function testResendWelcomeWithTempPassword(): void
{
$user = new User();
$user->setEmail('w@t.com');
$user->setFirstName('W');
$user->setLastName('T');
$user->setPassword('h');
$user->setTempPassword('temp123');
$customer = new Customer($user);
$ref = new \ReflectionProperty(Customer::class, 'id');
$ref->setAccessible(true);
$ref->setValue($customer, 5);
$mailer = $this->createStub(MailerService::class);
$twig = $this->createStub(Environment::class);
$twig->method('render')->willReturn('<html></html>');
$request = new Request();
$request->setSession(new Session(new MockArraySessionStorage()));
$controller = $this->createController($request);
$response = $controller->resendWelcome($customer, $mailer, $twig);
$this->assertSame(302, $response->getStatusCode());
}
public function testResendWelcomeWithoutTempPassword(): void
{
$user = new User();
$user->setEmail('w@t.com');
$user->setFirstName('W');
$user->setLastName('T');
$user->setPassword('h');
// No temp password set
$customer = new Customer($user);
$ref = new \ReflectionProperty(Customer::class, 'id');
$ref->setAccessible(true);
$ref->setValue($customer, 6);
$mailer = $this->createStub(MailerService::class);
$twig = $this->createStub(Environment::class);
$request = new Request();
$request->setSession(new Session(new MockArraySessionStorage()));
$controller = $this->createController($request);
$response = $controller->resendWelcome($customer, $mailer, $twig);
$this->assertSame(302, $response->getStatusCode());
}
public function testCreatePostMeilisearchError(): void
{
$user = new User();
$user->setEmail('c@d.com');
$user->setFirstName('C');
$user->setLastName('D');
$user->setPassword('h');
$userService = $this->createStub(UserManagementService::class);
$userService->method('createBaseUser')->willReturn($user);
$repo = $this->createStub(CustomerRepository::class);
$repo->method('generateUniqueCodeComptable')->willReturn('CLI-00003');
$meilisearch = $this->createStub(MeilisearchService::class);
$meilisearch->method('indexCustomer')->willThrowException(new \RuntimeException('Meili down'));
$request = new Request([], [
'firstName' => 'C', 'lastName' => 'D', 'email' => 'c@d.com',
'phone' => '', 'raisonSociale' => '', 'siret' => '', 'rcs' => '',
'numTva' => '', 'address' => '', 'address2' => '', 'zipCode' => '',
'city' => '', 'typeCompany' => '',
]);
$request->setMethod('POST');
$request->setSession(new Session(new MockArraySessionStorage()));
$controller = $this->createController($request);
$response = $controller->create(
$request,
$repo,
$this->createStub(\App\Repository\RevendeurRepository::class),
$this->createStub(EntityManagerInterface::class),
$meilisearch,
$userService,
$this->createStub(LoggerInterface::class),
$this->createStub(HttpClientInterface::class),
$this->createStub(MailerService::class),
$this->createStub(Environment::class),
'',
);
$this->assertSame(302, $response->getStatusCode());
}
}