Created
February 18, 2018 23:13
-
-
Save geraintp/2fba652e157740f5cedb787c50438de0 to your computer and use it in GitHub Desktop.
EmailAsserts Laravel Email test trait
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
<?php | |
namespace Tests; | |
use Swift_Message; | |
trait EmailAsserts | |
{ | |
protected $emails = []; | |
/** @before */ | |
public function setUpMailTracking() | |
{ | |
Mail::getSwiftMailer() | |
->registerPlugin(new MailTestingEventListener($this)); | |
} | |
protected function seeEmailWasSent() | |
{ | |
$this->assertNotEmpty( | |
$this->emails, | |
'Failed no emails have been sent.' | |
); | |
return $this; | |
} | |
protected function seeEmailWasNotSent() | |
{ | |
$this->assertEmpty( | |
$this->emails, | |
'Failed emails have been sent.' | |
); | |
return $this; | |
} | |
protected function seeEmailsSent($count) | |
{ | |
$emailsSent = count($this->emails); | |
$this->assertCount( | |
$count, $this->emails, | |
"Failed asserting $emailsSent email sent matches expected $count sent." | |
); | |
return $this; | |
} | |
protected function seeEmailTo($receipient, Swift_Message $message = null) | |
{ | |
$this->assertArrayHasKey( | |
$receipient, $this->getEmail($message)->getTo(), | |
"Failed asserting email sent to '$receipient'." | |
); | |
return $this; | |
} | |
protected function seeEmailNotTo($receipient, Swift_Message $message = null) | |
{ | |
if (empty($this->emails)) { | |
return $this; | |
} | |
$this->assertArrayNotHasKey( | |
$receipient, | |
$this->getEmail($message)->getTo(), | |
"Failed asserting no email sent to '$receipient'." | |
); | |
return $this; | |
} | |
protected function seeEmailFrom($receipient, Swift_Message $message = null) | |
{ | |
$this->assertArrayHasKey( | |
$receipient, | |
$this->getEmail($message)->getFrom(), | |
"Failed asserting email sent from '$receipient'." | |
); | |
return $this; | |
} | |
protected function seeEmailNotFrom($receipient, Swift_Message $message = null) | |
{ | |
if (empty($this->emails)) { | |
return $this; | |
} | |
$this->assertArrayNotHasKey( | |
$receipient, | |
$this->getEmail($message)->getFrom(), | |
"Failed asserting no email sent from '$receipient'." | |
); | |
return $this; | |
} | |
protected function seeEmailSubject($subject, Swift_Message $message = null) | |
{ | |
$this->assertEquals( | |
$subject, | |
$this->getEmail($message)->getSubject(), | |
"Failed asserting email subject equals string." | |
); | |
return $this; | |
} | |
protected function seeEmailNotSubject($subject, Swift_Message $message = null) | |
{ | |
$this->assertNotEquals( | |
$subject, | |
$this->getEmail($message)->getSubject(), | |
"Failed asserting email subject is not equal to string." | |
); | |
return $this; | |
} | |
protected function seeEmailEquals($body, Swift_Message $message = null) | |
{ | |
$this->assertEquals( | |
$body, $this->getEmail($message)->getBody(), | |
"Failed asserting email body equals string." | |
); | |
return $this; | |
} | |
protected function seeEmailNotEquals($body, Swift_Message $message = null) | |
{ | |
$this->assertNotEquals( | |
$body, | |
$this->getEmail($message)->getBody(), | |
"Failed asserting email body is not equal to string." | |
); | |
return $this; | |
} | |
protected function seeEmailContains($body, Swift_Message $message = null) | |
{ | |
$this->assertContains( | |
$body, | |
$this->getEmail($message)->getBody(), | |
"Failed asserting email body contains string." | |
); | |
return $this; | |
} | |
protected function seeEmailNotContains($body, Swift_Message $message = null) | |
{ | |
$this->assertNotContains( | |
$body, | |
$this->getEmail($message)->getBody(), | |
"Failed asserting email body does not contain string." | |
); | |
return $this; | |
} | |
public function addEmail(Swift_Message $email) | |
{ | |
$this->emails[] = $email; | |
} | |
private function getEmail(Swift_Message $message = null) | |
{ | |
return $message ?: $this->lastEmail(); | |
} | |
private function lastEmail() | |
{ | |
$this->seeEmailWasSent(); | |
return end($this->emails); | |
} | |
} | |
class MailTestingEventListener implements \Swift_Events_EventListener | |
{ | |
protected $test; | |
public function __construct($test) | |
{ | |
$this->test = $test; | |
} | |
public function beforeSendPerformed($event) | |
{ | |
$this->test->addEmail($event->getMessage()); | |
} | |
} |
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
<?php | |
namespace Tests\Unit; | |
use Tests\TestCase; | |
use Tests\EmailAsserts; | |
use Illuminate\Foundation\Testing\DatabaseMigrations; | |
use Illuminate\Foundation\Testing\DatabaseTransactions; | |
use Mail; | |
class EmailTest extends TestCase | |
{ | |
use EmailAsserts; | |
/** @test */ | |
public function testSeeEmailWasSent() | |
{ | |
$this->seeEmailWasNotSent() | |
->seeEmailsSent(0); | |
Mail::raw('Hello world', function ($message) | |
{ | |
$message->to('foo@bar.com') | |
->subject('Subject') | |
->from('bar@bar.com'); | |
}); | |
$this->seeEmailWasSent() | |
->seeEmailsSent(1) | |
->seeEmailNotTo('foos@bar.com') | |
->seeEmailTo('foo@bar.com') | |
->seeEmailFrom('bar@bar.com') | |
->seeEmailNotFrom('bard@bar.com') | |
->seeEmailEquals('Hello world') | |
->seeEmailNotEquals('Hell world') | |
->seeEmailContains('Hello') | |
->seeEmailNotContains('Hell0') | |
->seeEmailSubject("Subject") | |
->seeEmailNotSubject("Sub"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment