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

373 lines
17 KiB
PHP

<?php
namespace App\Tests\Controller\Admin;
use App\Controller\Admin\MembresController;
use App\Entity\User;
use App\Repository\UserRepository;
use App\Service\KeycloakAdminService;
use App\Service\MailerService;
use Doctrine\ORM\EntityManagerInterface;
use PHPUnit\Framework\TestCase;
use Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface;
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\PasswordHasher\Hasher\UserPasswordHasherInterface;
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 MembresControllerTest 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/membres');
$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 createKeycloak(array $users = [], array $groups = []): KeycloakAdminService
{
$kc = $this->createStub(KeycloakAdminService::class);
$kc->method('ensureRequiredGroups')->willReturn([]);
$kc->method('listUsers')->willReturn($users);
$kc->method('getUserGroups')->willReturn([]);
$kc->method('listGroups')->willReturn($groups);
return $kc;
}
public function testIndexEmpty(): void
{
$controller = new MembresController($this->createStub(LoggerInterface::class));
$controller->setContainer($this->createContainer());
$response = $controller->index($this->createKeycloak(), $this->createStub(UserRepository::class));
$this->assertSame(200, $response->getStatusCode());
}
public function testIndexWithUsers(): void
{
$users = [
['id' => 'kc-1', 'email' => 'a@test.com', 'firstName' => 'A', 'lastName' => 'B', 'enabled' => true, 'emailVerified' => true, 'createdTimestamp' => 1000],
];
$userRepo = $this->createStub(UserRepository::class);
$userRepo->method('findOneBy')->willReturn(null);
$controller = new MembresController($this->createStub(LoggerInterface::class));
$controller->setContainer($this->createContainer());
$response = $controller->index($this->createKeycloak($users, [['name' => 'siteconseil_member']]), $userRepo);
$this->assertSame(200, $response->getStatusCode());
}
public function testIndexWithLocalUser(): void
{
$users = [['id' => 'kc-2', 'email' => 'local@test.com', 'firstName' => 'L', 'lastName' => 'U', 'enabled' => true, 'emailVerified' => false, 'createdTimestamp' => 2000]];
$localUser = new User();
$localUser->setEmail('local@test.com');
$localUser->setFirstName('L');
$localUser->setLastName('U');
$localUser->setPassword('h');
$userRepo = $this->createStub(UserRepository::class);
$userRepo->method('findOneBy')->willReturn($localUser);
$controller = new MembresController($this->createStub(LoggerInterface::class));
$controller->setContainer($this->createContainer());
$response = $controller->index($this->createKeycloak($users), $userRepo);
$this->assertSame(200, $response->getStatusCode());
}
public function testIndexWithCreatedGroups(): void
{
$kc = $this->createStub(KeycloakAdminService::class);
$kc->method('ensureRequiredGroups')->willReturn(['siteconseil_member', 'siteconseil_admin']);
$kc->method('listUsers')->willReturn([]);
$kc->method('listGroups')->willReturn([]);
$controller = new MembresController($this->createStub(LoggerInterface::class));
$controller->setContainer($this->createContainer());
$response = $controller->index($kc, $this->createStub(UserRepository::class));
$this->assertSame(200, $response->getStatusCode());
}
public function testIndexKeycloakError(): void
{
$kc = $this->createStub(KeycloakAdminService::class);
$kc->method('ensureRequiredGroups')->willThrowException(new \RuntimeException('KC down'));
$controller = new MembresController($this->createStub(LoggerInterface::class));
$controller->setContainer($this->createContainer());
$response = $controller->index($kc, $this->createStub(UserRepository::class));
$this->assertSame(200, $response->getStatusCode());
}
public function testIndexGetGroupsError(): void
{
$users = [['id' => 'kc-3', 'email' => 'err@test.com', 'firstName' => 'E', 'lastName' => 'R']];
$kc = $this->createStub(KeycloakAdminService::class);
$kc->method('ensureRequiredGroups')->willReturn([]);
$kc->method('listUsers')->willReturn($users);
$kc->method('getUserGroups')->willThrowException(new \RuntimeException('Groups error'));
$kc->method('listGroups')->willReturn([]);
$controller = new MembresController($this->createStub(LoggerInterface::class));
$controller->setContainer($this->createContainer());
$response = $controller->index($kc, $this->createStub(UserRepository::class));
$this->assertSame(200, $response->getStatusCode());
}
public function testIndexListGroupsError(): void
{
$kc = $this->createStub(KeycloakAdminService::class);
$kc->method('ensureRequiredGroups')->willReturn([]);
$kc->method('listUsers')->willReturn([]);
$kc->method('listGroups')->willThrowException(new \RuntimeException('Groups list error'));
$controller = new MembresController($this->createStub(LoggerInterface::class));
$controller->setContainer($this->createContainer());
$response = $controller->index($kc, $this->createStub(UserRepository::class));
$this->assertSame(200, $response->getStatusCode());
}
public function testCreateEmptyFields(): void
{
$request = new Request([], ['firstName' => '', 'lastName' => '', 'email' => '', 'groups' => []]);
$request->setMethod('POST');
$controller = new MembresController($this->createStub(LoggerInterface::class));
$controller->setContainer($this->createContainer());
$response = $controller->create($request, $this->createStub(KeycloakAdminService::class), $this->createStub(UserRepository::class), $this->createStub(EntityManagerInterface::class), $this->createStub(UserPasswordHasherInterface::class), $this->createStub(MailerService::class), $this->createStub(Environment::class));
$this->assertSame(302, $response->getStatusCode());
}
public function testCreateEmailExists(): void
{
$existingUser = new User();
$existingUser->setEmail('exist@test.com');
$existingUser->setFirstName('E');
$existingUser->setLastName('X');
$existingUser->setPassword('h');
$userRepo = $this->createStub(UserRepository::class);
$userRepo->method('findOneBy')->willReturn($existingUser);
$request = new Request([], ['firstName' => 'A', 'lastName' => 'B', 'email' => 'exist@test.com', 'groups' => []]);
$request->setMethod('POST');
$controller = new MembresController($this->createStub(LoggerInterface::class));
$controller->setContainer($this->createContainer());
$response = $controller->create($request, $this->createStub(KeycloakAdminService::class), $userRepo, $this->createStub(EntityManagerInterface::class), $this->createStub(UserPasswordHasherInterface::class), $this->createStub(MailerService::class), $this->createStub(Environment::class));
$this->assertSame(302, $response->getStatusCode());
}
public function testCreateSuccess(): void
{
$kc = $this->createStub(KeycloakAdminService::class);
$kc->method('createUser')->willReturn(['created' => true, 'keycloakId' => 'kc-new', 'tempPassword' => 'tmp123']);
$userRepo = $this->createStub(UserRepository::class);
$userRepo->method('findOneBy')->willReturn(null);
$hasher = $this->createStub(UserPasswordHasherInterface::class);
$hasher->method('hashPassword')->willReturn('hashed');
$twig = $this->createStub(Environment::class);
$twig->method('render')->willReturn('<html>email</html>');
$request = new Request([], ['firstName' => 'Jean', 'lastName' => 'Membre', 'email' => 'jean@test.com', 'groups' => ['siteconseil_member', 'esy-web']]);
$request->setMethod('POST');
$controller = new MembresController($this->createStub(LoggerInterface::class));
$controller->setContainer($this->createContainer());
$response = $controller->create($request, $kc, $userRepo, $this->createStub(EntityManagerInterface::class), $hasher, $this->createStub(MailerService::class), $twig);
$this->assertSame(302, $response->getStatusCode());
}
public function testCreateSuccessAdmin(): void
{
$kc = $this->createStub(KeycloakAdminService::class);
$kc->method('createUser')->willReturn(['created' => true, 'keycloakId' => 'kc-admin', 'tempPassword' => 'tmp456']);
$userRepo = $this->createStub(UserRepository::class);
$userRepo->method('findOneBy')->willReturn(null);
$hasher = $this->createStub(UserPasswordHasherInterface::class);
$hasher->method('hashPassword')->willReturn('hashed');
$request = new Request([], ['firstName' => 'Admin', 'lastName' => 'User', 'email' => 'admin@test.com', 'groups' => ['siteconseil_admin']]);
$request->setMethod('POST');
$controller = new MembresController($this->createStub(LoggerInterface::class));
$controller->setContainer($this->createContainer());
$response = $controller->create($request, $kc, $userRepo, $this->createStub(EntityManagerInterface::class), $hasher, $this->createStub(MailerService::class), $this->createStub(Environment::class));
$this->assertSame(302, $response->getStatusCode());
}
public function testCreateKeycloakFailed(): void
{
$kc = $this->createStub(KeycloakAdminService::class);
$kc->method('createUser')->willReturn(['created' => false, 'keycloakId' => null, 'tempPassword' => null]);
$userRepo = $this->createStub(UserRepository::class);
$userRepo->method('findOneBy')->willReturn(null);
$request = new Request([], ['firstName' => 'A', 'lastName' => 'B', 'email' => 'fail@test.com', 'groups' => []]);
$request->setMethod('POST');
$controller = new MembresController($this->createStub(LoggerInterface::class));
$controller->setContainer($this->createContainer());
$response = $controller->create($request, $kc, $userRepo, $this->createStub(EntityManagerInterface::class), $this->createStub(UserPasswordHasherInterface::class), $this->createStub(MailerService::class), $this->createStub(Environment::class));
$this->assertSame(302, $response->getStatusCode());
}
public function testCreateThrowable(): void
{
$kc = $this->createStub(KeycloakAdminService::class);
$kc->method('createUser')->willThrowException(new \RuntimeException('KC error'));
$userRepo = $this->createStub(UserRepository::class);
$userRepo->method('findOneBy')->willReturn(null);
$request = new Request([], ['firstName' => 'A', 'lastName' => 'B', 'email' => 'err@test.com', 'groups' => []]);
$request->setMethod('POST');
$controller = new MembresController($this->createStub(LoggerInterface::class));
$controller->setContainer($this->createContainer());
$response = $controller->create($request, $kc, $userRepo, $this->createStub(EntityManagerInterface::class), $this->createStub(UserPasswordHasherInterface::class), $this->createStub(MailerService::class), $this->createStub(Environment::class));
$this->assertSame(302, $response->getStatusCode());
}
public function testResendSuccess(): void
{
$user = new User();
$user->setEmail('resend@test.com');
$user->setFirstName('R');
$user->setLastName('S');
$user->setPassword('h');
$user->setTempPassword('tmp789');
$userRepo = $this->createStub(UserRepository::class);
$userRepo->method('find')->willReturn($user);
$twig = $this->createStub(Environment::class);
$twig->method('render')->willReturn('<html>resend</html>');
$controller = new MembresController($this->createStub(LoggerInterface::class));
$controller->setContainer($this->createContainer());
$response = $controller->resend(1, $userRepo, $this->createStub(MailerService::class), $twig);
$this->assertSame(302, $response->getStatusCode());
}
public function testResendUserNotFound(): void
{
$userRepo = $this->createStub(UserRepository::class);
$userRepo->method('find')->willReturn(null);
$controller = new MembresController($this->createStub(LoggerInterface::class));
$controller->setContainer($this->createContainer());
$response = $controller->resend(999, $userRepo, $this->createStub(MailerService::class), $this->createStub(Environment::class));
$this->assertSame(302, $response->getStatusCode());
}
public function testResendNoTempPassword(): void
{
$user = new User();
$user->setEmail('no-tmp@test.com');
$user->setFirstName('N');
$user->setLastName('T');
$user->setPassword('h');
$userRepo = $this->createStub(UserRepository::class);
$userRepo->method('find')->willReturn($user);
$controller = new MembresController($this->createStub(LoggerInterface::class));
$controller->setContainer($this->createContainer());
$response = $controller->resend(1, $userRepo, $this->createStub(MailerService::class), $this->createStub(Environment::class));
$this->assertSame(302, $response->getStatusCode());
}
public function testDeleteSuccess(): void
{
$localUser = new User();
$localUser->setEmail('del@test.com');
$localUser->setFirstName('D');
$localUser->setLastName('E');
$localUser->setPassword('h');
$userRepo = $this->createStub(UserRepository::class);
$userRepo->method('findOneBy')->willReturn($localUser);
$controller = new MembresController($this->createStub(LoggerInterface::class));
$controller->setContainer($this->createContainer());
$response = $controller->delete('kc-del', $this->createStub(KeycloakAdminService::class), $userRepo, $this->createStub(EntityManagerInterface::class));
$this->assertSame(302, $response->getStatusCode());
}
public function testDeleteNoLocalUser(): void
{
$userRepo = $this->createStub(UserRepository::class);
$userRepo->method('findOneBy')->willReturn(null);
$controller = new MembresController($this->createStub(LoggerInterface::class));
$controller->setContainer($this->createContainer());
$response = $controller->delete('kc-nope', $this->createStub(KeycloakAdminService::class), $userRepo, $this->createStub(EntityManagerInterface::class));
$this->assertSame(302, $response->getStatusCode());
}
public function testDeleteError(): void
{
$kc = $this->createStub(KeycloakAdminService::class);
$kc->method('deleteUser')->willThrowException(new \RuntimeException('KC delete error'));
$controller = new MembresController($this->createStub(LoggerInterface::class));
$controller->setContainer($this->createContainer());
$response = $controller->delete('kc-err', $kc, $this->createStub(UserRepository::class), $this->createStub(EntityManagerInterface::class));
$this->assertSame(302, $response->getStatusCode());
}
}