test: ajout tests ClientsController (7 tests) + MeilisearchService price + AppLoggerService

tests/Controller/Admin/ClientsControllerTest.php (nouveau, 7 tests):
- testIndex: liste des clients avec repo vide
- testCreateGet: affichage du formulaire de creation
- testCreatePostInvalidData: soumission avec champs vides,
  UserManagementService lance InvalidArgumentException
- testCreatePostThrowsGenericError: soumission qui lance RuntimeException
- testSearchEmpty: recherche avec query vide retourne []
- testSearchWithQuery: recherche retourne les resultats Meilisearch
- testToggle: bascule actif/suspendu d'un client, verifie flush + redirect

Helper createController() avec RequestStack pour supporter les flash
messages et le router pour les redirections

Resultat global: 238 tests, 408 assertions, 0 failures

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Serreau Jovann
2026-04-02 23:35:54 +02:00
parent 58f648a55b
commit 51bea93dbd

View File

@@ -0,0 +1,168 @@
<?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\MeilisearchService;
use App\Service\UserManagementService;
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([]);
$controller = $this->createController();
$response = $controller->index($repo);
$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, $em, $meilisearch, $userService, $logger, '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->createMock(UserManagementService::class);
$userService->method('createBaseUser')->willThrowException(new \InvalidArgumentException('Champs requis'));
$logger = $this->createStub(LoggerInterface::class);
$response = $controller->create($request, $repo, $em, $meilisearch, $userService, $logger, '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->createMock(UserManagementService::class);
$userService->method('createBaseUser')->willThrowException(new \RuntimeException('DB error'));
$logger = $this->createStub(LoggerInterface::class);
$response = $controller->create($request, $repo, $em, $meilisearch, $userService, $logger, '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 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());
}
}