From 383bb7664ec5722e9c819a4e798bf807a5575296 Mon Sep 17 00:00:00 2001 From: William DURAND Date: Wed, 6 Apr 2011 10:40:46 +0200 Subject: [PATCH] Fixed PropelExtension with unit tests --- DependencyInjection/PropelExtension.php | 77 ++-------------- .../PropelExtensionTest.php | 92 ++++++++++++++----- 2 files changed, 77 insertions(+), 92 deletions(-) diff --git a/DependencyInjection/PropelExtension.php b/DependencyInjection/PropelExtension.php index fed7e26..85c7d46 100644 --- a/DependencyInjection/PropelExtension.php +++ b/DependencyInjection/PropelExtension.php @@ -24,16 +24,6 @@ class PropelExtension extends Extension $configuration = new Configuration($container->getParameter('kernel.debug')); $config = $processor->processConfiguration($configuration, $configs); - /* - $dbal = array(); - foreach ($configs as $config) { - if (isset($config['dbal'])) { - $dbal[] = $config['dbal']; - } - } - - $config = $configs[0]; - */ if (!$container->hasParameter('propel.path')) { if (!isset($config['path'])) { throw new \InvalidArgumentException('The "path" parameter is mandatory.'); @@ -69,7 +59,7 @@ class PropelExtension extends Extension if (!empty($config['dbal'])) { $this->dbalLoad($config['dbal'], $container); } else { - throw new \RuntimeException('No "dbal" configuration found.'); + throw new \InvalidArgumentException('No "dbal" configuration found.'); } } @@ -86,65 +76,7 @@ class PropelExtension extends Extension $loader->load('propel.xml'); } - /* - $mergedConfig = array( - 'default_connection' => 'default', - ); - - if ($container->hasParameter('kernel.debug')) { - $className = $container->getParameter('kernel.debug') ? 'DebugPDO' : 'PropelPDO'; - } else { - $className = 'PropelPDO'; - } - - $defaultConnection = array( - 'driver' => 'mysql', - 'user' => 'root', - 'password' => '', - 'dsn' => '', - 'classname' => $className, - 'options' => array(), - 'attributes' => array(), - 'settings' => array('charset' => array('value' => $container->getParameter('propel.charset'))), - ); - - foreach ($configs as $config) { - if (isset($config['default-connection'])) { - $mergedConfig['default_connection'] = $config['default-connection']; - } else if (isset($config['default_connection'])) { - $mergedConfig['default_connection'] = $config['default_connection']; - } - } - - foreach ($configs as $config) { - if (isset($config['connections'])) { - $configConnections = $config['connections']; - if (isset($config['connections']['connection']) && isset($config['connections']['connection'][0])) { - $configConnections = $config['connections']['connection']; - } - } else { - $configConnections[$mergedConfig['default_connection']] = $config; - } - - foreach ($configConnections as $name => $connection) { - $connectionName = isset($connection['name']) ? $connection['name'] : $name; - if (!isset($mergedConfig['connections'][$connectionName])) { - $mergedConfig['connections'][$connectionName] = $defaultConnection; - } - - $mergedConfig['connections'][$connectionName]['name'] = $connectionName; - - foreach ($connection as $k => $v) { - if (isset($defaultConnection[$k])) { - $mergedConfig['connections'][$connectionName][$k] = null !== $v ? $v : ''; - } - } - } - } - - $config = $mergedConfig; - */ - if (empty ($config['default_connection'])) { + if (empty($config['default_connection'])) { $keys = array_keys($config['connections']); $config['default_connection'] = reset($keys); } @@ -152,8 +84,11 @@ class PropelExtension extends Extension $connectionName = $config['default_connection']; $container->setParameter('propel.dbal.default_connection', $connectionName); - $c = array(); + if (0 == count($config['connections'])) { + $config['connections'] = array($connectionName => $config); + } + $c = array(); foreach ($config['connections'] as $name => $conf) { $c['datasources'][$name]['adapter'] = $config['connections'][$name]['driver']; diff --git a/Tests/DependencyInjection/PropelExtensionTest.php b/Tests/DependencyInjection/PropelExtensionTest.php index c7c2fbe..53a4913 100644 --- a/Tests/DependencyInjection/PropelExtensionTest.php +++ b/Tests/DependencyInjection/PropelExtensionTest.php @@ -13,62 +13,109 @@ namespace Propel\PropelBundle\Tests\DependencyInjection; use Propel\PropelBundle\Tests\TestCase; use Propel\PropelBundle\DependencyInjection\PropelExtension; -use Symfony\Component\DependencyInjection\ContainerBuilder; +use Symfony\Component\DependencyInjection\Container; class PropelExtensionTest extends TestCase { public function testLoad() { - $container = new ContainerBuilder(); + $container = $this->getContainer(); $loader = new PropelExtension(); - try { $loader->load(array(array()), $container); $this->fail(); } catch (\Exception $e) { - $this->assertInstanceOf('InvalidArgumentException', $e, '->load() throws an \InvalidArgumentException if the Propel path is not set.'); + $this->assertInstanceOf('InvalidArgumentException', $e, + '->load() throws an \InvalidArgumentException if the Propel path is not set'); } - $loader->load(array(array('path' => '/propel')), $container); - $this->assertEquals('/propel', $container->getParameter('propel.path'), '->load() sets the Propel path'); + $container = $this->getContainer(); + $loader = new PropelExtension(); + try { + $loader->load(array(array( + 'path' => '/propel', + 'phing_path' => '/phing', + )), $container); + $this->fail(); + } catch (\Exception $e) { + $this->assertInstanceOf('InvalidArgumentException', $e, + '->load() throws an \InvalidArgumentException if a "dbal" configuration is not set.'); + } - $loader->load(array(array()), $container); - $this->assertEquals('/propel', $container->getParameter('propel.path'), '->load() sets the Propel path'); + $container = $this->getContainer(); + $loader = new PropelExtension(); + try { + $loader->load(array(array( + 'path' => '/propel', + 'dbal' => array(), + )), $container); + $this->fail(); + } catch (\Exception $e) { + $this->assertInstanceOf('InvalidArgumentException', $e, + '->load() throws an \InvalidArgumentException if the Phing path is not set.'); + } + + $container = $this->getContainer(); + $loader = new PropelExtension(); + $loader->load(array(array( + 'path' => '/propel', + 'phing_path' => '/phing', + 'dbal' => array() + )), $container); + $this->assertEquals('/propel', $container->getParameter('propel.path'), '->load() requires the Propel path'); + $this->assertEquals('/phing', $container->getParameter('propel.phing_path'), '->load() requires the Phing path'); } public function testDbalLoad() { - $container = new ContainerBuilder(); + $container = $this->getContainer(); $loader = new PropelExtension(); - $loader->load(array(array('path' => '/propel')), $container); - - // propel.dbal.default_connection - $this->assertEquals('default', $container->getParameter('propel.dbal.default_connection'), '->dbalLoad() overrides existing configuration options'); - $loader->load(array(array('dbal' => array('default_connection' => 'foo'))), $container); + $loader->load(array(array( + 'path' => '/propel', + 'phing_path' => '/phing', + 'dbal' => array( + 'default_connection' => 'foo', + ) + )), $container); $this->assertEquals('foo', $container->getParameter('propel.dbal.default_connection'), '->dbalLoad() overrides existing configuration options'); - $container = new ContainerBuilder(); + $container = $this->getContainer(); $loader = new PropelExtension(); - $loader->load(array(array('path' => '/propel'), array('dbal' => array('password' => 'foo'))), $container); + $loader->load(array(array( + 'path' => '/propel', + 'phing_path' => '/phing', + 'dbal' => array( + 'password' => 'foo', + ) + )), $container); $arguments = $container->getDefinition('propel.configuration')->getArguments(); $config = $arguments[0]; + $this->assertEquals('foo', $config['datasources']['default']['connection']['password']); $this->assertEquals('root', $config['datasources']['default']['connection']['user']); - $loader->load(array(array('path' => '/propel'), array('dbal' => array('user' => 'foo'))), $container); + $loader->load(array(array( + 'path' => '/propel', + 'dbal' => array( + 'user' => 'foo', + ) + )), $container); $this->assertEquals('foo', $config['datasources']['default']['connection']['password']); $this->assertEquals('root', $config['datasources']['default']['connection']['user']); } public function testDbalLoadCascade() { - $container = new ContainerBuilder(); + $container = $this->getContainer(); $loader = new PropelExtension(); - $config_base = array('path' => '/propel'); + $config_base = array( + 'path' => '/propel', + 'phing_path' => '/propel', + ); $config_prod = array('dbal' => array( 'user' => 'toto', @@ -99,10 +146,13 @@ class PropelExtensionTest extends TestCase } public function testDbalLoadMultipleConnections() { - $container = new ContainerBuilder(); + $container = $this->getContainer(); $loader = new PropelExtension(); - $config_base = array('path' => '/propel'); + $config_base = array( + 'path' => '/propel', + 'phing_path' => '/phing', + ); $config_mysql = array( 'user' => 'mysql_usr',