]>
Commit | Line | Data |
---|---|---|
23ff8d36 JB |
1 | <?php |
2 | ||
3 | namespace Wallabag\UserBundle\Tests\Mailer; | |
4 | ||
5 | use Wallabag\UserBundle\Entity\User; | |
6 | use Wallabag\UserBundle\Mailer\AuthCodeMailer; | |
23ff8d36 JB |
7 | |
8 | /** | |
9 | * @see https://www.pmg.com/blog/integration-testing-swift-mailer/ | |
10 | */ | |
11 | final class CountableMemorySpool extends \Swift_MemorySpool implements \Countable | |
12 | { | |
13 | public function count() | |
14 | { | |
15 | return count($this->messages); | |
16 | } | |
17 | ||
18 | public function getMessages() | |
19 | { | |
20 | return $this->messages; | |
21 | } | |
22 | } | |
23 | ||
24 | class AuthCodeMailerTest extends \PHPUnit_Framework_TestCase | |
25 | { | |
26 | protected $mailer; | |
27 | protected $spool; | |
7ce895bf | 28 | protected $twig; |
23ff8d36 JB |
29 | |
30 | protected function setUp() | |
31 | { | |
32 | $this->spool = new CountableMemorySpool(); | |
33 | $transport = new \Swift_Transport_SpoolTransport( | |
34 | new \Swift_Events_SimpleEventDispatcher(), | |
35 | $this->spool | |
36 | ); | |
37 | $this->mailer = new \Swift_Mailer($transport); | |
38 | ||
7ce895bf JB |
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 %} | |
43 | '))); | |
23ff8d36 JB |
44 | } |
45 | ||
46 | public function testSendEmail() | |
47 | { | |
48 | $user = new User(); | |
49 | $user->setTwoFactorAuthentication(true); | |
50 | $user->setEmailAuthCode(666666); | |
51 | $user->setEmail('test@wallabag.io'); | |
52 | $user->setName('Bob'); | |
53 | ||
54 | $authCodeMailer = new AuthCodeMailer( | |
55 | $this->mailer, | |
7ce895bf | 56 | $this->twig, |
23ff8d36 JB |
57 | 'nobody@test.io', |
58 | 'wallabag test', | |
59 | 'http://0.0.0.0' | |
60 | ); | |
61 | ||
62 | $authCodeMailer->sendAuthCode($user); | |
63 | ||
64 | $this->assertCount(1, $this->spool); | |
65 | ||
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()); | |
7ce895bf JB |
69 | $this->assertEquals('subject', $msg->getSubject()); |
70 | $this->assertContains('text body', $msg->toString()); | |
71 | $this->assertContains('html body', $msg->toString()); | |
23ff8d36 JB |
72 | } |
73 | } |