diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index 8dbe78cc..d45112c7 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -83,6 +83,13 @@ public function getConfigTreeBuilder() ->end() ->end() ->scalarNode('delivery_address')->end() + ->end() + ->fixXmlConfig('delivery_whitelist_pattern', 'delivery_whitelist') + ->children() + ->arrayNode('delivery_whitelist') + ->prototype('scalar') + ->end() + ->end() ->booleanNode('disable_delivery')->end() ->booleanNode('logging')->defaultValue($this->debug)->end() ->end() diff --git a/DependencyInjection/SwiftmailerExtension.php b/DependencyInjection/SwiftmailerExtension.php index ef581911..bd477fca 100644 --- a/DependencyInjection/SwiftmailerExtension.php +++ b/DependencyInjection/SwiftmailerExtension.php @@ -126,6 +126,7 @@ public function load(array $configs, ContainerBuilder $container) } else { $container->setParameter('swiftmailer.single_address', null); } + $container->setParameter('swiftmailer.delivery_whitelist', $config['delivery_whitelist']); } /** diff --git a/Resources/config/schema/swiftmailer-1.0.xsd b/Resources/config/schema/swiftmailer-1.0.xsd index 9bc89ef4..5f73a127 100644 --- a/Resources/config/schema/swiftmailer-1.0.xsd +++ b/Resources/config/schema/swiftmailer-1.0.xsd @@ -8,10 +8,11 @@ - - + - + + + diff --git a/Resources/config/swiftmailer.xml b/Resources/config/swiftmailer.xml index 57753bf6..09c705c6 100644 --- a/Resources/config/swiftmailer.xml +++ b/Resources/config/swiftmailer.xml @@ -63,6 +63,7 @@ %swiftmailer.single_address% + %swiftmailer.delivery_whitelist% diff --git a/Tests/DependencyInjection/Fixtures/config/php/antiflood.php b/Tests/DependencyInjection/Fixtures/config/php/antiflood.php new file mode 100644 index 00000000..f87b678f --- /dev/null +++ b/Tests/DependencyInjection/Fixtures/config/php/antiflood.php @@ -0,0 +1,4 @@ +loadFromExtension('swiftmailer', array( + 'antiflood' => true +)); \ No newline at end of file diff --git a/Tests/DependencyInjection/Fixtures/config/php/empty.php b/Tests/DependencyInjection/Fixtures/config/php/empty.php new file mode 100644 index 00000000..92165e87 --- /dev/null +++ b/Tests/DependencyInjection/Fixtures/config/php/empty.php @@ -0,0 +1,2 @@ +loadFromExtension('swiftmailer', array()); \ No newline at end of file diff --git a/Tests/DependencyInjection/Fixtures/config/php/full.php b/Tests/DependencyInjection/Fixtures/config/php/full.php new file mode 100644 index 00000000..cd02ef4b --- /dev/null +++ b/Tests/DependencyInjection/Fixtures/config/php/full.php @@ -0,0 +1,16 @@ +loadFromExtension('swiftmailer', array( + 'transport' => "smtp", + 'username' =>"user", + 'password' => "pass", + 'host' => "example.org", + 'port' => "12345", + 'encryption' => "tls", + 'auth-mode' => "login", + 'timeout' => "1000", + 'source_ip' => "127.0.0.1", + 'logging' => true, + 'spool' => array('type' => 'memory'), + 'delivery_address' => 'single@host.com', + 'delivery_whitelist' => array('/foo@.*/', '/.*@bar.com$/'), +)); \ No newline at end of file diff --git a/Tests/DependencyInjection/Fixtures/config/php/null.php b/Tests/DependencyInjection/Fixtures/config/php/null.php new file mode 100644 index 00000000..503cbb1c --- /dev/null +++ b/Tests/DependencyInjection/Fixtures/config/php/null.php @@ -0,0 +1,4 @@ +loadFromExtension('swiftmailer', array( + 'transport' => null +)); \ No newline at end of file diff --git a/Tests/DependencyInjection/Fixtures/config/php/redirect.php b/Tests/DependencyInjection/Fixtures/config/php/redirect.php new file mode 100644 index 00000000..7e0f5724 --- /dev/null +++ b/Tests/DependencyInjection/Fixtures/config/php/redirect.php @@ -0,0 +1,5 @@ +loadFromExtension('swiftmailer', array( + 'delivery_address' => 'single@host.com', + 'delivery_whitelist' => array('/foo@.*/', '/.*@bar.com$/'), +)); \ No newline at end of file diff --git a/Tests/DependencyInjection/Fixtures/config/php/redirect_single.php b/Tests/DependencyInjection/Fixtures/config/php/redirect_single.php new file mode 100644 index 00000000..bfe2f3ef --- /dev/null +++ b/Tests/DependencyInjection/Fixtures/config/php/redirect_single.php @@ -0,0 +1,5 @@ +loadFromExtension('swiftmailer', array( + 'delivery_address' => 'single@host.com', + 'delivery_whitelist' => array('/foo@.*/'), +)); \ No newline at end of file diff --git a/Tests/DependencyInjection/Fixtures/config/php/smtp.php b/Tests/DependencyInjection/Fixtures/config/php/smtp.php new file mode 100644 index 00000000..0441d3c2 --- /dev/null +++ b/Tests/DependencyInjection/Fixtures/config/php/smtp.php @@ -0,0 +1,13 @@ +loadFromExtension('swiftmailer', array( + 'transport' => "smtp", + 'username' =>"user", + 'password' => "pass", + 'host' => "example.org", + 'port' => "12345", + 'encryption' => "tls", + 'auth-mode' => "login", + 'timeout' => "1000", + 'source_ip' => "127.0.0.1", + 'logging' => true, +)); \ No newline at end of file diff --git a/Tests/DependencyInjection/Fixtures/config/php/spool.php b/Tests/DependencyInjection/Fixtures/config/php/spool.php new file mode 100644 index 00000000..075efe89 --- /dev/null +++ b/Tests/DependencyInjection/Fixtures/config/php/spool.php @@ -0,0 +1,4 @@ +loadFromExtension('swiftmailer', array( + 'spool' => true +)); \ No newline at end of file diff --git a/Tests/DependencyInjection/Fixtures/config/php/spool_memory.php b/Tests/DependencyInjection/Fixtures/config/php/spool_memory.php new file mode 100644 index 00000000..3d181dda --- /dev/null +++ b/Tests/DependencyInjection/Fixtures/config/php/spool_memory.php @@ -0,0 +1,4 @@ +loadFromExtension('swiftmailer', array( + 'spool' => array('type' => 'memory') +)); \ No newline at end of file diff --git a/Tests/DependencyInjection/Fixtures/config/xml/antiflood.xml b/Tests/DependencyInjection/Fixtures/config/xml/antiflood.xml new file mode 100644 index 00000000..f93e824a --- /dev/null +++ b/Tests/DependencyInjection/Fixtures/config/xml/antiflood.xml @@ -0,0 +1,11 @@ + + + + + + + \ No newline at end of file diff --git a/Tests/DependencyInjection/Fixtures/config/xml/empty.xml b/Tests/DependencyInjection/Fixtures/config/xml/empty.xml new file mode 100644 index 00000000..f917cbaa --- /dev/null +++ b/Tests/DependencyInjection/Fixtures/config/xml/empty.xml @@ -0,0 +1,9 @@ + + + + + \ No newline at end of file diff --git a/Tests/DependencyInjection/Fixtures/config/xml/full.xml b/Tests/DependencyInjection/Fixtures/config/xml/full.xml new file mode 100644 index 00000000..46031ae1 --- /dev/null +++ b/Tests/DependencyInjection/Fixtures/config/xml/full.xml @@ -0,0 +1,25 @@ + + + + + + + /foo@.*/ + /.*@bar.com$/ + + \ No newline at end of file diff --git a/Tests/DependencyInjection/Fixtures/config/xml/null.xml b/Tests/DependencyInjection/Fixtures/config/xml/null.xml new file mode 100644 index 00000000..d0b000dd --- /dev/null +++ b/Tests/DependencyInjection/Fixtures/config/xml/null.xml @@ -0,0 +1,9 @@ + + + + + \ No newline at end of file diff --git a/Tests/DependencyInjection/Fixtures/config/xml/redirect.xml b/Tests/DependencyInjection/Fixtures/config/xml/redirect.xml new file mode 100644 index 00000000..f5f31765 --- /dev/null +++ b/Tests/DependencyInjection/Fixtures/config/xml/redirect.xml @@ -0,0 +1,12 @@ + + + + + /foo@.*/ + /.*@bar.com$/ + + \ No newline at end of file diff --git a/Tests/DependencyInjection/Fixtures/config/xml/redirect_single.xml b/Tests/DependencyInjection/Fixtures/config/xml/redirect_single.xml new file mode 100644 index 00000000..77b04fbc --- /dev/null +++ b/Tests/DependencyInjection/Fixtures/config/xml/redirect_single.xml @@ -0,0 +1,11 @@ + + + + + /foo@.*/ + + \ No newline at end of file diff --git a/Tests/DependencyInjection/Fixtures/config/xml/smtp.xml b/Tests/DependencyInjection/Fixtures/config/xml/smtp.xml new file mode 100644 index 00000000..6c28b302 --- /dev/null +++ b/Tests/DependencyInjection/Fixtures/config/xml/smtp.xml @@ -0,0 +1,20 @@ + + + + + + \ No newline at end of file diff --git a/Tests/DependencyInjection/Fixtures/config/xml/spool.xml b/Tests/DependencyInjection/Fixtures/config/xml/spool.xml new file mode 100644 index 00000000..a0363a60 --- /dev/null +++ b/Tests/DependencyInjection/Fixtures/config/xml/spool.xml @@ -0,0 +1,11 @@ + + + + + + + \ No newline at end of file diff --git a/Tests/DependencyInjection/Fixtures/config/xml/spool_memory.xml b/Tests/DependencyInjection/Fixtures/config/xml/spool_memory.xml new file mode 100644 index 00000000..1167391d --- /dev/null +++ b/Tests/DependencyInjection/Fixtures/config/xml/spool_memory.xml @@ -0,0 +1,11 @@ + + + + + + + \ No newline at end of file diff --git a/Tests/DependencyInjection/Fixtures/config/yml/antiflood.yml b/Tests/DependencyInjection/Fixtures/config/yml/antiflood.yml new file mode 100644 index 00000000..26d8ac05 --- /dev/null +++ b/Tests/DependencyInjection/Fixtures/config/yml/antiflood.yml @@ -0,0 +1,2 @@ +swiftmailer: + antiflood: true \ No newline at end of file diff --git a/Tests/DependencyInjection/Fixtures/config/yml/empty.yml b/Tests/DependencyInjection/Fixtures/config/yml/empty.yml new file mode 100644 index 00000000..21e89e52 --- /dev/null +++ b/Tests/DependencyInjection/Fixtures/config/yml/empty.yml @@ -0,0 +1 @@ +swiftmailer: \ No newline at end of file diff --git a/Tests/DependencyInjection/Fixtures/config/yml/full.yml b/Tests/DependencyInjection/Fixtures/config/yml/full.yml new file mode 100644 index 00000000..7b5eea85 --- /dev/null +++ b/Tests/DependencyInjection/Fixtures/config/yml/full.yml @@ -0,0 +1,17 @@ +swiftmailer: + transport: smtp + username: user + password: pass + host: example.org + port: 12345 + encryption: tls + auth-mode: login + timeout: 1000 + source_ip: 127.0.0.1 + logging: true + spool: + type: memory + delivery_address: single@host.com + delivery_whitelist: + - /foo@.*/ + - /.*@bar.com$/ diff --git a/Tests/DependencyInjection/Fixtures/config/yml/null.yml b/Tests/DependencyInjection/Fixtures/config/yml/null.yml new file mode 100644 index 00000000..a15653e6 --- /dev/null +++ b/Tests/DependencyInjection/Fixtures/config/yml/null.yml @@ -0,0 +1,2 @@ +swiftmailer: + transport: ~ \ No newline at end of file diff --git a/Tests/DependencyInjection/Fixtures/config/yml/redirect.yml b/Tests/DependencyInjection/Fixtures/config/yml/redirect.yml new file mode 100644 index 00000000..b42bfccb --- /dev/null +++ b/Tests/DependencyInjection/Fixtures/config/yml/redirect.yml @@ -0,0 +1,5 @@ +swiftmailer: + delivery_address: single@host.com + delivery_whitelist: + - /foo@.*/ + - /.*@bar.com$/ diff --git a/Tests/DependencyInjection/Fixtures/config/yml/redirect_single.yml b/Tests/DependencyInjection/Fixtures/config/yml/redirect_single.yml new file mode 100644 index 00000000..1653424a --- /dev/null +++ b/Tests/DependencyInjection/Fixtures/config/yml/redirect_single.yml @@ -0,0 +1,4 @@ +swiftmailer: + delivery_address: single@host.com + delivery_whitelist: + - /foo@.*/ \ No newline at end of file diff --git a/Tests/DependencyInjection/Fixtures/config/yml/smtp.yml b/Tests/DependencyInjection/Fixtures/config/yml/smtp.yml new file mode 100644 index 00000000..b508e4a1 --- /dev/null +++ b/Tests/DependencyInjection/Fixtures/config/yml/smtp.yml @@ -0,0 +1,11 @@ +swiftmailer: + transport: smtp + username: user + password: pass + host: example.org + port: 12345 + encryption: tls + auth-mode: login + timeout: 1000 + source_ip: 127.0.0.1 + logging: true diff --git a/Tests/DependencyInjection/Fixtures/config/yml/spool.yml b/Tests/DependencyInjection/Fixtures/config/yml/spool.yml new file mode 100644 index 00000000..3cee3550 --- /dev/null +++ b/Tests/DependencyInjection/Fixtures/config/yml/spool.yml @@ -0,0 +1,2 @@ +swiftmailer: + spool: true \ No newline at end of file diff --git a/Tests/DependencyInjection/Fixtures/config/yml/spool_memory.yml b/Tests/DependencyInjection/Fixtures/config/yml/spool_memory.yml new file mode 100644 index 00000000..4ab4932c --- /dev/null +++ b/Tests/DependencyInjection/Fixtures/config/yml/spool_memory.yml @@ -0,0 +1,3 @@ +swiftmailer: + spool: + type: memory \ No newline at end of file diff --git a/Tests/DependencyInjection/SwiftmailerExtensionTest.php b/Tests/DependencyInjection/SwiftmailerExtensionTest.php index 9d72ddf3..3686ce43 100644 --- a/Tests/DependencyInjection/SwiftmailerExtensionTest.php +++ b/Tests/DependencyInjection/SwiftmailerExtensionTest.php @@ -14,44 +14,178 @@ use Symfony\Bundle\SwiftmailerBundle\Tests\TestCase; use Symfony\Bundle\SwiftmailerBundle\DependencyInjection\SwiftmailerExtension; use Symfony\Component\DependencyInjection\ContainerBuilder; +use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; +use Symfony\Component\DependencyInjection\Loader\YamlFileLoader; +use Symfony\Component\DependencyInjection\Loader\PhpFileLoader; +use Symfony\Component\Config\FileLocator; class SwiftmailerExtensionTest extends TestCase { - public function testConfigLoad() + public function getConfigTypes() { - $container = new ContainerBuilder(); - $container->setParameter('kernel.debug', false); - $loader = new SwiftmailerExtension(); - - $loader->load(array(array()), $container); - $this->assertEquals('Swift_Mailer', $container->getParameter('swiftmailer.class'), '->load() loads the swiftmailer.xml file if not already loaded'); + return array( + array('xml'), + array('php'), + array('yml') + ); + } - $loader->load(array(array('transport' => 'sendmail')), $container); - $this->assertEquals('swiftmailer.transport.sendmail', (string) $container->getAlias('swiftmailer.transport')); + /** + * @dataProvider getConfigTypes + */ + public function testDefaultConfig($type) + { + $container = $this->loadContainerFromFile('empty', $type); - $loader->load(array(array()), $container); $this->assertEquals('swiftmailer.transport.smtp', (string) $container->getAlias('swiftmailer.transport')); } - public function testNullTransport() + /** + * @dataProvider getConfigTypes + */ + public function testNullTransport($type) { - $container = new ContainerBuilder(); - $container->setParameter('kernel.debug', false); - $loader = new SwiftmailerExtension(); + $container = $this->loadContainerFromFile('null', $type); - $loader->load(array(array('transport' => null)), $container); $this->assertEquals('swiftmailer.transport.null', (string) $container->getAlias('swiftmailer.transport')); } - public function testSpool() + /** + * @dataProvider getConfigTypes + */ + public function testFull($type) { - $container = new ContainerBuilder(); - $container->setParameter('kernel.debug', false); - $loader = new SwiftmailerExtension(); + $container = $this->loadContainerFromFile('full', $type); + + + $this->assertEquals('swiftmailer.transport.spool', (string) $container->getAlias('swiftmailer.transport')); + $this->assertEquals('swiftmailer.transport.smtp', (string) $container->getAlias('swiftmailer.transport.real')); + $this->assertTrue($container->has('swiftmailer.spool.memory')); + $this->assertEquals('example.org', $container->getParameter('swiftmailer.transport.smtp.host')); + $this->assertEquals('12345', $container->getParameter('swiftmailer.transport.smtp.port')); + $this->assertEquals('tls', $container->getParameter('swiftmailer.transport.smtp.encryption')); + $this->assertEquals('user', $container->getParameter('swiftmailer.transport.smtp.username')); + $this->assertEquals('pass', $container->getParameter('swiftmailer.transport.smtp.password')); + $this->assertEquals('login', $container->getParameter('swiftmailer.transport.smtp.auth_mode')); + $this->assertEquals('1000', $container->getParameter('swiftmailer.transport.smtp.timeout')); + $this->assertEquals('127.0.0.1', $container->getParameter('swiftmailer.transport.smtp.source_ip')); + $this->assertSame(array('swiftmailer.plugin' => array(array())), $container->getDefinition('swiftmailer.plugin.redirecting')->getTags()); + $this->assertSame('single@host.com', $container->getParameter('swiftmailer.single_address')); + $this->assertEquals(array('/foo@.*/', '/.*@bar.com$/'), $container->getParameter('swiftmailer.delivery_whitelist')); + } + + /** + * @dataProvider getConfigTypes + */ + public function testSpool($type) + { + $container = $this->loadContainerFromFile('spool', $type); - $loader->load(array(array('spool' => array())), $container); $this->assertEquals('swiftmailer.transport.spool', (string) $container->getAlias('swiftmailer.transport')); $this->assertEquals('swiftmailer.transport.smtp', (string) $container->getAlias('swiftmailer.transport.real')); + $this->assertTrue($container->has('swiftmailer.spool.file'), 'Default is file based spool'); + } + + /** + * @dataProvider getConfigTypes + */ + public function testMemorySpool($type) + { + $container = $this->loadContainerFromFile('spool_memory', $type); + + $this->assertEquals('swiftmailer.transport.spool', (string) $container->getAlias('swiftmailer.transport')); + $this->assertEquals('swiftmailer.transport.smtp', (string) $container->getAlias('swiftmailer.transport.real')); + $this->assertTrue($container->has('swiftmailer.spool.memory'), 'Memory based spool is configured'); + } + + /** + * @dataProvider getConfigTypes + */ + public function testSmtpConfig($type) + { + $container = $this->loadContainerFromFile('smtp', $type); + + $this->assertEquals('swiftmailer.transport.smtp', (string) $container->getAlias('swiftmailer.transport')); + + $this->assertEquals('example.org', $container->getParameter('swiftmailer.transport.smtp.host')); + $this->assertEquals('12345', $container->getParameter('swiftmailer.transport.smtp.port')); + $this->assertEquals('tls', $container->getParameter('swiftmailer.transport.smtp.encryption')); + $this->assertEquals('user', $container->getParameter('swiftmailer.transport.smtp.username')); + $this->assertEquals('pass', $container->getParameter('swiftmailer.transport.smtp.password')); + $this->assertEquals('login', $container->getParameter('swiftmailer.transport.smtp.auth_mode')); + $this->assertEquals('1000', $container->getParameter('swiftmailer.transport.smtp.timeout')); + $this->assertEquals('127.0.0.1', $container->getParameter('swiftmailer.transport.smtp.source_ip')); + } + + /** + * @dataProvider getConfigTypes + */ + public function testRedirectionConfig($type) + { + $container = $this->loadContainerFromFile('redirect', $type); + + $this->assertSame(array('swiftmailer.plugin' => array(array())), $container->getDefinition('swiftmailer.plugin.redirecting')->getTags()); + $this->assertSame('single@host.com', $container->getParameter('swiftmailer.single_address')); + $this->assertEquals(array('/foo@.*/', '/.*@bar.com$/'), $container->getParameter('swiftmailer.delivery_whitelist')); + } + + /** + * @dataProvider getConfigTypes + */ + public function testSingleRedirectionConfig($type) + { + $container = $this->loadContainerFromFile('redirect_single', $type); + + $this->assertSame(array('swiftmailer.plugin' => array(array())), $container->getDefinition('swiftmailer.plugin.redirecting')->getTags()); + $this->assertSame('single@host.com', $container->getParameter('swiftmailer.single_address')); + $this->assertEquals(array('/foo@.*/'), $container->getParameter('swiftmailer.delivery_whitelist')); + } + + /** + * @dataProvider getConfigTypes + */ + public function testAntifloodConfig($type) + { + $container = $this->loadContainerFromFile('antiflood', $type); + + $this->assertSame(array('swiftmailer.plugin' => array(array())), $container->getDefinition('swiftmailer.plugin.antiflood')->getTags()); } + /** + * @param string $file + * @param string $type + * @return ContainerBuilder + */ + private function loadContainerFromFile($file, $type) + { + $container = new ContainerBuilder(); + + $container->setParameter('kernel.debug', false); + $container->setParameter('kernel.cache_dir', '/tmp'); + + $container->registerExtension(new SwiftmailerExtension()); + $locator = new FileLocator(__DIR__ . '/Fixtures/config/' . $type); + + switch ($type) { + case 'xml': + $loader = new XmlFileLoader($container, $locator); + break; + + case 'yml': + $loader = new YamlFileLoader($container, $locator); + break; + + case 'php': + $loader = new PhpFileLoader($container, $locator); + break; + } + + $loader->load($file . '.' . $type); + + $container->getCompilerPassConfig()->setOptimizationPasses(array()); + $container->getCompilerPassConfig()->setRemovingPasses(array()); + $container->compile(); + + return $container; + } } diff --git a/composer.json b/composer.json index efb9999c..a45cc657 100644 --- a/composer.json +++ b/composer.json @@ -23,7 +23,8 @@ "require-dev": { "symfony/dependency-injection": "2.1.*", "symfony/http-kernel": "2.1.*", - "symfony/config": "2.1.*" + "symfony/config": "2.1.*", + "symfony/yaml": "2.1.*" }, "autoload": { "psr-0": { "Symfony\\Bundle\\SwiftmailerBundle": "" } @@ -33,5 +34,6 @@ "branch-alias": { "dev-master": "2.1-dev" } - } + }, + "minimum-stability": "dev" }