]> git.immae.eu Git - github/wallabag/wallabag.git/blob - tests/Wallabag/UserBundle/Mailer/AuthCodeMailerTest.php
aa1760688232603c85b775a851311b1c2104cb4d
[github/wallabag/wallabag.git] / tests / Wallabag / UserBundle / Mailer / AuthCodeMailerTest.php
1 <?php
2
3 namespace Tests\Wallabag\UserBundle\Mailer;
4
5 use PHPUnit\Framework\TestCase;
6 use Wallabag\UserBundle\Entity\User;
7 use Wallabag\UserBundle\Mailer\AuthCodeMailer;
8
9 /**
10 * @see https://www.pmg.com/blog/integration-testing-swift-mailer/
11 */
12 final class CountableMemorySpool extends \Swift_MemorySpool implements \Countable
13 {
14 public function count()
15 {
16 return \count($this->messages);
17 }
18
19 public function getMessages()
20 {
21 return $this->messages;
22 }
23 }
24
25 class AuthCodeMailerTest extends TestCase
26 {
27 protected $mailer;
28 protected $spool;
29 protected $twig;
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
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 %}
44 TWIG;
45
46 $this->twig = new \Twig_Environment(new \Twig_Loader_Array(['WallabagUserBundle:TwoFactor:email_auth_code.html.twig' => $twigTemplate]));
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,
59 $this->twig,
60 'nobody@test.io',
61 'wallabag test',
62 'http://0.0.0.0/support',
63 'http://0.0.0.0/'
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());
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());
76 }
77 }