Files
ludikevent_crm/tests/Command/CleanCommandTest.php

108 lines
3.6 KiB
PHP
Raw Normal View History

<?php
namespace App\Tests\Command;
use App\Command\CleanCommand;
use App\Entity\CustomerTracking;
use App\Entity\SitePerformance;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Query;
use Doctrine\ORM\QueryBuilder;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Tester\CommandTester;
class CleanCommandTest extends TestCase
{
private MockObject&EntityManagerInterface $entityManager;
private MockObject&QueryBuilder $qb1;
private MockObject&QueryBuilder $qb2;
private MockObject&Query $query1;
private MockObject&Query $query2;
protected function setUp(): void
{
$this->entityManager = $this->createMock(EntityManagerInterface::class);
$this->qb1 = $this->createMock(QueryBuilder::class);
$this->qb2 = $this->createMock(QueryBuilder::class);
$this->query1 = $this->createMock(Query::class);
$this->query2 = $this->createMock(Query::class);
}
public function testExecute()
{
// Configure EntityManager to return two different QueryBuilders in sequence
$this->entityManager->expects($this->exactly(2))
->method('createQueryBuilder')
->willReturnOnConsecutiveCalls($this->qb1, $this->qb2);
// --- Sequence 1: SitePerformance ---
$this->qb1->expects($this->once())
->method('delete')
->with(SitePerformance::class, 'p')
->willReturnSelf();
$this->qb1->expects($this->once())
->method('where')
->with('p.createdAt <= :limit')
->willReturnSelf();
$this->qb1->expects($this->once())
->method('setParameter')
->with('limit', $this->isInstanceOf(\DateTime::class))
->willReturnSelf();
$this->qb1->expects($this->once())
->method('getQuery')
->willReturn($this->query1);
$this->query1->expects($this->once())
->method('execute')
->willReturn(5); // Simulate 5 records deleted
// --- Sequence 2: CustomerTracking ---
$this->qb2->expects($this->once())
->method('delete')
->with(CustomerTracking::class, 't')
->willReturnSelf();
$this->qb2->expects($this->once())
->method('where')
->with('t.createAT <= :limit')
->willReturnSelf();
$this->qb2->expects($this->once())
->method('setParameter')
->with('limit', $this->isInstanceOf(\DateTime::class))
->willReturnSelf();
$this->qb2->expects($this->once())
->method('getQuery')
->willReturn($this->query2);
$this->query2->expects($this->once())
->method('execute')
->willReturn(10); // Simulate 10 records deleted
// Expect final flush
$this->entityManager->expects($this->once())
->method('flush');
// Instantiate and run Command
$command = new CleanCommand($this->entityManager);
$application = new Application();
$application->addCommand($command);
$commandTester = new CommandTester($application->find('app:clean'));
$commandTester->execute([]);
$output = $commandTester->getDisplay();
// Assertions on output
$this->assertStringContainsString('5 entrées de performance supprimées', $output);
$this->assertStringContainsString('10 entrées de tracking supprimées', $output);
$this->assertStringContainsString('Nettoyage terminé avec succès', $output);
}
}