feat: create Mailjet lists for travel mailings

This commit is contained in:
fromme committed 2026-11-04 15:32:50 +01:00
1 parent d63fa10a56
commit c16f368293
15 files changed
+851 -1

No files matched your search

+84
View File
@@ -194,4 +194,88 @@ class ApiClientTest extends TestCase
'Action' => 'addforce',
], json_decode((string) $requests[0][2]['body'], true, 512, JSON_THROW_ON_ERROR));
}
public function testAddContactsToListForcesSubscriptionAndSendsOnlyTheFirstName(): void
{
$requests = [];
$client = new MockHttpClient(function (string $method, string $url, array $options) use (&$requests): MockResponse {
$requests[] = [$method, $url, $options];
return new MockResponse(json_encode(['Data' => [['JobID' => 555]]], JSON_THROW_ON_ERROR), ['http_code' => 201]);
});
$mailjet = new ApiClient($client, new NullLogger(), 'key', 'secret', 'https://mailjet.test', null, [
'firstName' => 'vorname',
'lastName' => 'nachname',
]);
$jobId = $mailjet->addContactsToList(42, [
['email' => ' [email protected] ', 'firstName' => ' Mia '],
['email' => '[email protected]', 'firstName' => null],
]);
self::assertSame(555, $jobId);
self::assertSame('POST', $requests[0][0]);
self::assertSame('https://mailjet.test/contactslist/42/managemanycontacts', $requests[0][1]);
self::assertSame([
'Action' => 'addforce',
'Contacts' => [
['Email' => '[email protected]', 'Properties' => ['vorname' => 'Mia']],
['Email' => '[email protected]'],
],
], json_decode((string) $requests[0][2]['body'], true, 512, JSON_THROW_ON_ERROR));
}
public function testCreateContactsListReturnsTheNewListId(): void
{
$requests = [];
$client = new MockHttpClient(function (string $method, string $url, array $options) use (&$requests): MockResponse {
$requests[] = [$method, $url, $options];
return new MockResponse(json_encode(['Data' => [['ID' => 42, 'Name' => 'LPJMP281126']]], JSON_THROW_ON_ERROR), ['http_code' => 201]);
});
$mailjet = new ApiClient($client, new NullLogger(), 'key', 'secret', 'https://mailjet.test');
self::assertSame(42, $mailjet->createContactsList('LPJMP281126'));
self::assertSame('https://mailjet.test/contactslist', $requests[0][1]);
self::assertSame(['Name' => 'LPJMP281126'], json_decode((string) $requests[0][2]['body'], true, 512, JSON_THROW_ON_ERROR));
}
public function testGetListContactIdsPagesThroughAllRecipients(): void
{
$offsets = [];
$client = new MockHttpClient(function (string $method, string $url, array $options) use (&$offsets): MockResponse {
$offset = (int) $options['query']['Offset'];
$offsets[] = $offset;
$data = 0 === $offset
? array_map(static fn (int $id): array => ['ContactID' => $id], range(1, 1000))
: [['ContactID' => 1001]];
return new MockResponse(json_encode(['Data' => $data], JSON_THROW_ON_ERROR));
});
$mailjet = new ApiClient($client, new NullLogger(), 'key', 'secret', 'https://mailjet.test');
$contactIds = $mailjet->getListContactIds(42);
self::assertSame([0, 1000], $offsets);
self::assertCount(1001, $contactIds);
}
public function testDeleteContactUsesTheV4Api(): void
{
$requests = [];
$client = new MockHttpClient(function (string $method, string $url, array $options) use (&$requests): MockResponse {
$requests[] = [$method, $url];
return new MockResponse('', ['http_code' => 200]);
});
$mailjet = new ApiClient($client, new NullLogger(), 'key', 'secret', 'https://api.mailjet.com/v3/REST');
$mailjet->deleteContact(99);
self::assertSame([['DELETE', 'https://api.mailjet.com/v4/contacts/99']], $requests);
}
}
+26
View File
@@ -186,6 +186,32 @@ class TravelIndexTest extends TestCase
$this->assertNull($result);
}
// -------------------------------------------------------------------------
// mapDateCodeToStartDate
// -------------------------------------------------------------------------
public function testMapDateCodeToStartDateReturnsTheTravelStartDate(): void
{
$this->travelLoader->method('generateFilesMap')->willReturn([
42 => ['id' => 42, 'code' => 'WI25', 'dateFrom' => new \DateTimeImmutable('2026-12-28'), 'hotels' => []],
]);
$this->travelSnapshotService->method('generateMapping')->willReturn([]);
$result = $this->service()->mapDateCodeToStartDate('WI25');
$this->assertEquals(new \DateTimeImmutable('2026-12-28'), $result);
}
public function testMapDateCodeToStartDateReturnsNullWhenNotFound(): void
{
$this->travelLoader->method('generateFilesMap')->willReturn([]);
$this->travelSnapshotService->method('generateMapping')->willReturn([]);
$result = $this->service()->mapDateCodeToStartDate('UNKNOWN');
$this->assertNull($result);
}
// -------------------------------------------------------------------------
// mapHotelCodeToId
// -------------------------------------------------------------------------
@@ -0,0 +1,92 @@
<?php
declare(strict_types=1);
namespace App\Tests\Service;
use App\BpnConnect\Model\TravelMailingRecipient;
use App\Entity\TravelMailingList;
use App\Exception\NewsletterProviderException;
use App\Mailjet\ApiClient;
use App\Service\TravelMailingListManager;
use Doctrine\ORM\EntityManagerInterface;
use PHPUnit\Framework\TestCase;
use Psr\Log\NullLogger;
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('addContactsToList')->with(42, [
['email' => '[email protected]', 'firstName' => 'Mia'],
['email' => '[email protected]', '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('[email protected]', 'Mia', 'Muster'),
new TravelMailingRecipient('[email protected]', 'Lea', 'Muster'),
new TravelMailingRecipient('[email protected]', '', 'Test'),
], new \DateTimeImmutable('2026-11-28'));
self::assertSame(42, $mailingList->getMailjetListId());
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('[email protected]', 'Mia', 'Muster'),
], 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, 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, new \DateTimeImmutable('2026-12-28')));
}
}