Skip to content

Commit

Permalink
[feature][BC BREAK] SentEmails is now a collection of TestEmail's
Browse files Browse the repository at this point in the history
  • Loading branch information
kbond committed Oct 7, 2021
1 parent 48e37f8 commit 1ad786e
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 10 deletions.
6 changes: 3 additions & 3 deletions src/SentEmailMixin.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ public function assertEmailSentTo(string $expectedTo, $callback): self
if (\in_array($expectedTo, $toAddresses, true)) {
// address matches
Callback::createFor($callback)->invoke(Parameter::union(
Parameter::untyped(Parameter::factory(fn() => new TestEmail($email))),
Parameter::typed(Email::class, $email),
Parameter::typed(TestEmail::class, Parameter::factory(fn(string $class) => new $class($email)))
Parameter::untyped($email),
Parameter::typed(Email::class, $email->inner()),
Parameter::typed(TestEmail::class, Parameter::factory(fn(string $class) => $email->as($class)))
));

return $this;
Expand Down
25 changes: 20 additions & 5 deletions src/SentEmails.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
*/
final class SentEmails implements \IteratorAggregate, \Countable
{
/** @var Email[] */
/** @var TestEmail[] */
private array $emails;

public function __construct(Email ...$emails)
public function __construct(TestEmail ...$emails)
{
$this->emails = $emails;
}
Expand Down Expand Up @@ -45,15 +45,30 @@ public static function fromEvents(MessageEvents $events): self
// remove non Email messages
$messages = \array_filter($messages, static fn(RawMessage $message) => $message instanceof Email);

// convert to TestEmails
$messages = \array_map(static fn(Email $email) => new TestEmail($email), $messages);

return new self(...$messages);
}

/**
* @template T
*
* @param class-string $class
*
* @return TestEmail[]|T[]
*/
public function all(string $class = TestEmail::class): array
{
return \array_map(static fn(TestEmail $email) => $email->as($class), $this->emails);
}

/**
* @return Email[]
*/
public function all(): array
public function raw(): array
{
return $this->emails;
return \array_map(static fn(TestEmail $email) => $email->inner(), $this->emails);
}

public function assertCount(int $expected): self
Expand All @@ -80,7 +95,7 @@ public function ensureSome(): self
}

/**
* @return \Traversable|Email[]
* @return \Traversable|TestEmail[]
*/
public function getIterator(): \Traversable
{
Expand Down
25 changes: 25 additions & 0 deletions src/TestEmail.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,31 @@ final public function __call($name, $arguments)
return $this->email->{$name}(...$arguments);
}

/**
* @template T
*
* @param class-string $class
*
* @return T
*/
final public function as(string $class): self
{
if (self::class === $class) {
return $this;
}

if (!\is_a($class, self::class, true)) {
throw new \InvalidArgumentException(\sprintf('$class must be a class that\'s an instance of "%s".', self::class));
}

return new $class($this->inner());
}

final public function inner(): Email
{
return $this->email;
}

/**
* @return string|null The first {@see TagHeader} value found or null if none
*/
Expand Down
4 changes: 2 additions & 2 deletions tests/InteractsWithMailerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public function can_access_sent_test_emails(string $environment): void

self::$container->get('mailer')->send(new Email1());

$this->assertInstanceOf(TestEmail::class, $this->mailer()->sentTestEmails()[0]);
$this->assertInstanceOf(TestEmail::class, $this->mailer()->sentEmails()->all()[0]);
}

/**
Expand All @@ -141,7 +141,7 @@ public function can_access_sent_test_emails_with_custom_test_email_class(string

self::$container->get('mailer')->send(new Email1());

$this->assertInstanceOf(CustomTestEmail::class, $this->mailer()->sentTestEmails(CustomTestEmail::class)[0]);
$this->assertInstanceOf(CustomTestEmail::class, $this->mailer()->sentEmails()->all(CustomTestEmail::class)[0]);
}

/**
Expand Down

0 comments on commit 1ad786e

Please sign in to comment.