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); } }