-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
207 additions
and
201 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<?php | ||
|
||
namespace Zenstruck\Mailer\Test; | ||
|
||
use Symfony\Component\Mime\Address; | ||
use Zenstruck\Assert; | ||
use Zenstruck\Callback; | ||
use Zenstruck\Callback\Parameter; | ||
|
||
/** | ||
* @author Kevin Bond <kevinbond@gmail.com> | ||
*/ | ||
trait SentEmailMixin | ||
{ | ||
/** | ||
* @return static | ||
*/ | ||
public function assertNoEmailSent(): self | ||
{ | ||
$this->sentEmails()->assertNone(); | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @return static | ||
*/ | ||
public function assertSentEmailCount(int $expected): self | ||
{ | ||
$this->sentEmails()->assertCount($expected); | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @param callable|string $callback Takes an instance of the found Email as TestEmail - if string, assume subject | ||
* | ||
* @return static | ||
*/ | ||
public function assertEmailSentTo(string $expectedTo, $callback): self | ||
{ | ||
$emails = $this->sentEmails(); | ||
|
||
if (0 === \count($emails)) { | ||
Assert::fail('No emails have been sent.'); | ||
} | ||
|
||
if (!\is_callable($callback)) { | ||
$callback = static fn(TestEmail $message) => $message->assertSubject($callback); | ||
} | ||
|
||
$foundToAddresses = []; | ||
|
||
foreach ($emails as $email) { | ||
$toAddresses = \array_map(static fn(Address $address) => $address->getAddress(), $email->getTo()); | ||
$foundToAddresses[] = $toAddresses; | ||
|
||
if (\in_array($expectedTo, $toAddresses, true)) { | ||
// address matches | ||
Callback::createFor($callback)->invoke( | ||
Parameter::typed(TestEmail::class, Parameter::factory(fn(string $class) => new $class($email))) | ||
); | ||
|
||
return $this; | ||
} | ||
} | ||
|
||
Assert::fail(\sprintf('Email sent, but "%s" is not among to-addresses: %s', $expectedTo, \implode(', ', \array_merge(...$foundToAddresses)))); | ||
} | ||
|
||
abstract public function sentEmails(): SentEmails; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
<?php | ||
|
||
namespace Zenstruck\Mailer\Test; | ||
|
||
use Symfony\Component\Mailer\Event\MessageEvent; | ||
use Symfony\Component\Mailer\Event\MessageEvents; | ||
use Symfony\Component\Mime\Email; | ||
use Symfony\Component\Mime\RawMessage; | ||
use Zenstruck\Assert; | ||
|
||
/** | ||
* @author Kevin Bond <kevinbond@gmail.com> | ||
*/ | ||
final class SentEmails implements \IteratorAggregate, \Countable | ||
{ | ||
/** @var Email[] */ | ||
private array $emails; | ||
|
||
public function __construct(Email ...$emails) | ||
{ | ||
$this->emails = $emails; | ||
} | ||
|
||
public static function fromEvents(MessageEvents $events): self | ||
{ | ||
$usingQueue = false; | ||
$events = $events->getEvents(); | ||
|
||
foreach ($events as $event) { | ||
if ($event->isQueued()) { | ||
$usingQueue = true; | ||
|
||
break; | ||
} | ||
} | ||
|
||
if ($usingQueue) { | ||
// if using queue, remove non queued messages to avoid duplicates | ||
$events = \array_filter($events, static fn(MessageEvent $event) => $event->isQueued()); | ||
} | ||
|
||
// convert events to messages | ||
$messages = \array_map(static fn(MessageEvent $event) => $event->getMessage(), $events); | ||
|
||
// remove non Email messages | ||
$messages = \array_filter($messages, static fn(RawMessage $message) => $message instanceof Email); | ||
|
||
return new self(...$messages); | ||
} | ||
|
||
/** | ||
* @return Email[] | ||
*/ | ||
public function all(): array | ||
{ | ||
return $this->emails; | ||
} | ||
|
||
public function assertCount(int $expected): self | ||
{ | ||
Assert::that($this->emails) | ||
->hasCount($expected, 'Expected {expected} emails to be sent, but {actual} emails were sent.') | ||
; | ||
|
||
return $this; | ||
} | ||
|
||
public function assertNone(): self | ||
{ | ||
return $this->assertCount(0); | ||
} | ||
|
||
/** | ||
* @return \Traversable|Email[] | ||
*/ | ||
public function getIterator(): \Traversable | ||
{ | ||
return new \ArrayIterator($this->emails); | ||
} | ||
|
||
public function count(): int | ||
{ | ||
return \count($this->emails); | ||
} | ||
} |
Oops, something went wrong.