em = $container->get(EntityManagerInterface::class); /** @var \PDO $pdo */ $pdo = $this->em->getConnection()->getNativeConnection(); $principalBackend = new PrincipalBackend($pdo); $this->cardBackend = new CardDAV\Backend\PDO($pdo); $this->server = new DAV\Server([ new CardDAV\AddressBookRoot($principalBackend, $this->cardBackend), ]); $this->server->setBaseUri('/dav/'); $this->server->addPlugin(new CardDAV\Plugin()); $this->server->addPlugin(new BirthdayCalendarPlugin( $container->get(BirthdayService::class), new CalendarBackend($pdo) )); } private function addressBook(): AddressBook { $addressBook = $this->em->getRepository(AddressBook::class)->findOneBy([ 'principalUri' => self::PRINCIPAL, 'uri' => 'default', ]); $this->assertNotNull($addressBook, 'The fixtures provide a default address book'); return $addressBook; } private function includeInBirthdayCalendar(bool $included): void { $this->addressBook()->setIncludedInBirthdayCalendar($included); $this->em->flush(); $this->em->clear(); } private function birthdayEvent(): ?CalendarObject { $this->em->clear(); return $this->em->getRepository(CalendarObject::class)->findOneBy(['uri' => 'default-jane.vcf.ics']); } /** * Emits the event sabre emits after a `PUT` creates a card, which is what the plugin listens to. */ private function writeCardThroughDav(): void { $path = 'addressbooks/test_user/default/jane.vcf'; $this->cardBackend->createCard($this->addressBook()->getId(), 'jane.vcf', self::CARD); $this->server->emit('afterCreateFile', [$path, $this->server->tree->getNodeForPath(\dirname($path))]); } public function testWritingACardCreatesTheBirthdayEvent(): void { $this->includeInBirthdayCalendar(true); $this->writeCardThroughDav(); $event = $this->birthdayEvent(); $this->assertNotNull($event, 'The plugin should have produced a birthday event'); $this->assertStringContainsString('Jane Doe', $event->getCalendarData()); } public function testNothingHappensWhenTheAddressBookOptedOut(): void { $this->includeInBirthdayCalendar(false); $this->writeCardThroughDav(); $this->assertNull($this->birthdayEvent()); } public function testDeletingACardRemovesItsBirthdayEvent(): void { $this->includeInBirthdayCalendar(true); $this->writeCardThroughDav(); $this->assertNotNull($this->birthdayEvent()); $this->server->emit('beforeUnbind', ['addressbooks/test_user/default/jane.vcf']); $this->assertNull($this->birthdayEvent(), 'The birthday event should go with the card'); } /** * The hooks fire for every write in the tree, not just cards. */ public function testANonCardWriteIsIgnored(): void { $this->includeInBirthdayCalendar(true); $this->server->emit('afterCreateFile', [ 'addressbooks/test_user/default', $this->server->tree->getNodeForPath('addressbooks/test_user'), ]); $this->assertNull($this->birthdayEvent()); } }