createMock(ApiClient::class); $mailjet->expects(self::once())->method('createContactsList')->with('LPJMP281126 nur Bus')->willReturn(42); $mailjet->expects(self::once())->method('addContactsToList')->with(42, [ ['email' => 'mia@example.com', 'firstName' => 'Mia'], ['email' => 'tom@example.com', 'firstName' => null], ])->willReturn(555); $entityManager = $this->createMock(EntityManagerInterface::class); $entityManager->expects(self::once())->method('persist')->with(self::isInstanceOf(TravelMailingList::class)); $entityManager->expects(self::never())->method('remove'); $mailingList = (new TravelMailingListManager($mailjet, $entityManager, new NullLogger()))->create('LPJMP281126', [ new TravelMailingRecipient('Mia@Example.com', 'Mia', 'Muster'), new TravelMailingRecipient('mia@example.com', 'Lea', 'Muster'), new TravelMailingRecipient('tom@example.com', '', 'Test'), ], BusLeg::Outbound, new \DateTimeImmutable('2026-11-28')); self::assertSame(42, $mailingList->getMailjetListId()); self::assertSame(BusLeg::Outbound, $mailingList->getBusLeg()); self::assertSame('LPJMP281126 nur Bus', $mailingList->getName()); self::assertEquals(new \DateTimeImmutable('2026-12-28'), $mailingList->getDeleteAfter()); } public function testCreateDiscardsTheListWhenTheContactsCannotBeAdded(): void { $mailjet = $this->createMock(ApiClient::class); $mailjet->method('createContactsList')->willReturn(42); $mailjet->method('addContactsToList')->willThrowException(new NewsletterProviderException('boom')); $mailjet->expects(self::once())->method('deleteContactsList')->with(42); $entityManager = $this->createMock(EntityManagerInterface::class); $entityManager->expects(self::once())->method('remove')->with(self::isInstanceOf(TravelMailingList::class)); $this->expectException(NewsletterProviderException::class); (new TravelMailingListManager($mailjet, $entityManager, new NullLogger()))->create('LPJMP281126', [ new TravelMailingRecipient('mia@example.com', 'Mia', 'Muster'), ], null, new \DateTimeImmutable('2026-11-28')); } public function testDeleteRemovesOnlyContactsOnNoOtherList(): void { $mailjet = $this->createMock(ApiClient::class); $mailjet->method('getListContactIds')->with(42)->willReturn([1, 2]); $mailjet->method('getContactListIds')->willReturnMap([ [1, [42]], [2, [42, 10321569]], ]); $mailjet->expects(self::once())->method('deleteContact')->with(1); $mailjet->expects(self::once())->method('deleteContactsList')->with(42); $entityManager = $this->createMock(EntityManagerInterface::class); $entityManager->expects(self::once())->method('remove'); $deleted = (new TravelMailingListManager($mailjet, $entityManager, new NullLogger())) ->delete([new TravelMailingList('LPJMP281126', 42, null, new \DateTimeImmutable('2026-12-28'))]); self::assertSame(1, $deleted); } public function testDeleteTreatsTheTravelsOtherListAsItsOwn(): void { // 1 is on both lists of the travel, 2 also on a newsletter list. $mailjet = $this->createMock(ApiClient::class); $mailjet->method('getListContactIds')->willReturnMap([ [42, [1, 2]], [43, [1]], ]); $mailjet->method('getContactListIds')->willReturnMap([ [1, [42, 43]], [2, [42, 10321569]], ]); $mailjet->expects(self::once())->method('deleteContact')->with(1); $mailjet->expects(self::exactly(2))->method('deleteContactsList'); $entityManager = $this->createMock(EntityManagerInterface::class); $entityManager->expects(self::exactly(2))->method('remove'); $deleted = (new TravelMailingListManager($mailjet, $entityManager, new NullLogger()))->delete([ new TravelMailingList('LPJMP281126', 42, null, new \DateTimeImmutable('2026-12-28')), new TravelMailingList('LPJMP281126', 43, BusLeg::Outbound, new \DateTimeImmutable('2026-12-28')), ]); self::assertSame(1, $deleted); } public function testDeleteKeepsTheRowWhenMailjetFails(): void { $mailjet = $this->createMock(ApiClient::class); $mailjet->method('getListContactIds')->willReturn([]); $mailjet->expects(self::once())->method('deleteContactsList')->willThrowException(new NewsletterProviderException('boom')); $entityManager = $this->createMock(EntityManagerInterface::class); $entityManager->expects(self::never())->method('remove'); $this->expectException(NewsletterProviderException::class); (new TravelMailingListManager($mailjet, $entityManager, new NullLogger())) ->delete([new TravelMailingList('LPJMP281126', 42, null, new \DateTimeImmutable('2026-12-28'))]); } }