mailer-test icon indicating copy to clipboard operation
mailer-test copied to clipboard

Email->assertTo() might accept multiple recipient email addresses

Open myselfhimself opened this issue 2 years ago • 3 comments
trafficstars

Hello

for now TestEmail::assertTo allows for checking that only 1 email address belongs in the To: recipients email addresses list.

So if I want to check that my e-mail goes to three specific email addresses, I have to call assertTo 3 three times, then check a count as follows:

        foreach($effectiveToEmailAddresses as $effectiveToEmailAddress) {
            $sentEmail->assertTo($effectiveToEmailAddress);
        }
        $this->assertCount(count($effectiveToEmailAddresses), $sentEmail->getTo());

maybe final public function assertTo(string $expectedEmail, ?string $expectedName = null): self could have a friend sibling method assertToAddresses(Address[]|array $expectedAddresses, bool $strict = true): self where the $strict ensures that the recipient addresses set is exactly that of $expectedAddresses, instead of just checking for a subset inclusion (ie. expected addresses belong in actual addresses).

This is not blocking... Just ideating.

myselfhimself avatar Jan 13 '23 13:01 myselfhimself

This makes sense. We'd need this method for each of the methods that return addresses.

Would be nice to perhaps have a TestAddressSet object so we could do:

$email->getTo()
    ->assertCount(int)
    ->assertContains(string $expectedEmail, ?string $expectedName = null)
    ->assertContainsAll(Address[] $expectedAddresses) // check subset
    ->assertOnlyContainsAll(Address[] $expectedAddresses) // strict
;

Not sure how to preserve BC for this though as changing the signature of getTo() would be a BC break.

kbond avatar Jan 13 '23 15:01 kbond

Hello,

maybe... getTo() could have a getToMultiple() or getToAsSet() sibling method returning your imagined TestAddressSet object and linked methods.

assertOnlyContainsAll could equate to assertContainsAll( with a $strict = true) flag last parameter.

myselfhimself avatar Jan 19 '23 14:01 myselfhimself

maybe... getTo() could have a getToMultiple() or getToAsSet() sibling method returning your imagined TestAddressSet object and linked methods.

getToAsSet() seems nice.

kbond avatar Jan 19 '23 15:01 kbond