Skip to content

Commit

Permalink
Fix checking FakeEmailer for specific types of messages (in tests)
Browse files Browse the repository at this point in the history
  • Loading branch information
forevermatt committed Nov 6, 2024
1 parent 990b1ac commit 39e68fb
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
25 changes: 16 additions & 9 deletions application/features/bootstrap/EmailContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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
*/
Expand Down
7 changes: 5 additions & 2 deletions application/features/bootstrap/fakes/FakeEmailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 39e68fb

Please sign in to comment.