3 namespace Wallabag\UserBundle\Tests\Mailer
;
5 use Wallabag\UserBundle\Entity\User
;
6 use Wallabag\UserBundle\Mailer\AuthCodeMailer
;
7 use Symfony\Component\Translation\Translator
;
8 use Symfony\Component\Translation\Loader\ArrayLoader
;
11 * @see https://www.pmg.com/blog/integration-testing-swift-mailer/
13 final class CountableMemorySpool
extends \Swift_MemorySpool
implements \Countable
15 public function count()
17 return count($this->messages
);
20 public function getMessages()
22 return $this->messages
;
26 class AuthCodeMailerTest
extends \PHPUnit_Framework_TestCase
32 protected function setUp()
34 $this->spool
= new CountableMemorySpool();
35 $transport = new \
Swift_Transport_SpoolTransport(
36 new \
Swift_Events_SimpleEventDispatcher(),
39 $this->mailer
= new \
Swift_Mailer($transport);
41 $this->twig
= new \
Twig_Environment(new \
Twig_Loader_Array(array('@WallabagUserBundle/Resources/views/TwoFactor/email_auth_code.html.twig' => '
42 {% block subject %}subject{% endblock %}
43 {% block body_html %}html body{% endblock %}
44 {% block body_text %}text body{% endblock %}
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(
64 $authCodeMailer->sendAuthCode($user);
66 $this->assertCount(1, $this->spool
);
68 $msg = $this->spool
->getMessages()[0];
69 $this->assertArrayHasKey('test@wallabag.io', $msg->getTo());
70 $this->assertEquals(array('nobody@test.io' => 'wallabag test'), $msg->getFrom());
71 $this->assertEquals('subject', $msg->getSubject());
72 $this->assertContains('text body', $msg->toString());
73 $this->assertContains('html body', $msg->toString());