Files
e-ticket/tests/Command/ExpirePendingOrdersCommandTest.php
Serreau Jovann 61200adc74 Add stock management, order notifications, webhooks, expiration cron, and billet type validation
- Decrement billet quantity after purchase in BilletOrderService::generateOrderTickets
- Block purchase when stock is exhausted (quantity <= 0) in OrderController::buildOrderItems
- Add organizer email notification on new order (order_notification_orga template)
- Add organizer email notification on cancel/refund (order_cancelled_orga template)
- Add ExpirePendingOrdersCommand (app:orders:expire-pending) cron every 5min via Ansible
  - Cancels pending orders older than 30 minutes, restores stock, invalidates tickets
  - Includes BilletBuyerRepository::findExpiredPending query method
  - 3 unit tests covering: no expired orders, stock restoration, unlimited billets
- Add payment_intent.payment_failed webhook: cancels order, logs audit, emails buyer
- Add charge.refunded webhook: sets order to refunded, invalidates tickets, notifies orga and buyer
- Validate billet type (billet/reservation_brocante/vote) against organizer offer
  - getAllowedBilletTypes: gratuit=billet only, basic/sur-mesure=all types
  - Server-side validation in hydrateBilletFromRequest, UI filtering in templates
- Update TASK_CHECKUP.md: all Billetterie & Commandes items now complete

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-23 00:12:30 +01:00

124 lines
4.5 KiB
PHP

<?php
namespace App\Tests\Command;
use App\Command\ExpirePendingOrdersCommand;
use App\Entity\Billet;
use App\Entity\BilletBuyer;
use App\Entity\BilletBuyerItem;
use App\Entity\BilletOrder;
use App\Entity\Event;
use App\Repository\BilletBuyerRepository;
use App\Service\AuditService;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\EntityRepository;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Tester\CommandTester;
class ExpirePendingOrdersCommandTest extends TestCase
{
private EntityManagerInterface $em;
private BilletBuyerRepository $buyerRepo;
private AuditService $audit;
private CommandTester $tester;
protected function setUp(): void
{
$this->em = $this->createMock(EntityManagerInterface::class);
$this->buyerRepo = $this->createMock(BilletBuyerRepository::class);
$this->audit = $this->createMock(AuditService::class);
$command = new ExpirePendingOrdersCommand($this->em, $this->buyerRepo, $this->audit);
$app = new Application();
$app->addCommand($command);
$this->tester = new CommandTester($app->find('app:orders:expire-pending'));
}
public function testNoExpiredOrders(): void
{
$this->buyerRepo->method('findExpiredPending')->willReturn([]);
$this->em->expects($this->never())->method('flush');
$this->tester->execute([]);
$this->assertStringContainsString('No expired pending orders', $this->tester->getDisplay());
}
public function testExpiresOldPendingOrders(): void
{
$event = $this->createMock(Event::class);
$event->method('getTitle')->willReturn('Test Event');
$billet = $this->createMock(Billet::class);
$billet->method('getQuantity')->willReturn(5);
$item = $this->createMock(BilletBuyerItem::class);
$item->method('getBillet')->willReturn($billet);
$item->method('getQuantity')->willReturn(2);
$order = $this->createMock(BilletBuyer::class);
$order->method('getId')->willReturn(1);
$order->method('getOrderNumber')->willReturn('2026-03-23-1');
$order->method('getEvent')->willReturn($event);
$order->method('getItems')->willReturn(new ArrayCollection([$item]));
$order->expects($this->once())->method('setStatus')->with('cancelled');
$billet->expects($this->once())->method('setQuantity')->with(7);
$this->buyerRepo->method('findExpiredPending')->willReturn([$order]);
$ticket = $this->createMock(BilletOrder::class);
$ticket->expects($this->once())->method('setState')->with(BilletOrder::STATE_INVALID);
$ticketRepo = $this->createMock(EntityRepository::class);
$ticketRepo->method('findBy')->willReturn([$ticket]);
$this->em->method('getRepository')
->with(BilletOrder::class)
->willReturn($ticketRepo);
$this->em->expects($this->once())->method('flush');
$this->audit->expects($this->once())
->method('log')
->with('order_expired', 'BilletBuyer', 1, $this->anything());
$this->tester->execute([]);
$this->assertStringContainsString('1 pending order(s) expired', $this->tester->getDisplay());
}
public function testExpiresOrderWithUnlimitedBillet(): void
{
$event = $this->createMock(Event::class);
$event->method('getTitle')->willReturn('Test');
$billet = $this->createMock(Billet::class);
$billet->method('getQuantity')->willReturn(null);
$item = $this->createMock(BilletBuyerItem::class);
$item->method('getBillet')->willReturn($billet);
$item->method('getQuantity')->willReturn(1);
$order = $this->createMock(BilletBuyer::class);
$order->method('getId')->willReturn(2);
$order->method('getOrderNumber')->willReturn('2026-03-23-2');
$order->method('getEvent')->willReturn($event);
$order->method('getItems')->willReturn(new ArrayCollection([$item]));
$billet->expects($this->never())->method('setQuantity');
$this->buyerRepo->method('findExpiredPending')->willReturn([$order]);
$ticketRepo = $this->createMock(EntityRepository::class);
$ticketRepo->method('findBy')->willReturn([]);
$this->em->method('getRepository')
->with(BilletOrder::class)
->willReturn($ticketRepo);
$this->tester->execute([]);
$this->assertStringContainsString('1 pending order(s) expired', $this->tester->getDisplay());
}
}