From 39e68fb9e1a2401bb555f7cfbd78550e92af51c3 Mon Sep 17 00:00:00 2001 From: "Matt H." Date: Wed, 6 Nov 2024 16:15:25 -0500 Subject: [PATCH] Fix checking FakeEmailer for specific types of messages (in tests) --- .../features/bootstrap/EmailContext.php | 25 ++++++++++++------- .../features/bootstrap/fakes/FakeEmailer.php | 7 ++++-- 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/application/features/bootstrap/EmailContext.php b/application/features/bootstrap/EmailContext.php index a6bcfd03..262c28b3 100644 --- a/application/features/bootstrap/EmailContext.php +++ b/application/features/bootstrap/EmailContext.php @@ -1255,15 +1255,8 @@ public function iSendAbandonedUserEmail() */ public function theAbandonedUserEmailHasOrHasNotBeenSent($hasOrHasNot) { - $emails = $this->fakeEmailer->getFakeEmailsSent(); - $hasBeenSent = false; - - foreach ($emails as $email) { - if ($email[Emailer::PROP_SUBJECT] === $this->fakeEmailer->subjectForAbandonedUsers) { - $hasBeenSent = true; - break; - } - } + $numberSent = $this->countEmailsSent(EmailLog::MESSAGE_TYPE_ABANDONED_USERS); + $hasBeenSent = ($numberSent > 0); if ($hasOrHasNot === 'has') { Assert::true($hasBeenSent); @@ -1272,6 +1265,20 @@ public function theAbandonedUserEmailHasOrHasNotBeenSent($hasOrHasNot) } } + protected function countEmailsSent(string $messageType): int + { + $emails = $this->fakeEmailer->getFakeEmailsSent(); + $actualCount = 0; + + foreach ($emails as $email) { + $subject = $email[Emailer::PROP_SUBJECT]; + if ($this->fakeEmailer->isSubjectForMessageType($subject, $messageType)) { + $actualCount++; + } + } + return $actualCount; + } + /** * @Given the database has been purged */ diff --git a/application/features/bootstrap/fakes/FakeEmailer.php b/application/features/bootstrap/fakes/FakeEmailer.php index ce73f776..eb7603af 100644 --- a/application/features/bootstrap/fakes/FakeEmailer.php +++ b/application/features/bootstrap/fakes/FakeEmailer.php @@ -64,8 +64,11 @@ public function getFakeEmailsSent() return $this->getEmailServiceClient()->emailsSent; } - public function isSubjectForMessageType(string $subject, string $messageType, ?User $user) - { + public function isSubjectForMessageType( + string $subject, + string $messageType, + ?User $user = null + ): bool { $dataForEmail = ArrayHelper::merge( $user ? $user->getAttributesForEmail() : [], $this->otherDataForEmails