]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/UserBundle/Tests/Mailer/AuthCodeMailerTest.php
61e960f9cdefe4b179b0635ea2b35974c4b22172
[github/wallabag/wallabag.git] / src / Wallabag / UserBundle / Tests / Mailer / AuthCodeMailerTest.php
1 <?php
2
3 namespace Wallabag\UserBundle\Tests\Mailer;
4
5 use Wallabag\UserBundle\Entity\User;
6 use Wallabag\UserBundle\Mailer\AuthCodeMailer;
7 use Symfony\Component\Translation\Translator;
8 use Symfony\Component\Translation\Loader\ArrayLoader;
9
10 /**
11 * @see https://www.pmg.com/blog/integration-testing-swift-mailer/
12 */
13 final class CountableMemorySpool extends \Swift_MemorySpool implements \Countable
14 {
15 public function count()
16 {
17 return count($this->messages);
18 }
19
20 public function getMessages()
21 {
22 return $this->messages;
23 }
24 }
25
26 class AuthCodeMailerTest extends \PHPUnit_Framework_TestCase
27 {
28 protected $mailer;
29 protected $spool;
30 protected $translator;
31
32 protected function setUp()
33 {
34 $this->spool = new CountableMemorySpool();
35 $transport = new \Swift_Transport_SpoolTransport(
36 new \Swift_Events_SimpleEventDispatcher(),
37 $this->spool
38 );
39 $this->mailer = new \Swift_Mailer($transport);
40
41 $this->translator = new Translator('en');
42 $this->translator->addLoader('array', new ArrayLoader());
43 $this->translator->addResource('array', array(
44 'auth_code.mailer.subject' => 'auth_code subject',
45 'auth_code.mailer.body' => 'Hi %user%, here is the code: %code% and the support: %support%',
46 ), 'en', 'wallabag_user');
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->translator,
60 'nobody@test.io',
61 'wallabag test',
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(array('nobody@test.io' => 'wallabag test'), $msg->getFrom());
72 $this->assertEquals('auth_code subject', $msg->getSubject());
73 $this->assertContains('Hi Bob, here is the code: 666666 and the support: http://0.0.0.0', $msg->toString());
74 }
75 }