3 namespace Wallabag\UserBundle\Tests\Mailer
;
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
;
11 * @see https://www.pmg.com/blog/integration-testing-swift-mailer/
13 final class CountableMemorySpool
extends \Swift_MemorySpool
implements \Countable
15 public function count()
17 return count($this->messages
);
20 public function getMessages()
22 return $this->messages
;
26 class AuthCodeMailerTest
extends \PHPUnit_Framework_TestCase
30 protected $translator;
32 protected function setUp()
34 $this->spool
= new CountableMemorySpool();
35 $transport = new \
Swift_Transport_SpoolTransport(
36 new \
Swift_Events_SimpleEventDispatcher(),
39 $this->mailer
= new \
Swift_Mailer($transport);
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');
49 public function testSendEmail()
52 $user->setTwoFactorAuthentication(true);
53 $user->setEmailAuthCode(666666);
54 $user->setEmail('test@wallabag.io');
55 $user->setName('Bob');
57 $authCodeMailer = new AuthCodeMailer(
65 $authCodeMailer->sendAuthCode($user);
67 $this->assertCount(1, $this->spool
);
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());