]>
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; | |
7 | use Symfony\Component\Translation\Translator; | |
8 | use Symfony\Component\Translation\Loader\ArrayLoader; | |
23ff8d36 JB |
9 | |
10 | /** | |
11 | * @see https://www.pmg.com/blog/integration-testing-swift-mailer/ | |
12 | */ | |
13 | final class CountableMemorySpool extends \Swift_MemorySpool implements \Countable | |
14 | { | |
15 | public function count() | |
16 | { | |
17 | return count($this->messages); | |
18 | } | |
19 | ||
20 | public function getMessages() | |
21 | { | |
22 | return $this->messages; | |
23 | } | |
24 | } | |
25 | ||
26 | class AuthCodeMailerTest extends \PHPUnit_Framework_TestCase | |
27 | { | |
28 | protected $mailer; | |
29 | protected $spool; | |
e678c475 | 30 | protected $translator; |
23ff8d36 JB |
31 | |
32 | protected function setUp() | |
33 | { | |
34 | $this->spool = new CountableMemorySpool(); | |
35 | $transport = new \Swift_Transport_SpoolTransport( | |
36 | new \Swift_Events_SimpleEventDispatcher(), | |
37 | $this->spool | |
38 | ); | |
39 | $this->mailer = new \Swift_Mailer($transport); | |
40 | ||
e678c475 JB |
41 | $this->translator = new Translator('en'); |
42 | $this->translator->addLoader('array', new ArrayLoader()); | |
43 | $this->translator->addResource('array', array( | |
23ff8d36 JB |
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'); | |
23ff8d36 JB |
47 | } |
48 | ||
49 | public function testSendEmail() | |
50 | { | |
51 | $user = new User(); | |
52 | $user->setTwoFactorAuthentication(true); | |
53 | $user->setEmailAuthCode(666666); | |
54 | $user->setEmail('test@wallabag.io'); | |
55 | $user->setName('Bob'); | |
56 | ||
57 | $authCodeMailer = new AuthCodeMailer( | |
58 | $this->mailer, | |
e678c475 | 59 | $this->translator, |
23ff8d36 JB |
60 | 'nobody@test.io', |
61 | 'wallabag test', | |
62 | 'http://0.0.0.0' | |
63 | ); | |
64 | ||
65 | $authCodeMailer->sendAuthCode($user); | |
66 | ||
67 | $this->assertCount(1, $this->spool); | |
68 | ||
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()); | |
74 | } | |
75 | } |