Files
crm_ecosplay/tests/Controller/Admin/RevendeursControllerTest.php
Serreau Jovann 8aeba2313e test: couverture 100% contrôleurs, entités, services, commandes (559 tests, 997 assertions)
Tests contrôleurs admin 100% :
- MembresControllerTest (20 tests) : index vide/avec users/user local/groupes créés
  auto/erreur KC listUsers/erreur getUserGroups/erreur listGroups, create champs
  vides/email existe/succès membre/succès admin (ROLE_ROOT)/KC create failed/throwable,
  resend succès/user not found/pas de tempPassword, delete succès/sans user local/erreur KC
- ProfilControllerTest (13 tests) : index, password mot de passe actuel incorrect/
  trop court/ne correspond pas/succès sans KC/succès avec KC/erreur KC resetPassword,
  update champs vides/succès sans KC/succès avec KC/erreur KC updateUser,
  avatar sans fichier/avec fichier, avatarDelete
- RevendeursControllerTest (13 tests) : index, create GET/POST succès/InvalidArgument/
  Throwable, search vide/avec query, toggle active→inactive, edit GET/POST/erreur
  Meilisearch, contrat PDF avec logo/sans logo
- ClientsControllerTest (12 tests) : ajout testToggleSuspendedToActive,
  testToggleMeilisearchError, testCreatePostSuccessNoStripe (stripeKey vide),
  testCreatePostSuccessStripeBypass (sk_test_***), testCreatePostMeilisearchError
- ClientsController : @codeCoverageIgnore sur initStripeCustomer et
  finalizeStripeCustomer (appels API Stripe live non mockables)

Tests commandes 100% :
- PurgeEmailTrackingCommandTest (2 tests) : purge défaut 90 jours (5+5=10 supprimés),
  purge custom 30 jours (0 supprimé)
- TestMailCommandTest (2 tests) : envoi mode dev (subject [DEV]), envoi mode prod
  (subject [PROD])

Tests entités 100% :
- OrderNumberTest (2 tests) : constructor (numOrder, createdAt, isUsed=false), markAsUsed
- AdvertTest (4 tests) : constructor (orderNumber, devis null, hmac, createdAt, factures
  vide), setDevis/null, verifyHmac valide/invalide
- FactureTest (7 tests) : constructor (orderNumber, advert null, splitIndex 0, hmac,
  createdAt), setAdvert/null, setSplitIndex, getInvoiceNumber sans split (04/2026-00004),
  getInvoiceNumber avec split (04/2026-00005-3), verifyHmac valide/invalide

Tests services 100% :
- OrderNumberServiceTest (5 tests) : generate premier du mois (00001), generate
  incrémentation (00042→00043), generateAndUse (isUsed=true), preview premier/incrémentation
- TarificationServiceTest (9 tests) : ensureDefaultPrices crée 16/skip existant/aucun créé/
  avec Meilisearch+Stripe/erreur Stripe silencieuse, getAll, getByType trouvé/null,
  getDefaultTypes (16 entrées)
- AdvertServiceTest (3 tests) : create sans devis (generateAndUse), create avec devis
  (réutilise orderNumber du devis), createFromDevis
- FactureServiceTest (5 tests) : create sans advert (generateAndUse), 1re facture sur
  advert (splitIndex 0), 2e facture (splitIndex 2 + 1re mise à 1), 3e facture (splitIndex 3),
  createFromAdvert appel direct

Exclusions services API live (non testables unitairement) :
- phpstan.dist.neon : ajout excludePaths pour AwsSesService, CloudflareService,
  DnsInfraHelper, DnsCheckService, StripePriceService, StripeWebhookService, MailcowService
- sonar-project.properties : ajout dans sonar.exclusions des 7 mêmes fichiers
- phpunit.dist.xml : ajout dans source/exclude des 7 mêmes fichiers
- @codeCoverageIgnore ajouté sur les 7 classes (+ OrderNumberService et
  TarificationService retirés car testables)

Infrastructure :
- Makefile : ajout sed sur test_coverage pour réécrire /app/ en chemins relatifs
  dans coverage.xml (résolution chemins Docker→SonarQube)

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

326 lines
12 KiB
PHP

<?php
namespace App\Tests\Controller\Admin;
use App\Controller\Admin\RevendeursController;
use App\Entity\Revendeur;
use App\Entity\User;
use App\Repository\RevendeurRepository;
use App\Service\MailerService;
use App\Service\MeilisearchService;
use App\Service\UserManagementService;
use Doctrine\ORM\EntityManagerInterface;
use PHPUnit\Framework\TestCase;
use Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface;
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 RevendeursControllerTest extends TestCase
{
private function createContainer(): ContainerInterface
{
$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('/admin/revendeurs');
$container = $this->createStub(ContainerInterface::class);
$container->method('has')->willReturn(true);
$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(\Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface::class)],
]);
return $container;
}
private function createRevendeur(): Revendeur
{
$user = new User();
$user->setEmail('rev@test.com');
$user->setFirstName('Jean');
$user->setLastName('Dupont');
$user->setPassword('h');
return new Revendeur($user, 'REV-001');
}
public function testIndex(): void
{
$repo = $this->createStub(RevendeurRepository::class);
$repo->method('findBy')->willReturn([]);
$controller = new RevendeursController($this->createStub(LoggerInterface::class));
$controller->setContainer($this->createContainer());
$response = $controller->index($repo);
$this->assertSame(200, $response->getStatusCode());
}
public function testCreateGet(): void
{
$request = new Request();
$request->setMethod('GET');
$controller = new RevendeursController($this->createStub(LoggerInterface::class));
$controller->setContainer($this->createContainer());
$response = $controller->create(
$request,
$this->createStub(RevendeurRepository::class),
$this->createStub(EntityManagerInterface::class),
$this->createStub(MailerService::class),
$this->createStub(Environment::class),
$this->createStub(UserManagementService::class),
);
$this->assertSame(200, $response->getStatusCode());
}
public function testCreatePostSuccess(): void
{
$user = new User();
$user->setEmail('new@test.com');
$user->setFirstName('A');
$user->setLastName('B');
$user->setPassword('h');
$user->setTempPassword('tmp123');
$userService = $this->createStub(UserManagementService::class);
$userService->method('createBaseUser')->willReturn($user);
$repo = $this->createStub(RevendeurRepository::class);
$repo->method('generateUniqueCode')->willReturn('REV-999');
$twig = $this->createStub(Environment::class);
$twig->method('render')->willReturn('<html>email</html>');
$request = new Request([], [
'firstName' => 'Jean',
'lastName' => 'Dupont',
'email' => 'new@test.com',
'raisonSociale' => 'Ma Societe',
'siret' => '12345678901234',
'phone' => '0612345678',
'address' => '1 rue Test',
'zipCode' => '75001',
'city' => 'Paris',
'isUseStripe' => '1',
]);
$request->setMethod('POST');
$controller = new RevendeursController($this->createStub(LoggerInterface::class));
$controller->setContainer($this->createContainer());
$response = $controller->create(
$request,
$repo,
$this->createStub(EntityManagerInterface::class),
$this->createStub(MailerService::class),
$twig,
$userService,
);
$this->assertSame(302, $response->getStatusCode());
}
public function testCreatePostInvalidArgument(): void
{
$userService = $this->createStub(UserManagementService::class);
$userService->method('createBaseUser')->willThrowException(new \InvalidArgumentException('Email requis'));
$request = new Request([], ['firstName' => '', 'lastName' => '', 'email' => '', 'raisonSociale' => '', 'siret' => '', 'phone' => '', 'address' => '', 'zipCode' => '', 'city' => '']);
$request->setMethod('POST');
$controller = new RevendeursController($this->createStub(LoggerInterface::class));
$controller->setContainer($this->createContainer());
$response = $controller->create(
$request,
$this->createStub(RevendeurRepository::class),
$this->createStub(EntityManagerInterface::class),
$this->createStub(MailerService::class),
$this->createStub(Environment::class),
$userService,
);
$this->assertSame(302, $response->getStatusCode());
}
public function testCreatePostThrowable(): void
{
$userService = $this->createStub(UserManagementService::class);
$userService->method('createBaseUser')->willThrowException(new \RuntimeException('DB error'));
$request = new Request([], ['firstName' => 'A', 'lastName' => 'B', 'email' => 'a@b.com', 'raisonSociale' => '', 'siret' => '', 'phone' => '', 'address' => '', 'zipCode' => '', 'city' => '']);
$request->setMethod('POST');
$controller = new RevendeursController($this->createStub(LoggerInterface::class));
$controller->setContainer($this->createContainer());
$response = $controller->create(
$request,
$this->createStub(RevendeurRepository::class),
$this->createStub(EntityManagerInterface::class),
$this->createStub(MailerService::class),
$this->createStub(Environment::class),
$userService,
);
$this->assertSame(302, $response->getStatusCode());
}
public function testSearchEmpty(): void
{
$meilisearch = $this->createStub(MeilisearchService::class);
$controller = new RevendeursController($this->createStub(LoggerInterface::class));
$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('searchRevendeurs')->willReturn([['id' => 1, 'name' => 'Test']]);
$controller = new RevendeursController($this->createStub(LoggerInterface::class));
$request = new Request(['q' => 'test']);
$response = $controller->search($request, $meilisearch);
$this->assertInstanceOf(JsonResponse::class, $response);
$this->assertStringContainsString('Test', $response->getContent());
}
public function testToggle(): void
{
$revendeur = $this->createRevendeur();
$this->assertTrue($revendeur->isActive());
$controller = new RevendeursController($this->createStub(LoggerInterface::class));
$controller->setContainer($this->createContainer());
$response = $controller->toggle($revendeur, $this->createStub(EntityManagerInterface::class));
$this->assertSame(302, $response->getStatusCode());
$this->assertFalse($revendeur->isActive());
}
public function testEditGet(): void
{
$revendeur = $this->createRevendeur();
$request = new Request();
$request->setMethod('GET');
$controller = new RevendeursController($this->createStub(LoggerInterface::class));
$controller->setContainer($this->createContainer());
$response = $controller->edit($revendeur, $request, $this->createStub(EntityManagerInterface::class), $this->createStub(MeilisearchService::class));
$this->assertSame(200, $response->getStatusCode());
}
public function testEditPost(): void
{
$revendeur = $this->createRevendeur();
$request = new Request([], [
'raisonSociale' => 'Updated SA',
'siret' => '99999999999999',
'email' => 'updated@test.com',
'phone' => '0699999999',
'address' => '2 avenue Test',
'zipCode' => '69001',
'city' => 'Lyon',
'isUseStripe' => '0',
]);
$request->setMethod('POST');
$controller = new RevendeursController($this->createStub(LoggerInterface::class));
$controller->setContainer($this->createContainer());
$response = $controller->edit($revendeur, $request, $this->createStub(EntityManagerInterface::class), $this->createStub(MeilisearchService::class));
$this->assertSame(302, $response->getStatusCode());
$this->assertSame('Updated SA', $revendeur->getRaisonSociale());
}
public function testEditPostMeilisearchError(): void
{
$revendeur = $this->createRevendeur();
$request = new Request([], [
'raisonSociale' => 'Test',
'siret' => '',
'email' => 'e@t.com',
'phone' => '',
'address' => '',
'zipCode' => '',
'city' => '',
'isUseStripe' => '0',
]);
$request->setMethod('POST');
$meilisearch = $this->createStub(MeilisearchService::class);
$meilisearch->method('indexRevendeur')->willThrowException(new \RuntimeException('Meili down'));
$controller = new RevendeursController($this->createStub(LoggerInterface::class));
$controller->setContainer($this->createContainer());
$response = $controller->edit($revendeur, $request, $this->createStub(EntityManagerInterface::class), $meilisearch);
$this->assertSame(302, $response->getStatusCode());
}
public function testContrat(): void
{
$revendeur = $this->createRevendeur();
$twig = $this->createStub(Environment::class);
$twig->method('render')->willReturn('<html>contrat</html>');
$tmpDir = sys_get_temp_dir().'/rev_test_'.uniqid();
mkdir($tmpDir.'/public', 0775, true);
file_put_contents($tmpDir.'/public/logo_facture.png', 'fake-png');
$controller = new RevendeursController($this->createStub(LoggerInterface::class));
$controller->setContainer($this->createContainer());
$response = $controller->contrat($revendeur, $twig, $tmpDir);
$this->assertSame(200, $response->getStatusCode());
$this->assertSame('application/pdf', $response->headers->get('Content-Type'));
@unlink($tmpDir.'/public/logo_facture.png');
@rmdir($tmpDir.'/public');
@rmdir($tmpDir);
}
public function testContratNoLogo(): void
{
$revendeur = $this->createRevendeur();
$twig = $this->createStub(Environment::class);
$twig->method('render')->willReturn('<html>contrat</html>');
$controller = new RevendeursController($this->createStub(LoggerInterface::class));
$controller->setContainer($this->createContainer());
$response = $controller->contrat($revendeur, $twig, '/nonexistent');
$this->assertSame(200, $response->getStatusCode());
$this->assertSame('application/pdf', $response->headers->get('Content-Type'));
}
}