Skip to content

Commit

Permalink
[feature] add TestEmail::dump()/dd()
Browse files Browse the repository at this point in the history
  • Loading branch information
kbond committed Oct 8, 2021
1 parent 8a8b0da commit edc7922
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 68 deletions.
2 changes: 1 addition & 1 deletion src/SentEmails.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public function each(callable $callback): self
*/
public function where(callable $filter): self
{
return new self(...\array_filter($this->emails, $filter));
return new self(...\array_filter($this->emails, static fn(TestEmail $email) => $email->call($filter)));
}

public function whereSubject(string $subject): self
Expand Down
118 changes: 51 additions & 67 deletions src/TestEmail.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@

use Symfony\Component\Mailer\Header\MetadataHeader;
use Symfony\Component\Mailer\Header\TagHeader;
use Symfony\Component\Mime\Address;
use Symfony\Component\Mime\Email;
use Zenstruck\Assert;
use Zenstruck\Callback;
use Zenstruck\Callback\Parameter;

/**
* @author Kevin Bond <kevinbond@gmail.com>
*
* @mixin Email
*
* @author Kevin Bond <kevinbond@gmail.com>
*/
class TestEmail
{
Expand Down Expand Up @@ -129,84 +130,29 @@ final public function assertSubjectContains(string $needle): self
return $this;
}

final public function assertFrom(string $expectedEmail, string $expectedName = ''): self
final public function assertFrom(string $expectedEmail, ?string $expectedName = null): self
{
foreach ($this->email->getFrom() as $address) {
if ($expectedEmail !== $address->getAddress()) {
continue;
}

Assert::that($address->getAddress())->is($expectedEmail);
Assert::that($address->getName())->is($expectedName);

return $this;
}

Assert::fail("Message does not have from [{$expectedEmail}]");
return $this->assertEmail($this->email->getFrom(), $expectedEmail, $expectedName, 'from');
}

final public function assertTo(string $expectedEmail, string $expectedName = ''): self
final public function assertTo(string $expectedEmail, ?string $expectedName = null): self
{
foreach ($this->email->getTo() as $address) {
if ($expectedEmail !== $address->getAddress()) {
continue;
}

Assert::that($address->getAddress())->is($expectedEmail);
Assert::that($address->getName())->is($expectedName);

return $this;
}

Assert::fail("Message does not have to [{$expectedEmail}]");
return $this->assertEmail($this->email->getTo(), $expectedEmail, $expectedName, 'to');
}

final public function assertCc(string $expectedEmail, string $expectedName = ''): self
final public function assertCc(string $expectedEmail, ?string $expectedName = null): self
{
foreach ($this->email->getCc() as $address) {
if ($expectedEmail !== $address->getAddress()) {
continue;
}

Assert::that($address->getAddress())->is($expectedEmail);
Assert::that($address->getName())->is($expectedName);

return $this;
}

Assert::fail("Message does not have cc [{$expectedEmail}]");
return $this->assertEmail($this->email->getCc(), $expectedEmail, $expectedName, 'cc');
}

final public function assertBcc(string $expectedEmail, string $expectedName = ''): self
final public function assertBcc(string $expectedEmail, ?string $expectedName = null): self
{
foreach ($this->email->getBcc() as $address) {
if ($expectedEmail !== $address->getAddress()) {
continue;
}

Assert::that($address->getAddress())->is($expectedEmail);
Assert::that($address->getName())->is($expectedName);

return $this;
}

Assert::fail("Message does not have bcc [{$expectedEmail}]");
return $this->assertEmail($this->email->getBcc(), $expectedEmail, $expectedName, 'bcc');
}

final public function assertReplyTo(string $expectedEmail, string $expectedName = ''): self
final public function assertReplyTo(string $expectedEmail, ?string $expectedName = null): self
{
foreach ($this->email->getReplyTo() as $address) {
if ($expectedEmail !== $address->getAddress()) {
continue;
}

Assert::that($address->getAddress())->is($expectedEmail);
Assert::that($address->getName())->is($expectedName);

return $this;
}

Assert::fail("Message does not have reply-to [{$expectedEmail}]");
return $this->assertEmail($this->email->getReplyTo(), $expectedEmail, $expectedName, 'reply-to');
}

/**
Expand Down Expand Up @@ -279,4 +225,42 @@ final public function assertHasMetadata(string $expectedKey, ?string $expectedVa

return $this;
}

final public function dump(): self
{
\call_user_func(\function_exists('dump') ? 'dump' : 'var_dump', $this->email);

return $this;
}

final public function dd(): void
{
$this->dump();
exit(1);
}

/**
* @param Address[] $addresses
*/
private function assertEmail(array $addresses, string $expectedEmail, ?string $expectedName, string $type): self
{
foreach ($addresses as $address) {
if ($expectedEmail !== $address->getAddress()) {
continue;
}

Assert::that($address->getAddress())->is($expectedEmail);

if (null !== $expectedName) {
Assert::that($address->getName())->is($expectedName);
}

return $this;
}

Assert::fail('Message does not have {type} {expected}', [
'type' => \mb_strtoupper($type),
'expected' => $expectedEmail, ]
);
}
}

0 comments on commit edc7922

Please sign in to comment.