Last active
August 29, 2015 14:17
-
-
Save chrisguitarguy/f692df29fa4cc565504a to your computer and use it in GitHub Desktop.
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
final class CountableMemorySpool extends \Swift_MemorySpool implements \Countable | |
{ | |
public function count() | |
{ | |
return count($this->messages); | |
} | |
public function getMessages() | |
{ | |
return $this->messages; | |
} | |
} |
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
class Helloer | |
{ | |
private $mailer; | |
public function __construct(\Swift_Mailer $mailer) | |
{ | |
$this->mailer = $mailer; | |
} | |
public function sayHello($email) | |
{ | |
$msg = \Swift_Message::newInstance() | |
->setSubject('Hello') | |
->setBody('Hello') | |
->setFrom('helle@example.com') | |
->setTo($email); | |
$this->mailer->send($msg); | |
} | |
} | |
class HelloerTest extends SwiftMailerTestCase | |
{ | |
private $helloer; | |
public function testSayHelloSendsSpamEmails() | |
{ | |
$this->helloer->sayHello('test@pmg.co'); | |
$this->assertMessageCount(1, 'should have sent one email'); | |
$msg = $this->getMessages()[0]; | |
$this->assertArrayHasKey('test@pmg.co', $msg->getTo()); | |
} | |
protected function setUp() | |
{ | |
parent::setUp(); | |
$this->helloer = new Helloer($this->mailer); | |
} | |
} |
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
abstract class SwiftMailerTestCase extends \PHPUnit_Framework_TestCase | |
{ | |
protected $mailer, $transport, $spool; | |
protected function setUp() | |
{ | |
$this->spool = new CountableMemorySpool(); | |
$this->transport = new \Swift_Transport_SpoolTransport( | |
new \Swift_Events_SimpleEventDispatcher(), | |
$this->spool | |
); | |
$this->mailer = new \Swift_Mailer($this->transport); | |
} | |
protected function assertMessageCount($count, $msg='') | |
{ | |
$this->assertCount($count, $this->spool, $msg); | |
} | |
protected function getMessages() | |
{ | |
return $this->spool->getMessages(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment