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
;
9 use Symfony\Component\Translation\DataCollectorTranslator
;
12 * @see https://www.pmg.com/blog/integration-testing-swift-mailer/
14 final class CountableMemorySpool
extends \Swift_MemorySpool
implements \Countable
16 public function count()
18 return count($this->messages
);
21 public function getMessages()
23 return $this->messages
;
27 class AuthCodeMailerTest
extends \PHPUnit_Framework_TestCase
31 protected $dataCollector;
33 protected function setUp()
35 $this->spool
= new CountableMemorySpool();
36 $transport = new \
Swift_Transport_SpoolTransport(
37 new \
Swift_Events_SimpleEventDispatcher(),
40 $this->mailer
= new \
Swift_Mailer($transport);
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');
49 $this->dataCollector
= new DataCollectorTranslator($translator);
52 public function testSendEmail()
55 $user->setTwoFactorAuthentication(true);
56 $user->setEmailAuthCode(666666);
57 $user->setEmail('test@wallabag.io');
58 $user->setName('Bob');
60 $authCodeMailer = new AuthCodeMailer(
68 $authCodeMailer->sendAuthCode($user);
70 $this->assertCount(1, $this->spool
);
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());