3 namespace Tests\Wallabag\UserBundle\Mailer
;
5 use Wallabag\UserBundle\Entity\User
;
6 use Wallabag\UserBundle\Mailer\AuthCodeMailer
;
9 * @see https://www.pmg.com/blog/integration-testing-swift-mailer/
11 final class CountableMemorySpool
extends \Swift_MemorySpool
implements \Countable
13 public function count()
15 return count($this->messages
);
18 public function getMessages()
20 return $this->messages
;
24 class AuthCodeMailerTest
extends \PHPUnit_Framework_TestCase
31 protected function setUp()
33 $this->spool
= new CountableMemorySpool();
34 $transport = new \
Swift_Transport_SpoolTransport(
35 new \
Swift_Events_SimpleEventDispatcher(),
38 $this->mailer
= new \
Swift_Mailer($transport);
40 $twigTemplate = <<<'TWIG'
41 {% block subject %}subject{% endblock %}
42 {% block body_html %}html body {{ code }}{% endblock %}
43 {% block body_text %}text body {{ support_url }}{% endblock %}
46 $this->twig
= new \
Twig_Environment(new \
Twig_Loader_Array(['WallabagUserBundle:TwoFactor:email_auth_code.html.twig' => $twigTemplate]));
48 $this->config
= $this->getMockBuilder('Craue\ConfigBundle\Util\Config')
49 ->disableOriginalConstructor()
52 $this->config
->expects($this->any())
54 ->willReturn('http://0.0.0.0/support');
57 public function testSendEmail()
60 $user->setTwoFactorAuthentication(true);
61 $user->setEmailAuthCode(666666);
62 $user->setEmail('test@wallabag.io');
63 $user->setName('Bob');
65 $authCodeMailer = new AuthCodeMailer(
73 $authCodeMailer->sendAuthCode($user);
75 $this->assertCount(1, $this->spool
);
77 $msg = $this->spool
->getMessages()[0];
78 $this->assertArrayHasKey('test@wallabag.io', $msg->getTo());
79 $this->assertEquals(['nobody@test.io' => 'wallabag test'], $msg->getFrom());
80 $this->assertEquals('subject', $msg->getSubject());
81 $this->assertContains('text body http://0.0.0.0/support', $msg->toString());
82 $this->assertContains('html body 666666', $msg->toString());