3 namespace Tests\Wallabag\UserBundle\Mailer
;
5 use PHPUnit\Framework\TestCase
;
6 use Wallabag\UserBundle\Entity\User
;
7 use Wallabag\UserBundle\Mailer\AuthCodeMailer
;
10 * @see https://www.pmg.com/blog/integration-testing-swift-mailer/
12 final class CountableMemorySpool
extends \Swift_MemorySpool
implements \Countable
14 public function count()
16 return \
count($this->messages
);
19 public function getMessages()
21 return $this->messages
;
25 class AuthCodeMailerTest
extends 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]));
49 public function testSendEmail()
52 $user->setTwoFactorAuthentication(true);
53 $user->setEmailAuthCode(666666);
54 $user->setEmail('test@wallabag.io');
55 $user->setName('Bob');
57 $authCodeMailer = new AuthCodeMailer(
62 'http://0.0.0.0/support',
66 $authCodeMailer->sendAuthCode($user);
68 $this->assertCount(1, $this->spool
);
70 $msg = $this->spool
->getMessages()[0];
71 $this->assertArrayHasKey('test@wallabag.io', $msg->getTo());
72 $this->assertSame(['nobody@test.io' => 'wallabag test'], $msg->getFrom());
73 $this->assertSame('subject', $msg->getSubject());
74 $this->assertContains('text body http://0.0.0.0/support', $msg->toString());
75 $this->assertContains('html body 666666', $msg->toString());