Skip to content

Commit

Permalink
Allow set Mailer instance in EmailCollector
Browse files Browse the repository at this point in the history
  • Loading branch information
natanfelles committed Oct 2, 2022
1 parent 19e333d commit bb0dfa8
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/Debug/EmailCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Framework\Debug\Collector;
use Framework\Debug\Debugger;
use Framework\Email\Header;
use Framework\Email\Mailer;

/**
* Class EmailCollector.
Expand All @@ -20,6 +21,14 @@
*/
class EmailCollector extends Collector
{
protected Mailer $mailer;

public function setMailer(Mailer $mailer) : static
{
$this->mailer = $mailer;
return $this;
}

public function getActivities() : array
{
$activities = [];
Expand All @@ -38,6 +47,10 @@ public function getActivities() : array
public function getContents() : string
{
\ob_start();
if ( ! isset($this->mailer)) {
echo '<p>This collector has not been added to a Mailer instance.</p>';
return \ob_get_clean(); // @phpstan-ignore-line
}
if ( ! $this->hasData()) {
echo '<p>No messages have been sent.</p>';
return \ob_get_clean(); // @phpstan-ignore-line
Expand Down
1 change: 1 addition & 0 deletions src/Mailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ abstract public function send(Message $message) : bool;

public function setDebugCollector(EmailCollector $collector) : static
{
$collector->setMailer($this);
$this->debugCollector = $collector;
return $this;
}
Expand Down
9 changes: 9 additions & 0 deletions tests/Debug/EmailCollectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,17 @@ protected function makeMailer() : Mailer
return $mailer;
}

public function testNoMailer() : void
{
self::assertStringContainsString(
'This collector has not been added to a Mailer instance',
$this->collector->getContents()
);
}

public function testNoData() : void
{
$this->makeMailer();
self::assertStringContainsString(
'No messages have been sent',
$this->collector->getContents()
Expand Down

0 comments on commit bb0dfa8

Please sign in to comment.