feat: add bus-only travel mailing lists

This commit is contained in:
fromme committed 2026-11-09 15:53:53 +01:00
1 parent f5894a6bef
commit 2705401393
15 files changed
+335 -60

No files matched your search

+38
View File
@@ -7,6 +7,7 @@ namespace App\Tests\BpnConnect;
use App\BpnConnect\Exception\BpnConnectException;
use App\BpnConnect\Model\TravelMailingRecipient;
use App\BpnConnect\TravelsClient;
use App\Enum\BusLeg;
use PHPUnit\Framework\TestCase;
use Psr\Log\NullLogger;
use Symfony\Component\HttpClient\MockHttpClient;
@@ -35,12 +36,49 @@ class TravelsClientTest extends TestCase
self::assertSame('https://bpn-connect.test/api/v1/travels/mailing?travelCode=LPJMP281126', $requests[0][1]);
self::assertContains('X-API-KEY: secret', $requests[0][2]['headers']);
self::assertSame('LPJMP281126', $response->travelCode);
self::assertNull($response->busLeg);
self::assertEquals([
new TravelMailingRecipient('[email protected]', 'Erika', 'Muster'),
new TravelMailingRecipient('[email protected]', 'Max', 'Muster'),
], $response->data);
}
public function testGetMailingRecipientsPassesTheBusLegAndAcceptsItsEcho(): void
{
$requests = [];
$http = new MockHttpClient(function (string $method, string $url) use (&$requests): MockResponse {
$requests[] = $url;
return $this->jsonResponse([
'meta' => ['travel_code' => 'LPJMP281126', 'bus_leg' => 'outbound', 'row_count' => 1, 'execution_time_ms' => 12],
'data' => [
['email' => '[email protected]', 'first_name' => 'Erika', 'last_name' => 'Muster'],
],
]);
});
$response = $this->client($http)->getMailingRecipients('LPJMP281126', BusLeg::Outbound);
self::assertSame(['https://bpn-connect.test/api/v1/travels/mailing?travelCode=LPJMP281126&busLeg=outbound'], $requests);
self::assertSame(BusLeg::Outbound, $response->busLeg);
self::assertCount(1, $response->data);
}
public function testABusLegTheServerDidNotApplyIsRejected(): void
{
// What a bpn-connect without the filter answers: everyone, and no bus_leg in meta.
$http = new MockHttpClient($this->jsonResponse([
'meta' => ['travel_code' => 'LPJMP281126', 'row_count' => 1, 'execution_time_ms' => 12],
'data' => [
['email' => '[email protected]', 'first_name' => 'Erika', 'last_name' => 'Muster'],
],
]));
$this->expectException(BpnConnectException::class);
$this->client($http)->getMailingRecipients('LPJMP281126', BusLeg::Outbound);
}
public function testAnUnknownTravelCodeYieldsAnEmptyList(): void
{
$http = new MockHttpClient($this->jsonResponse([
+34 -5
View File
@@ -6,6 +6,7 @@ namespace App\Tests\Service;
use App\BpnConnect\Model\TravelMailingRecipient;
use App\Entity\TravelMailingList;
use App\Enum\BusLeg;
use App\Exception\NewsletterProviderException;
use App\Mailjet\ApiClient;
use App\Service\TravelMailingListManager;
@@ -18,7 +19,7 @@ class TravelMailingListManagerTest extends TestCase
public function testCreateTracksTheListAndAddsDeduplicatedContacts(): void
{
$mailjet = $this->createMock(ApiClient::class);
$mailjet->expects(self::once())->method('createContactsList')->with('LPJMP281126')->willReturn(42);
$mailjet->expects(self::once())->method('createContactsList')->with('LPJMP281126 nur Bus')->willReturn(42);
$mailjet->expects(self::once())->method('addContactsToList')->with(42, [
['email' => '[email protected]', 'firstName' => 'Mia'],
['email' => '[email protected]', 'firstName' => null],
@@ -32,9 +33,11 @@ class TravelMailingListManagerTest extends TestCase
new TravelMailingRecipient('[email protected]', 'Mia', 'Muster'),
new TravelMailingRecipient('[email protected]', 'Lea', 'Muster'),
new TravelMailingRecipient('[email protected]', '', 'Test'),
], new \DateTimeImmutable('2026-11-28'));
], 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());
}
@@ -52,7 +55,7 @@ class TravelMailingListManagerTest extends TestCase
(new TravelMailingListManager($mailjet, $entityManager, new NullLogger()))->create('LPJMP281126', [
new TravelMailingRecipient('[email protected]', 'Mia', 'Muster'),
], new \DateTimeImmutable('2026-11-28'));
], null, new \DateTimeImmutable('2026-11-28'));
}
public function testDeleteRemovesOnlyContactsOnNoOtherList(): void
@@ -70,7 +73,33 @@ class TravelMailingListManagerTest extends TestCase
$entityManager->expects(self::once())->method('remove');
$deleted = (new TravelMailingListManager($mailjet, $entityManager, new NullLogger()))
->delete(new TravelMailingList('LPJMP281126', 42, new \DateTimeImmutable('2026-12-28')));
->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);
}
@@ -87,6 +116,6 @@ class TravelMailingListManagerTest extends TestCase
$this->expectException(NewsletterProviderException::class);
(new TravelMailingListManager($mailjet, $entityManager, new NullLogger()))
->delete(new TravelMailingList('LPJMP281126', 42, new \DateTimeImmutable('2026-12-28')));
->delete([new TravelMailingList('LPJMP281126', 42, null, new \DateTimeImmutable('2026-12-28'))]);
}
}