3 namespace Wallabag\UserBundle\Tests\Mailer
;
5 use Wallabag\UserBundle\Entity\User
;
6 use Wallabag\UserBundle\Mailer\AuthCodeMailer
;
9 * @see https://www.pmg.com/blog/integration-testing-swift-mailer/
11 final class CountableMemorySpool
extends \Swift_MemorySpool
implements \Countable
13 public function count()
15 return count($this->messages
);
18 public function getMessages()
20 return $this->messages
;
24 class AuthCodeMailerTest
extends \PHPUnit_Framework_TestCase
30 protected function setUp()
32 $this->spool
= new CountableMemorySpool();
33 $transport = new \
Swift_Transport_SpoolTransport(
34 new \
Swift_Events_SimpleEventDispatcher(),
37 $this->mailer
= new \
Swift_Mailer($transport);
39 $this->twig
= new \
Twig_Environment(new \
Twig_Loader_Array(array('@WallabagUserBundle/Resources/views/TwoFactor/email_auth_code.html.twig' => '
40 {% block subject %}subject{% endblock %}
41 {% block body_html %}html body{% endblock %}
42 {% block body_text %}text body{% endblock %}
46 public function testSendEmail()
49 $user->setTwoFactorAuthentication(true);
50 $user->setEmailAuthCode(666666);
51 $user->setEmail('test@wallabag.io');
52 $user->setName('Bob');
54 $authCodeMailer = new AuthCodeMailer(
62 $authCodeMailer->sendAuthCode($user);
64 $this->assertCount(1, $this->spool
);
66 $msg = $this->spool
->getMessages()[0];
67 $this->assertArrayHasKey('test@wallabag.io', $msg->getTo());
68 $this->assertEquals(array('nobody@test.io' => 'wallabag test'), $msg->getFrom());
69 $this->assertEquals('subject', $msg->getSubject());
70 $this->assertContains('text body', $msg->toString());
71 $this->assertContains('html body', $msg->toString());