]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/UserBundle/Tests/Mailer/AuthCodeMailerTest.php
9122576a3a5ebd7d65a60dea2b5ca8bdbe9993c5
[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 use Symfony\Component\Translation\DataCollectorTranslator;
10
11 /**
12 * @see https://www.pmg.com/blog/integration-testing-swift-mailer/
13 */
14 final class CountableMemorySpool extends \Swift_MemorySpool implements \Countable
15 {
16 public function count()
17 {
18 return count($this->messages);
19 }
20
21 public function getMessages()
22 {
23 return $this->messages;
24 }
25 }
26
27 class AuthCodeMailerTest extends \PHPUnit_Framework_TestCase
28 {
29 protected $mailer;
30 protected $spool;
31 protected $dataCollector;
32
33 protected function setUp()
34 {
35 $this->spool = new CountableMemorySpool();
36 $transport = new \Swift_Transport_SpoolTransport(
37 new \Swift_Events_SimpleEventDispatcher(),
38 $this->spool
39 );
40 $this->mailer = new \Swift_Mailer($transport);
41
42 $translator = new Translator('en');
43 $translator->addLoader('array', new ArrayLoader());
44 $translator->addResource('array', array(
45 'auth_code.mailer.subject' => 'auth_code subject',
46 'auth_code.mailer.body' => 'Hi %user%, here is the code: %code% and the support: %support%',
47 ), 'en', 'wallabag_user');
48
49 $this->dataCollector = new DataCollectorTranslator($translator);
50 }
51
52 public function testSendEmail()
53 {
54 $user = new User();
55 $user->setTwoFactorAuthentication(true);
56 $user->setEmailAuthCode(666666);
57 $user->setEmail('test@wallabag.io');
58 $user->setName('Bob');
59
60 $authCodeMailer = new AuthCodeMailer(
61 $this->mailer,
62 $this->dataCollector,
63 'nobody@test.io',
64 'wallabag test',
65 'http://0.0.0.0'
66 );
67
68 $authCodeMailer->sendAuthCode($user);
69
70 $this->assertCount(1, $this->spool);
71
72 $msg = $this->spool->getMessages()[0];
73 $this->assertArrayHasKey('test@wallabag.io', $msg->getTo());
74 $this->assertEquals(array('nobody@test.io' => 'wallabag test'), $msg->getFrom());
75 $this->assertEquals('auth_code subject', $msg->getSubject());
76 $this->assertContains('Hi Bob, here is the code: 666666 and the support: http://0.0.0.0', $msg->toString());
77 }
78 }