]> git.immae.eu Git - github/wallabag/wallabag.git/blob - UserBundle/Mailer/AuthCodeMailerTest.php
Merge pull request #4438 from wallabag/dependabot/composer/scheb/two-factor-bundle...
[github/wallabag/wallabag.git] / UserBundle / Mailer / AuthCodeMailerTest.php
1 <?php
2
3 namespace Tests\Wallabag\UserBundle\Mailer;
4
5 use Wallabag\UserBundle\Entity\User;
6 use Wallabag\UserBundle\Mailer\AuthCodeMailer;
7
8 /**
9 * @see https://www.pmg.com/blog/integration-testing-swift-mailer/
10 */
11 final 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
24 class AuthCodeMailerTest extends \PHPUnit_Framework_TestCase
25 {
26 protected $mailer;
27 protected $spool;
28 protected $twig;
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
39 $twigTemplate = <<<'TWIG'
40 {% block subject %}subject{% endblock %}
41 {% block body_html %}html body {{ code }}{% endblock %}
42 {% block body_text %}text body {{ support_url }}{% endblock %}
43 TWIG;
44
45 $this->twig = new \Twig_Environment(new \Twig_Loader_Array(['WallabagUserBundle:TwoFactor:email_auth_code.html.twig' => $twigTemplate]));
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,
58 $this->twig,
59 'nobody@test.io',
60 'wallabag test',
61 'http://0.0.0.0/support',
62 'http://0.0.0.0/'
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());
71 $this->assertEquals(['nobody@test.io' => 'wallabag test'], $msg->getFrom());
72 $this->assertEquals('subject', $msg->getSubject());
73 $this->assertContains('text body http://0.0.0.0/support', $msg->toString());
74 $this->assertContains('html body 666666', $msg->toString());
75 }
76 }