em = self::getContainer()->get(EntityManagerInterface::class); $this->em->getConnection()->beginTransaction(); $this->command = new CommandTester((new Application(self::$kernel))->find('app:booking-events:prune')); } protected function tearDown(): void { $this->em->getConnection()->rollBack(); parent::tearDown(); } public function testEventsOlderThanAYearAreDeleted(): void { $old = $this->event('-13 months'); $recent = $this->event('-11 months'); $this->command->execute([]); $this->command->assertCommandIsSuccessful(); self::assertStringContainsString('Deleted 1 booking events', $this->command->getDisplay()); self::assertNull($this->em->find(BookingEvent::class, $old)); self::assertNotNull($this->em->find(BookingEvent::class, $recent)); } public function testTheRetentionCanBeShortened(): void { $event = $this->event('-2 months'); $this->command->execute(['--months' => '1']); $this->command->assertCommandIsSuccessful(); self::assertNull($this->em->find(BookingEvent::class, $event)); } public function testANonsensicalRetentionIsRefused(): void { $event = $this->event('-2 months'); self::assertSame(Command::INVALID, $this->command->execute(['--months' => '0'])); self::assertNotNull($this->em->find(BookingEvent::class, $event)); } private function event(string $when): int { $event = new BookingEvent(BookingEventType::BOOKING_CREATED, null, BookingEvent::SYSTEM_ACTOR, null, 'Tisch 1', ResourceType::DESK); $this->em->persist($event); $this->em->flush(); $this->em->getConnection()->executeStatement( 'UPDATE booking_event SET occurred_at = ? WHERE id = ?', [(new \DateTimeImmutable($when))->format('Y-m-d H:i:s'), $event->getId()], ); $this->em->clear(); return (int) $event->getId(); } }