|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <[email protected]> |
| 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 Symfony\Bundle\SwiftmailerBundle\Tests\DependencyInjection; |
| 13 | + |
| 14 | +use Symfony\Bundle\SwiftmailerBundle\DependencyInjection\SwiftmailerTransportFactory; |
| 15 | +use Symfony\Component\Routing\RequestContext; |
| 16 | + |
| 17 | +class SwiftmailerTransportFactoryTest extends \PHPUnit_Framework_TestCase |
| 18 | +{ |
| 19 | + public function testCreateTransportWithSmtp() |
| 20 | + { |
| 21 | + $options = array( |
| 22 | + 'transport' => 'smtp', |
| 23 | + 'username' => 'user', |
| 24 | + 'password' => 'pass', |
| 25 | + 'host' => 'host', |
| 26 | + 'port' => 1234, |
| 27 | + 'timeout' => 42, |
| 28 | + 'source_ip' => 'source_ip', |
| 29 | + 'local_domain' => 'local_domain', |
| 30 | + 'encryption' => 'encryption', |
| 31 | + 'auth_mode' => 'auth_mode', |
| 32 | + ); |
| 33 | + |
| 34 | + $transport = SwiftmailerTransportFactory::createTransport( |
| 35 | + $options, |
| 36 | + new RequestContext(), |
| 37 | + new \Swift_Events_SimpleEventDispatcher() |
| 38 | + ); |
| 39 | + $this->assertInstanceOf('Swift_Transport_EsmtpTransport', $transport); |
| 40 | + $this->assertSame($transport->getHost(), $options['host']); |
| 41 | + $this->assertSame($transport->getPort(), $options['port']); |
| 42 | + $this->assertSame($transport->getEncryption(), $options['encryption']); |
| 43 | + $this->assertSame($transport->getTimeout(), $options['timeout']); |
| 44 | + $this->assertSame($transport->getSourceIp(), $options['source_ip']); |
| 45 | + |
| 46 | + $authHandler = current($transport->getExtensionHandlers()); |
| 47 | + $this->assertSame($authHandler->getUsername(), $options['username']); |
| 48 | + $this->assertSame($authHandler->getPassword(), $options['password']); |
| 49 | + $this->assertSame($authHandler->getAuthMode(), $options['auth_mode']); |
| 50 | + } |
| 51 | + |
| 52 | + public function testCreateTransportWithSendmail() |
| 53 | + { |
| 54 | + $options = array( |
| 55 | + 'transport' => 'sendmail', |
| 56 | + ); |
| 57 | + |
| 58 | + $transport = SwiftmailerTransportFactory::createTransport( |
| 59 | + $options, |
| 60 | + new RequestContext(), |
| 61 | + new \Swift_Events_SimpleEventDispatcher() |
| 62 | + ); |
| 63 | + $this->assertInstanceOf('Swift_Transport_SendmailTransport', $transport); |
| 64 | + } |
| 65 | + |
| 66 | + public function testCreateTransportWithNull() |
| 67 | + { |
| 68 | + $options = array( |
| 69 | + 'transport' => 'null', |
| 70 | + ); |
| 71 | + |
| 72 | + $transport = SwiftmailerTransportFactory::createTransport( |
| 73 | + $options, |
| 74 | + new RequestContext(), |
| 75 | + new \Swift_Events_SimpleEventDispatcher() |
| 76 | + ); |
| 77 | + $this->assertInstanceOf('Swift_Transport_NullTransport', $transport); |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * @dataProvider optionsAndResultExpected |
| 82 | + */ |
| 83 | + public function testResolveOptions($options, $expected) |
| 84 | + { |
| 85 | + $result = SwiftmailerTransportFactory::resolveOptions($options); |
| 86 | + $this->assertEquals($expected, $result); |
| 87 | + } |
| 88 | + |
| 89 | + public function optionsAndResultExpected() |
| 90 | + { |
| 91 | + return array( |
| 92 | + array( |
| 93 | + array( |
| 94 | + 'url' => '', |
| 95 | + ), |
| 96 | + array( |
| 97 | + 'transport' => 'null', |
| 98 | + 'username' => null, |
| 99 | + 'password' => null, |
| 100 | + 'host' => null, |
| 101 | + 'port' => 25, |
| 102 | + 'timeout' => null, |
| 103 | + 'source_ip' => null, |
| 104 | + 'local_domain' => null, |
| 105 | + 'encryption' => null, |
| 106 | + 'auth_mode' => null, |
| 107 | + 'url' => '', |
| 108 | + ), |
| 109 | + ), |
| 110 | + array( |
| 111 | + array( |
| 112 | + 'url' => 'smtp://user:pass@host:1234', |
| 113 | + ), |
| 114 | + array( |
| 115 | + 'transport' => 'smtp', |
| 116 | + 'username' => 'user', |
| 117 | + 'password' => 'pass', |
| 118 | + 'host' => 'host', |
| 119 | + 'port' => 1234, |
| 120 | + 'timeout' => null, |
| 121 | + 'source_ip' => null, |
| 122 | + 'local_domain' => null, |
| 123 | + 'encryption' => null, |
| 124 | + 'auth_mode' => null, |
| 125 | + 'url' => 'smtp://user:pass@host:1234', |
| 126 | + ), |
| 127 | + ), |
| 128 | + array( |
| 129 | + array( |
| 130 | + 'url' => 'smtp://user:pass@host:1234?transport=sendmail&username=username&password=password&host=example.com&port=5678', |
| 131 | + ), |
| 132 | + array( |
| 133 | + 'transport' => 'sendmail', |
| 134 | + 'username' => 'username', |
| 135 | + 'password' => 'password', |
| 136 | + 'host' => 'example.com', |
| 137 | + 'port' => 5678, |
| 138 | + 'timeout' => null, |
| 139 | + 'source_ip' => null, |
| 140 | + 'local_domain' => null, |
| 141 | + 'encryption' => null, |
| 142 | + 'auth_mode' => null, |
| 143 | + 'url' => 'smtp://user:pass@host:1234?transport=sendmail&username=username&password=password&host=example.com&port=5678', |
| 144 | + ), |
| 145 | + ), |
| 146 | + array( |
| 147 | + array( |
| 148 | + 'url' => 'smtp://user:pass@host:1234?timeout=42&source_ip=source_ip&local_domain=local_domain&encryption=encryption&auth_mode=auth_mode', |
| 149 | + ), |
| 150 | + array( |
| 151 | + 'transport' => 'smtp', |
| 152 | + 'username' => 'user', |
| 153 | + 'password' => 'pass', |
| 154 | + 'host' => 'host', |
| 155 | + 'port' => 1234, |
| 156 | + 'timeout' => 42, |
| 157 | + 'source_ip' => 'source_ip', |
| 158 | + 'local_domain' => 'local_domain', |
| 159 | + 'encryption' => 'encryption', |
| 160 | + 'auth_mode' => 'auth_mode', |
| 161 | + 'url' => 'smtp://user:pass@host:1234?timeout=42&source_ip=source_ip&local_domain=local_domain&encryption=encryption&auth_mode=auth_mode', |
| 162 | + ), |
| 163 | + ), |
| 164 | + array( |
| 165 | + array(), |
| 166 | + array( |
| 167 | + 'transport' => 'null', |
| 168 | + 'username' => null, |
| 169 | + 'password' => null, |
| 170 | + 'host' => null, |
| 171 | + 'port' => 25, |
| 172 | + 'timeout' => null, |
| 173 | + 'source_ip' => null, |
| 174 | + 'local_domain' => null, |
| 175 | + 'encryption' => null, |
| 176 | + 'auth_mode' => null, |
| 177 | + ), |
| 178 | + ), |
| 179 | + array( |
| 180 | + array( |
| 181 | + 'transport' => 'smtp', |
| 182 | + ), |
| 183 | + array( |
| 184 | + 'transport' => 'smtp', |
| 185 | + 'username' => null, |
| 186 | + 'password' => null, |
| 187 | + 'host' => null, |
| 188 | + 'port' => 25, |
| 189 | + 'timeout' => null, |
| 190 | + 'source_ip' => null, |
| 191 | + 'local_domain' => null, |
| 192 | + 'encryption' => null, |
| 193 | + 'auth_mode' => null, |
| 194 | + ), |
| 195 | + ), |
| 196 | + array( |
| 197 | + array( |
| 198 | + 'transport' => 'gmail', |
| 199 | + ), |
| 200 | + array( |
| 201 | + 'transport' => 'smtp', |
| 202 | + 'username' => null, |
| 203 | + 'password' => null, |
| 204 | + 'host' => 'smtp.gmail.com', |
| 205 | + 'port' => 465, |
| 206 | + 'timeout' => null, |
| 207 | + 'source_ip' => null, |
| 208 | + 'local_domain' => null, |
| 209 | + 'encryption' => 'ssl', |
| 210 | + 'auth_mode' => 'login', |
| 211 | + ), |
| 212 | + ), |
| 213 | + array( |
| 214 | + array( |
| 215 | + 'transport' => 'sendmail', |
| 216 | + ), |
| 217 | + array( |
| 218 | + 'transport' => 'sendmail', |
| 219 | + 'username' => null, |
| 220 | + 'password' => null, |
| 221 | + 'host' => null, |
| 222 | + 'port' => 25, |
| 223 | + 'timeout' => null, |
| 224 | + 'source_ip' => null, |
| 225 | + 'local_domain' => null, |
| 226 | + 'encryption' => null, |
| 227 | + 'auth_mode' => null, |
| 228 | + ), |
| 229 | + ), |
| 230 | + array( |
| 231 | + array( |
| 232 | + 'encryption' => 'ssl', |
| 233 | + ), |
| 234 | + array( |
| 235 | + 'transport' => 'null', |
| 236 | + 'username' => null, |
| 237 | + 'password' => null, |
| 238 | + 'host' => null, |
| 239 | + 'port' => 465, |
| 240 | + 'timeout' => null, |
| 241 | + 'source_ip' => null, |
| 242 | + 'local_domain' => null, |
| 243 | + 'encryption' => 'ssl', |
| 244 | + 'auth_mode' => null, |
| 245 | + ), |
| 246 | + ), |
| 247 | + array( |
| 248 | + array( |
| 249 | + 'port' => 42, |
| 250 | + ), |
| 251 | + array( |
| 252 | + 'transport' => 'null', |
| 253 | + 'username' => null, |
| 254 | + 'password' => null, |
| 255 | + 'host' => null, |
| 256 | + 'port' => 42, |
| 257 | + 'timeout' => null, |
| 258 | + 'source_ip' => null, |
| 259 | + 'local_domain' => null, |
| 260 | + 'encryption' => null, |
| 261 | + 'auth_mode' => null, |
| 262 | + ), |
| 263 | + ), |
| 264 | + ); |
| 265 | + } |
| 266 | +} |
0 commit comments