test: couverture 100% OrderNumberController et TarificationController

OrderNumberControllerTest (8 tests) :
- testIndex : preview + queryBuilder retourne 200
- testUpdateInvalidFormat : format non MM/YYYY-XXXXX redirige avec erreur
- testUpdateEmptyNumber : numéro vide redirige avec erreur
- testUpdateNumberAlreadyExists : numéro existant redirige avec erreur
- testUpdateNumberTooLow : 00000 (previousNum < 0) redirige avec erreur
- testUpdateSuccess : numéro valide, placeholder créé, flash success
- testUpdateSuccessFirstNumber : 00001 (previousNum=0, pas de placeholder)
- testUpdatePreviousAlreadyExists : previous existe déjà, pas de persist

TarificationControllerTest (6 tests) :
- testIndexNoCreated : aucun tarif créé, retourne 200
- testIndexWithCreated : 2 tarifs créés, flash success pour chaque
- testEditNotFound : tarif null lance NotFoundHttpException
- testEditSuccessStripeOk : mise à jour champs + Stripe sync OK
- testEditStripeError : erreur Stripe, flash error + flash success fallback
- testEditMeilisearchError : erreur Meilisearch, flash error

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Serreau Jovann
2026-04-03 11:07:13 +02:00
parent f0a5fdc849
commit 22cfefc900
2 changed files with 359 additions and 0 deletions

View File

@@ -0,0 +1,185 @@
<?php
namespace App\Tests\Controller\Admin;
use App\Controller\Admin\OrderNumberController;
use App\Entity\OrderNumber;
use App\Repository\OrderNumberRepository;
use App\Service\OrderNumberService;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Query;
use Doctrine\ORM\QueryBuilder;
use PHPUnit\Framework\TestCase;
use Psr\Container\ContainerInterface;
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 OrderNumberControllerTest 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/numerotation');
$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 createRepoWithQueryBuilder(): OrderNumberRepository
{
$query = $this->createStub(Query::class);
$query->method('getResult')->willReturn([]);
$qb = $this->createStub(QueryBuilder::class);
$qb->method('orderBy')->willReturnSelf();
$qb->method('setMaxResults')->willReturnSelf();
$qb->method('getQuery')->willReturn($query);
$repo = $this->createStub(OrderNumberRepository::class);
$repo->method('createQueryBuilder')->willReturn($qb);
return $repo;
}
public function testIndex(): void
{
$orderService = $this->createStub(OrderNumberService::class);
$orderService->method('preview')->willReturn('04/2026-00001');
$controller = new OrderNumberController();
$controller->setContainer($this->createContainer());
$response = $controller->index($orderService, $this->createRepoWithQueryBuilder());
$this->assertSame(200, $response->getStatusCode());
}
public function testUpdateInvalidFormat(): void
{
$request = new Request([], ['next_number' => 'invalid']);
$request->setMethod('POST');
$controller = new OrderNumberController();
$controller->setContainer($this->createContainer());
$response = $controller->update($request, $this->createStub(OrderNumberRepository::class), $this->createStub(EntityManagerInterface::class));
$this->assertSame(302, $response->getStatusCode());
}
public function testUpdateEmptyNumber(): void
{
$request = new Request([], ['next_number' => '']);
$request->setMethod('POST');
$controller = new OrderNumberController();
$controller->setContainer($this->createContainer());
$response = $controller->update($request, $this->createStub(OrderNumberRepository::class), $this->createStub(EntityManagerInterface::class));
$this->assertSame(302, $response->getStatusCode());
}
public function testUpdateNumberAlreadyExists(): void
{
$repo = $this->createStub(OrderNumberRepository::class);
$repo->method('findOneBy')->willReturn(new OrderNumber('04/2026-00042'));
$request = new Request([], ['next_number' => '04/2026-00042']);
$request->setMethod('POST');
$controller = new OrderNumberController();
$controller->setContainer($this->createContainer());
$response = $controller->update($request, $repo, $this->createStub(EntityManagerInterface::class));
$this->assertSame(302, $response->getStatusCode());
}
public function testUpdateNumberTooLow(): void
{
$repo = $this->createStub(OrderNumberRepository::class);
$repo->method('findOneBy')->willReturn(null);
$request = new Request([], ['next_number' => '04/2026-00000']);
$request->setMethod('POST');
$controller = new OrderNumberController();
$controller->setContainer($this->createContainer());
$response = $controller->update($request, $repo, $this->createStub(EntityManagerInterface::class));
$this->assertSame(302, $response->getStatusCode());
}
public function testUpdateSuccess(): void
{
$repo = $this->createStub(OrderNumberRepository::class);
$repo->method('findOneBy')->willReturn(null);
$request = new Request([], ['next_number' => '04/2026-00042']);
$request->setMethod('POST');
$controller = new OrderNumberController();
$controller->setContainer($this->createContainer());
$response = $controller->update($request, $repo, $this->createStub(EntityManagerInterface::class));
$this->assertSame(302, $response->getStatusCode());
}
public function testUpdateSuccessFirstNumber(): void
{
$repo = $this->createStub(OrderNumberRepository::class);
$repo->method('findOneBy')->willReturn(null);
$request = new Request([], ['next_number' => '04/2026-00001']);
$request->setMethod('POST');
$controller = new OrderNumberController();
$controller->setContainer($this->createContainer());
$response = $controller->update($request, $repo, $this->createStub(EntityManagerInterface::class));
$this->assertSame(302, $response->getStatusCode());
}
public function testUpdatePreviousAlreadyExists(): void
{
$callCount = 0;
$repo = $this->createStub(OrderNumberRepository::class);
$repo->method('findOneBy')->willReturnCallback(function (array $criteria) use (&$callCount) {
++$callCount;
if (1 === $callCount) {
return null; // newNumber not found
}
return new OrderNumber('04/2026-00041'); // previousNumOrder exists
});
$request = new Request([], ['next_number' => '04/2026-00042']);
$request->setMethod('POST');
$controller = new OrderNumberController();
$controller->setContainer($this->createContainer());
$response = $controller->update($request, $repo, $this->createStub(EntityManagerInterface::class));
$this->assertSame(302, $response->getStatusCode());
}
}

View File

@@ -0,0 +1,174 @@
<?php
namespace App\Tests\Controller\Admin;
use App\Controller\Admin\TarificationController;
use App\Entity\PriceAutomatic;
use App\Repository\PriceAutomaticRepository;
use App\Service\MeilisearchService;
use App\Service\StripePriceService;
use App\Service\TarificationService;
use Doctrine\ORM\EntityManagerInterface;
use PHPUnit\Framework\TestCase;
use Psr\Container\ContainerInterface;
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\HttpKernel\Exception\NotFoundHttpException;
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 TarificationControllerTest 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/tarification');
$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 createPrice(): PriceAutomatic
{
$p = new PriceAutomatic();
$p->setType('esyweb_business');
$p->setTitle('Esy-Web Business');
$p->setPriceHt('500.00');
$p->setMonthPrice('100.00');
$p->setPeriod(1);
return $p;
}
public function testIndexNoCreated(): void
{
$tarification = $this->createStub(TarificationService::class);
$tarification->method('ensureDefaultPrices')->willReturn([]);
$tarification->method('getAll')->willReturn([]);
$controller = new TarificationController();
$controller->setContainer($this->createContainer());
$response = $controller->index($tarification);
$this->assertSame(200, $response->getStatusCode());
}
public function testIndexWithCreated(): void
{
$tarification = $this->createStub(TarificationService::class);
$tarification->method('ensureDefaultPrices')->willReturn(['esyweb_business', 'esymail']);
$tarification->method('getAll')->willReturn([$this->createPrice()]);
$controller = new TarificationController();
$controller->setContainer($this->createContainer());
$response = $controller->index($tarification);
$this->assertSame(200, $response->getStatusCode());
}
public function testEditNotFound(): void
{
$repo = $this->createStub(PriceAutomaticRepository::class);
$repo->method('find')->willReturn(null);
$controller = new TarificationController();
$controller->setContainer($this->createContainer());
$this->expectException(NotFoundHttpException::class);
$controller->edit(999, new Request(), $repo, $this->createStub(EntityManagerInterface::class), $this->createStub(StripePriceService::class), $this->createStub(MeilisearchService::class));
}
public function testEditSuccessStripeOk(): void
{
$price = $this->createPrice();
$repo = $this->createStub(PriceAutomaticRepository::class);
$repo->method('find')->willReturn($price);
$request = new Request([], [
'title' => 'Updated Title',
'description' => 'New desc',
'priceHt' => '600.00',
'monthPrice' => '120.00',
'period' => '3',
'stripeId' => 'price_abc',
'stripeAbonnementId' => 'sub_xyz',
]);
$request->setMethod('POST');
$controller = new TarificationController();
$controller->setContainer($this->createContainer());
$response = $controller->edit(1, $request, $repo, $this->createStub(EntityManagerInterface::class), $this->createStub(StripePriceService::class), $this->createStub(MeilisearchService::class));
$this->assertSame(302, $response->getStatusCode());
$this->assertSame('Updated Title', $price->getTitle());
$this->assertSame('600.00', $price->getPriceHt());
}
public function testEditStripeError(): void
{
$price = $this->createPrice();
$repo = $this->createStub(PriceAutomaticRepository::class);
$repo->method('find')->willReturn($price);
$stripe = $this->createStub(StripePriceService::class);
$stripe->method('syncPrice')->willThrowException(new \RuntimeException('Stripe down'));
$request = new Request([], [
'title' => 'T', 'description' => '', 'priceHt' => '1.00',
'monthPrice' => '0.00', 'period' => '1', 'stripeId' => '', 'stripeAbonnementId' => '',
]);
$request->setMethod('POST');
$controller = new TarificationController();
$controller->setContainer($this->createContainer());
$response = $controller->edit(1, $request, $repo, $this->createStub(EntityManagerInterface::class), $stripe, $this->createStub(MeilisearchService::class));
$this->assertSame(302, $response->getStatusCode());
}
public function testEditMeilisearchError(): void
{
$price = $this->createPrice();
$repo = $this->createStub(PriceAutomaticRepository::class);
$repo->method('find')->willReturn($price);
$meilisearch = $this->createStub(MeilisearchService::class);
$meilisearch->method('indexPrice')->willThrowException(new \RuntimeException('Meili down'));
$request = new Request([], [
'title' => 'T', 'description' => '', 'priceHt' => '1.00',
'monthPrice' => '0.00', 'period' => '1', 'stripeId' => '', 'stripeAbonnementId' => '',
]);
$request->setMethod('POST');
$controller = new TarificationController();
$controller->setContainer($this->createContainer());
$response = $controller->edit(1, $request, $repo, $this->createStub(EntityManagerInterface::class), $this->createStub(StripePriceService::class), $meilisearch);
$this->assertSame(302, $response->getStatusCode());
}
}