diff --git a/src/Debug/EmailCollector.php b/src/Debug/EmailCollector.php index df2601a..3a0a9dd 100644 --- a/src/Debug/EmailCollector.php +++ b/src/Debug/EmailCollector.php @@ -12,6 +12,7 @@ use Framework\Debug\Collector; use Framework\Debug\Debugger; use Framework\Email\Header; +use Framework\Email\Mailer; /** * Class EmailCollector. @@ -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 = []; @@ -38,6 +47,10 @@ public function getActivities() : array public function getContents() : string { \ob_start(); + if ( ! isset($this->mailer)) { + echo '

This collector has not been added to a Mailer instance.

'; + return \ob_get_clean(); // @phpstan-ignore-line + } if ( ! $this->hasData()) { echo '

No messages have been sent.

'; return \ob_get_clean(); // @phpstan-ignore-line diff --git a/src/Mailer.php b/src/Mailer.php index c1365bd..b215da0 100644 --- a/src/Mailer.php +++ b/src/Mailer.php @@ -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; } diff --git a/tests/Debug/EmailCollectorTest.php b/tests/Debug/EmailCollectorTest.php index 262dfd4..e09e8d5 100644 --- a/tests/Debug/EmailCollectorTest.php +++ b/tests/Debug/EmailCollectorTest.php @@ -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()