Files
crm_ecosplay/tests/Controller/Admin/TarificationControllerTest.php

232 lines
8.9 KiB
PHP
Raw Normal View History

<?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());
}
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
public function testPurgeWithNoPrices(): void
{
$repo = $this->createStub(PriceAutomaticRepository::class);
$repo->method('findAll')->willReturn([]);
$em = $this->createMock(EntityManagerInterface::class);
$em->expects($this->once())->method('flush');
$meilisearch = $this->createStub(MeilisearchService::class);
$controller = new TarificationController();
$controller->setContainer($this->createContainer());
$response = $controller->purge($repo, $em, $meilisearch, '');
$this->assertSame(302, $response->getStatusCode());
}
public function testPurgeWithPrices(): void
{
$price = $this->createPrice();
$repo = $this->createStub(PriceAutomaticRepository::class);
$repo->method('findAll')->willReturn([$price]);
$em = $this->createMock(EntityManagerInterface::class);
$em->expects($this->once())->method('remove')->with($price);
$em->expects($this->once())->method('flush');
$meilisearch = $this->createStub(MeilisearchService::class);
$controller = new TarificationController();
$controller->setContainer($this->createContainer());
$response = $controller->purge($repo, $em, $meilisearch, '');
$this->assertSame(302, $response->getStatusCode());
}
public function testPurgeMeilisearchError(): void
{
$price = $this->createPrice();
$repo = $this->createStub(PriceAutomaticRepository::class);
$repo->method('findAll')->willReturn([$price]);
$em = $this->createStub(EntityManagerInterface::class);
$meilisearch = $this->createStub(MeilisearchService::class);
$meilisearch->method('removePrice')->willThrowException(new \RuntimeException('Meili error'));
$controller = new TarificationController();
$controller->setContainer($this->createContainer());
// Should not throw, error is swallowed
$response = $controller->purge($repo, $em, $meilisearch, '');
$this->assertSame(302, $response->getStatusCode());
}
}