|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the FOSUserBundle package. |
| 5 | + * |
| 6 | + * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace FOS\UserBundle\Tests\Mailer; |
| 13 | + |
| 14 | +use FOS\UserBundle\Command\ActivateUserCommand; |
| 15 | +use FOS\UserBundle\Mailer\TwigSwiftMailer; |
| 16 | +use FOS\UserBundle\Model\UserInterface; |
| 17 | +use Swift_Events_EventDispatcher; |
| 18 | +use Swift_Mailer; |
| 19 | +use Swift_Transport_NullTransport; |
| 20 | +use Symfony\Component\Console\Application; |
| 21 | +use Symfony\Component\Console\Helper\HelperSet; |
| 22 | +use Symfony\Component\Console\Tester\CommandTester; |
| 23 | +use Symfony\Component\DependencyInjection\ContainerInterface; |
| 24 | +use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
| 25 | +use Twig_Environment; |
| 26 | +use Twig_Template; |
| 27 | + |
| 28 | +class TwigSwiftMailerTest extends \PHPUnit_Framework_TestCase |
| 29 | +{ |
| 30 | + /** |
| 31 | + * @dataProvider goodEmailProvider |
| 32 | + */ |
| 33 | + public function testSendConfirmationEmailMessageWithGoodEmails($emailAddress) |
| 34 | + { |
| 35 | + $mailer = $this->getTwigSwiftMailer(); |
| 36 | + $mailer->sendConfirmationEmailMessage($this->getUser($emailAddress)); |
| 37 | + |
| 38 | + $this->assertTrue(true); |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * @dataProvider badEmailProvider |
| 43 | + * @expectedException Swift_RfcComplianceException |
| 44 | + */ |
| 45 | + public function testSendConfirmationEmailMessageWithBadEmails($emailAddress) |
| 46 | + { |
| 47 | + $mailer = $this->getTwigSwiftMailer(); |
| 48 | + $mailer->sendConfirmationEmailMessage($this->getUser($emailAddress)); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * @dataProvider goodEmailProvider |
| 53 | + */ |
| 54 | + public function testSendResettingEmailMessageWithGoodEmails($emailAddress) |
| 55 | + { |
| 56 | + $mailer = $this->getTwigSwiftMailer(); |
| 57 | + $mailer->sendResettingEmailMessage($this->getUser($emailAddress)); |
| 58 | + |
| 59 | + $this->assertTrue(true); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * @dataProvider badEmailProvider |
| 64 | + * @expectedException Swift_RfcComplianceException |
| 65 | + */ |
| 66 | + public function testSendResettingEmailMessageWithBadEmails($emailAddress) |
| 67 | + { |
| 68 | + $mailer = $this->getTwigSwiftMailer(); |
| 69 | + $mailer->sendResettingEmailMessage($this->getUser($emailAddress)); |
| 70 | + } |
| 71 | + |
| 72 | + private function getTwigSwiftMailer() |
| 73 | + { |
| 74 | + return new TwigSwiftMailer( |
| 75 | + new Swift_Mailer( |
| 76 | + new Swift_Transport_NullTransport( |
| 77 | + $this->getMock('Swift_Events_EventDispatcher') |
| 78 | + ) |
| 79 | + ), |
| 80 | + $this->getMock('Symfony\Component\Routing\Generator\UrlGeneratorInterface'), |
| 81 | + $this->getTwigEnvironment(), |
| 82 | + array( |
| 83 | + 'template' => array( |
| 84 | + 'confirmation' => 'foo', |
| 85 | + 'resetting' => 'foo', |
| 86 | + ), |
| 87 | + 'from_email' => array( |
| 88 | + 'confirmation' => '[email protected]', |
| 89 | + 'resetting' => '[email protected]', |
| 90 | + ), |
| 91 | + ) |
| 92 | + ); |
| 93 | + } |
| 94 | + |
| 95 | + private function getTwigEnvironment() |
| 96 | + { |
| 97 | + $twigEnvironment = $this->getMockBuilder('Twig_Environment') |
| 98 | + ->disableOriginalConstructor() |
| 99 | + ->getMock() |
| 100 | + ; |
| 101 | + |
| 102 | + $twigEnvironment->method('mergeGlobals') |
| 103 | + ->willReturn(array()) |
| 104 | + ; |
| 105 | + |
| 106 | + $twigEnvironment->method('loadTemplate') |
| 107 | + ->willReturn($this->getTwigTemplate()) |
| 108 | + ; |
| 109 | + |
| 110 | + return $twigEnvironment; |
| 111 | + } |
| 112 | + |
| 113 | + private function getTwigTemplate() |
| 114 | + { |
| 115 | + // Using this method of building a mock due to a possible bug in phpunit |
| 116 | + // see http://tinyurl.com/gtybc3b |
| 117 | + $methods = get_class_methods('Twig_Template'); |
| 118 | + $twigTemplate = $this->getMockBuilder('Twig_Template') |
| 119 | + ->disableOriginalConstructor() |
| 120 | + ->setMethods($methods) |
| 121 | + ->getMockForAbstractClass() |
| 122 | + ; |
| 123 | + |
| 124 | + return $twigTemplate; |
| 125 | + } |
| 126 | + |
| 127 | + private function getUser($emailAddress) |
| 128 | + { |
| 129 | + $user = $this->getMock('FOS\UserBundle\Model\UserInterface'); |
| 130 | + $user->method('getEmail') |
| 131 | + ->willReturn($emailAddress) |
| 132 | + ; |
| 133 | + |
| 134 | + return $user; |
| 135 | + } |
| 136 | + |
| 137 | + private function getEmailAddressValueObject($emailAddressAsString) |
| 138 | + { |
| 139 | + $emailAddress = $this->getMock('EmailAddress', array( |
| 140 | + '__toString', |
| 141 | + )); |
| 142 | + |
| 143 | + $emailAddress->method('__toString') |
| 144 | + ->willReturn($emailAddressAsString) |
| 145 | + ; |
| 146 | + |
| 147 | + return $emailAddress; |
| 148 | + } |
| 149 | + |
| 150 | + public function goodEmailProvider() |
| 151 | + { |
| 152 | + return array( |
| 153 | + |
| 154 | + |
| 155 | + array( $this-> getEmailAddressValueObject( '[email protected]')), |
| 156 | + array( $this-> getEmailAddressValueObject( '[email protected]')), |
| 157 | + ); |
| 158 | + } |
| 159 | + |
| 160 | + public function badEmailProvider() |
| 161 | + { |
| 162 | + return array( |
| 163 | + array('foo'), |
| 164 | + array($this->getEmailAddressValueObject('foo')), |
| 165 | + ); |
| 166 | + } |
| 167 | +} |
0 commit comments