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