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
30 protected function setUp()
32 $this->spool
= new CountableMemorySpool();
33 $transport = new \
Swift_Transport_SpoolTransport(
34 new \
Swift_Events_SimpleEventDispatcher(),
37 $this->mailer
= new \
Swift_Mailer($transport);
39 $twigTemplate = <<<'TWIG'
40 {% block subject %}subject{% endblock %}
41 {% block body_html %}html body {{ code }}{% endblock %}
42 {% block body_text %}text body {{ support_url }}{% endblock %}
45 $this->twig
= new \
Twig_Environment(new \
Twig_Loader_Array(['WallabagUserBundle:TwoFactor:email_auth_code.html.twig' => $twigTemplate]));
48 public function testSendEmail()
51 $user->setTwoFactorAuthentication(true);
52 $user->setEmailAuthCode(666666);
53 $user->setEmail('test@wallabag.io');
54 $user->setName('Bob');
56 $authCodeMailer = new AuthCodeMailer(
61 'http://0.0.0.0/support',
65 $authCodeMailer->sendAuthCode($user);
67 $this->assertCount(1, $this->spool
);
69 $msg = $this->spool
->getMessages()[0];
70 $this->assertArrayHasKey('test@wallabag.io', $msg->getTo());
71 $this->assertEquals(['nobody@test.io' => 'wallabag test'], $msg->getFrom());
72 $this->assertEquals('subject', $msg->getSubject());
73 $this->assertContains('text body http://0.0.0.0/support', $msg->toString());
74 $this->assertContains('html body 666666', $msg->toString());