]> git.immae.eu Git - github/wallabag/wallabag.git/blame - tests/Wallabag/UserBundle/Mailer/AuthCodeMailerTest.php
php-cs-fixer
[github/wallabag/wallabag.git] / tests / Wallabag / UserBundle / Mailer / AuthCodeMailerTest.php
CommitLineData
23ff8d36
JB
1<?php
2
23634d5d 3namespace Tests\Wallabag\UserBundle\Mailer;
23ff8d36 4
bd91bd5c 5use PHPUnit\Framework\TestCase;
23ff8d36
JB
6use Wallabag\UserBundle\Entity\User;
7use Wallabag\UserBundle\Mailer\AuthCodeMailer;
23ff8d36
JB
8
9/**
10 * @see https://www.pmg.com/blog/integration-testing-swift-mailer/
11 */
12final class CountableMemorySpool extends \Swift_MemorySpool implements \Countable
13{
14 public function count()
15 {
2a1ceb67 16 return \count($this->messages);
23ff8d36
JB
17 }
18
19 public function getMessages()
20 {
21 return $this->messages;
22 }
23}
24
bd91bd5c 25class AuthCodeMailerTest extends TestCase
23ff8d36
JB
26{
27 protected $mailer;
28 protected $spool;
7ce895bf 29 protected $twig;
23ff8d36
JB
30
31 protected function setUp()
32 {
33 $this->spool = new CountableMemorySpool();
34 $transport = new \Swift_Transport_SpoolTransport(
35 new \Swift_Events_SimpleEventDispatcher(),
36 $this->spool
37 );
38 $this->mailer = new \Swift_Mailer($transport);
39
084fb0d3 40 $twigTemplate = <<<'TWIG'
7ce895bf 41{% block subject %}subject{% endblock %}
abd454c4
JB
42{% block body_html %}html body {{ code }}{% endblock %}
43{% block body_text %}text body {{ support_url }}{% endblock %}
44TWIG;
45
4094ea47 46 $this->twig = new \Twig_Environment(new \Twig_Loader_Array(['WallabagUserBundle:TwoFactor:email_auth_code.html.twig' => $twigTemplate]));
23ff8d36
JB
47 }
48
49 public function testSendEmail()
50 {
51 $user = new User();
52 $user->setTwoFactorAuthentication(true);
53 $user->setEmailAuthCode(666666);
54 $user->setEmail('test@wallabag.io');
55 $user->setName('Bob');
56
57 $authCodeMailer = new AuthCodeMailer(
58 $this->mailer,
7ce895bf 59 $this->twig,
23ff8d36
JB
60 'nobody@test.io',
61 'wallabag test',
e1632cea
JB
62 'http://0.0.0.0/support',
63 'http://0.0.0.0/'
23ff8d36
JB
64 );
65
66 $authCodeMailer->sendAuthCode($user);
67
68 $this->assertCount(1, $this->spool);
69
70 $msg = $this->spool->getMessages()[0];
71 $this->assertArrayHasKey('test@wallabag.io', $msg->getTo());
f808b016
JB
72 $this->assertSame(['nobody@test.io' => 'wallabag test'], $msg->getFrom());
73 $this->assertSame('subject', $msg->getSubject());
abd454c4
JB
74 $this->assertContains('text body http://0.0.0.0/support', $msg->toString());
75 $this->assertContains('html body 666666', $msg->toString());
23ff8d36
JB
76 }
77}