Skip to content

Instantly share code, notes, and snippets.

@chrisguitarguy
Last active August 29, 2015 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrisguitarguy/f692df29fa4cc565504a to your computer and use it in GitHub Desktop.
Save chrisguitarguy/f692df29fa4cc565504a to your computer and use it in GitHub Desktop.
final class CountableMemorySpool extends \Swift_MemorySpool implements \Countable
{
public function count()
{
return count($this->messages);
}
public function getMessages()
{
return $this->messages;
}
}
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);
}
}
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