feat: improved performance by avoiding redundant MailJet API calls
This commit is contained in:
@@ -29,12 +29,10 @@ class NewsletterManagerTest extends TestCase
|
||||
$mailjet = $this->createMock(MailjetApiClient::class);
|
||||
$mailer = $this->createMock(Mailer::class);
|
||||
|
||||
$mailjet
|
||||
->expects(self::exactly(2))
|
||||
->method('isSubscribed')
|
||||
->withConsecutive(['[email protected]', 1], ['[email protected]', 2])
|
||||
->willReturn(false);
|
||||
$mailjet->expects(self::never())->method('isSubscribed');
|
||||
$mailjet->expects(self::never())->method('ensureSubscribed');
|
||||
$consents->expects(self::once())->method('findActiveByEmail')->with('[email protected]')->willReturn(null);
|
||||
$consents->expects(self::never())->method('findOneByEmailAndListId');
|
||||
$repository->expects(self::once())->method('deleteExpiredPendingByEmail')->with('[email protected]')->willReturn(0);
|
||||
$repository->expects(self::once())->method('findPendingByEmail')->with('[email protected]')->willReturn(null);
|
||||
|
||||
@@ -88,8 +86,10 @@ class NewsletterManagerTest extends TestCase
|
||||
$mailjet = $this->createMock(MailjetApiClient::class);
|
||||
$mailer = $this->createMock(Mailer::class);
|
||||
|
||||
$mailjet->method('isSubscribed')->willReturn(false);
|
||||
$mailjet->expects(self::never())->method('isSubscribed');
|
||||
$mailjet->expects(self::never())->method('ensureSubscribed');
|
||||
$consents->expects(self::once())->method('findActiveByEmail')->with('[email protected]')->willReturn(null);
|
||||
$consents->expects(self::never())->method('findOneByEmailAndListId');
|
||||
$repository->method('deleteExpiredPendingByEmail')->willReturn(0);
|
||||
$repository->expects(self::once())->method('findPendingByEmail')->with('[email protected]')->willReturn($pending);
|
||||
|
||||
@@ -117,8 +117,10 @@ class NewsletterManagerTest extends TestCase
|
||||
$mailjet = $this->createMock(MailjetApiClient::class);
|
||||
$mailer = $this->createMock(Mailer::class);
|
||||
|
||||
$mailjet->method('isSubscribed')->willReturn(false);
|
||||
$mailjet->expects(self::never())->method('isSubscribed');
|
||||
$mailjet->expects(self::never())->method('ensureSubscribed');
|
||||
$consents->expects(self::once())->method('findActiveByEmail')->with('[email protected]')->willReturn(null);
|
||||
$consents->expects(self::never())->method('findOneByEmailAndListId');
|
||||
$repository->method('deleteExpiredPendingByEmail')->willReturn(0);
|
||||
$repository->expects(self::exactly(2))->method('findPendingByEmail')->with('[email protected]')->willReturn($pending);
|
||||
|
||||
@@ -136,20 +138,33 @@ class NewsletterManagerTest extends TestCase
|
||||
self::assertSame('Person', $pending->getLastName());
|
||||
}
|
||||
|
||||
public function testApiRequestRecordsConsentWhenAllListsAreAlreadySubscribed(): void
|
||||
public function testApiRequestUsesLocalConsentStateForAlreadyRegisteredLists(): void
|
||||
{
|
||||
$existingConsent = new NewsletterConsent('[email protected]', 1, 'Old', 'Name');
|
||||
$existingConsent->markConfirmed();
|
||||
$existingOtherConsent = new NewsletterConsent('[email protected]', 2, 'Old', 'Name');
|
||||
$existingOtherConsent->markConfirmed();
|
||||
$repository = $this->createMock(NewsletterOptInRequestRepository::class);
|
||||
$consents = $this->createMock(NewsletterConsentRepository::class);
|
||||
$entityManager = $this->createMock(EntityManagerInterface::class);
|
||||
$mailjet = $this->createMock(MailjetApiClient::class);
|
||||
$mailer = $this->createMock(Mailer::class);
|
||||
|
||||
$mailjet->method('isSubscribed')->willReturn(true);
|
||||
$mailjet->expects(self::never())->method('isSubscribed');
|
||||
$mailjet->expects(self::never())->method('ensureSubscribed');
|
||||
$mailjet->expects(self::once())->method('upsertContact')->with('[email protected]', 'Mia', 'Muster');
|
||||
$consents->method('findActiveByEmail')->with('[email protected]')->willReturn(null);
|
||||
$consents->method('findOneByEmailAndListId')->willReturn(null);
|
||||
$consents->method('findActiveByEmail')->with('[email protected]')->willReturn($existingConsent);
|
||||
$consents
|
||||
->method('findOneByEmailAndListId')
|
||||
->willReturnCallback(static function (string $email, int $mailjetListId) use ($existingConsent, $existingOtherConsent): ?NewsletterConsent {
|
||||
return match ($mailjetListId) {
|
||||
1 => $existingConsent,
|
||||
2 => $existingOtherConsent,
|
||||
default => null,
|
||||
};
|
||||
});
|
||||
$repository->expects(self::once())->method('deletePendingByEmail')->with('[email protected]')->willReturn(0);
|
||||
$entityManager->expects(self::exactly(2))->method('persist')->with(self::isInstanceOf(NewsletterConsent::class));
|
||||
$entityManager->expects(self::never())->method('persist');
|
||||
$entityManager->expects(self::once())->method('flush');
|
||||
$mailer->expects(self::never())->method('createAndSendEmail');
|
||||
|
||||
@@ -192,20 +207,18 @@ class NewsletterManagerTest extends TestCase
|
||||
|
||||
public function testApiRequestDirectlySubscribesMissingListWhenEmailIsSubscribedToKnownList(): void
|
||||
{
|
||||
$existingConsent = new NewsletterConsent('[email protected]', 1, 'Old', 'Name');
|
||||
$existingConsent->markConfirmed();
|
||||
$repository = $this->createMock(NewsletterOptInRequestRepository::class);
|
||||
$consents = $this->createMock(NewsletterConsentRepository::class);
|
||||
$entityManager = $this->createMock(EntityManagerInterface::class);
|
||||
$mailjet = $this->createMock(MailjetApiClient::class);
|
||||
$mailer = $this->createMock(Mailer::class);
|
||||
|
||||
$mailjet
|
||||
->expects(self::exactly(2))
|
||||
->method('isSubscribed')
|
||||
->withConsecutive(['[email protected]', 2], ['[email protected]', 1])
|
||||
->willReturnOnConsecutiveCalls(false, true);
|
||||
$mailjet->expects(self::never())->method('isSubscribed');
|
||||
$mailjet->expects(self::once())->method('upsertContact')->with('[email protected]', 'Mia', 'Muster');
|
||||
$mailjet->expects(self::once())->method('ensureSubscribed')->with('[email protected]', 2);
|
||||
$consents->method('findActiveByEmail')->with('[email protected]')->willReturn(null);
|
||||
$consents->method('findActiveByEmail')->with('[email protected]')->willReturn($existingConsent);
|
||||
$consents->method('findOneByEmailAndListId')->willReturn(null);
|
||||
$repository->expects(self::once())->method('deletePendingByEmail')->with('[email protected]')->willReturn(0);
|
||||
$repository->expects(self::never())->method('findPendingByEmail');
|
||||
@@ -233,7 +246,7 @@ class NewsletterManagerTest extends TestCase
|
||||
$mailjet = $this->createMock(MailjetApiClient::class);
|
||||
$mailer = $this->createMock(Mailer::class);
|
||||
|
||||
$mailjet->method('isSubscribed')->with('[email protected]', 2)->willReturn(false);
|
||||
$mailjet->expects(self::never())->method('isSubscribed');
|
||||
$mailjet->expects(self::once())->method('upsertContact')->with('[email protected]', 'Mia', 'Muster');
|
||||
$mailjet->expects(self::once())->method('ensureSubscribed')->with('[email protected]', 2);
|
||||
$consents->method('findActiveByEmail')->with('[email protected]')->willReturn($existingConsent);
|
||||
|
||||
Reference in New Issue
Block a user