Skip to content

Commit

Permalink
Misconfigured connections throw exception
Browse files Browse the repository at this point in the history
If a connection, in runtime or generator sections, isn't configured in
propel.database.connections section, it throws a specific exception,
with a more explaining message.
  • Loading branch information
cristianoc72 committed Oct 18, 2014
1 parent 2302f06 commit 4857392
Show file tree
Hide file tree
Showing 5 changed files with 247 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/Propel/Common/Config/ConfigurationManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
namespace Propel\Common\Config;

use Propel\Common\Config\Exception\InvalidArgumentException;
use Propel\Common\Config\Exception\InvalidConfigurationException;
use Propel\Common\Config\Loader\DelegatingLoader;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Config\Definition\Processor;
Expand Down Expand Up @@ -239,6 +240,18 @@ protected function process($extraConf = null)
if (!isset($this->config[$section]['defaultConnection'])) {
$this->config[$section]['defaultConnection'] = key($this->config['database']['connections']);
}

foreach ($this->config[$section]['connections'] as $connection) {
if (!array_key_exists($connection, $this->config['database']['connections'])) {
throw new InvalidConfigurationException("`$connection` isn't a valid configured connection (Section: propel.$section.connections). " .
"Please, check your configured connections in `propel.database.connections` section of your configuration file.");
}
}

if (!array_key_exists($defaultConnection = $this->config[$section]['defaultConnection'], $this->config['database']['connections'])) {
throw new InvalidConfigurationException("`$defaultConnection` isn't a valid configured connection (Section: propel.$section.defaultConnection). " .
"Please, check your configured connections in `propel.database.connections` section of your configuration file.");
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

/**
* This file is part of the Propel package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT License
*/

namespace Propel\Common\Config\Exception;

use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException as BaseConfigurationException;

class InvalidConfigurationException extends BaseConfigurationException implements ExceptionInterface
{
}
26 changes: 26 additions & 0 deletions tests/Propel/Tests/Common/Config/ConfigurationManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

class ConfigurationManagerTest extends ConfigTestCase
{
use DataProviderTrait;

/**
* Current working directory
*/
Expand Down Expand Up @@ -425,6 +427,30 @@ classname: Propel\Runtime\Connection\DebugPDO
$manager = new ConfigurationManager();
}

/**
* @dataProvider providerForInvalidConnections
*/
public function testRuntimeOrGeneratorConnectionIsNotInConfiguredConnectionsThrowsException($yamlConf, $section)
{
$this->setExpectedException("Propel\Common\Config\Exception\InvalidConfigurationException",
"`wrongsource` isn't a valid configured connection (Section: propel.$section.connections).");

$this->getFilesystem()->dumpFile('propel.yaml', $yamlConf);
$manager = new ConfigurationManager();
}

/**
* @dataProvider providerForInvalidDefaultConnection
*/
public function testRuntimeOrGeneratorDefaultConnectionIsNotInConfiguredConnectionsThrowsException($yamlConf, $section)
{
$this->setExpectedException("Propel\Common\Config\Exception\InvalidConfigurationException",
"`wrongsource` isn't a valid configured connection (Section: propel.$section.defaultConnection).");

$this->getFilesystem()->dumpFile('propel.yaml', $yamlConf);
$manager = new ConfigurationManager();
}

public function testLoadValidConfigurationFile()
{
$yamlConf = <<<EOF
Expand Down
179 changes: 179 additions & 0 deletions tests/Propel/Tests/Common/Config/DataProviderTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
<?php

/**
* This file is part of the Propel package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT License
*/

namespace Propel\Tests\Common\Config;

/**
* This trait contains the data providers for ConfigurationManagerTest class.
*/
trait DataProviderTrait
{
public function providerForInvalidConnections()
{
return array(
array("
propel:
database:
connections:
mysource:
adapter: mysql
classname: Propel\Runtime\Connection\DebugPDO
dsn: mysql:host=localhost;dbname=mydb
user: root
password:
runtime:
defaultConnection: wrongsource
connections:
- wrongsource
"
, 'runtime'),
array("
propel:
database:
connections:
mysource:
adapter: mysql
classname: Propel\Runtime\Connection\DebugPDO
dsn: mysql:host=localhost;dbname=mydb
user: root
password:
runtime:
defaultConnection: wrongsource
connections:
- mysource
- wrongsource
"
, 'runtime'),
array("
propel:
database:
connections:
mysource:
adapter: mysql
classname: Propel\Runtime\Connection\DebugPDO
dsn: mysql:host=localhost;dbname=mydb
user: root
password:
generator:
defaultConnection: wrongsource
connections:
- wrongsource
"
, 'generator'),
array("
propel:
database:
connections:
mysource:
adapter: mysql
classname: Propel\Runtime\Connection\DebugPDO
dsn: mysql:host=localhost;dbname=mydb
user: root
password:
generator:
defaultConnection: wrongsource
connections:
- wrongsource
- mysource
"
, 'generator'),
array("
propel:
database:
connections:
mysource:
adapter: mysql
classname: Propel\Runtime\Connection\DebugPDO
dsn: mysql:host=localhost;dbname=mydb
user: root
password:
generator:
defaultConnection: wrongsource
connections:
- wrongsource
runtime:
defaultConnection: wrongsource
connections:
- wrongsource
"
, 'runtime'),
);
}

public function providerForInvalidDefaultConnection()
{
return array(
array("
propel:
database:
connections:
mysource:
adapter: mysql
classname: Propel\Runtime\Connection\DebugPDO
dsn: mysql:host=localhost;dbname=mydb
user: root
password:
runtime:
defaultConnection: wrongsource
connections:
- mysource
"
, 'runtime'),
array("
propel:
database:
connections:
mysource:
adapter: mysql
classname: Propel\Runtime\Connection\DebugPDO
dsn: mysql:host=localhost;dbname=mydb
user: root
password:
generator:
defaultConnection: wrongsource
connections:
- mysource
"
, 'generator'),
array("
propel:
database:
connections:
mysource:
adapter: mysql
classname: Propel\Runtime\Connection\DebugPDO
dsn: mysql:host=localhost;dbname=mydb
user: root
password:
generator:
defaultConnection: wrongsource
connections:
- mysource
runtime:
defaultConnection: wrongsource
connections:
- mysource
"
, 'runtime'),
);
}
}
12 changes: 12 additions & 0 deletions tests/Propel/Tests/Generator/Config/QuickGeneratorConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,16 @@ public function testPassExtraConfigProperties()
{
$extraConf = array(
'propel' => array(
'database' => array(
'connections' => array(
'fakeConn' => array(
'adapter' => 'sqlite',
'dsn' => 'sqlite:fakeDb.sqlite',
'user'=> '',
'password' => ''
)
)
),
'runtime' => array(
'defaultConnection' => 'fakeConn',
'connections' => array('fakeConn', 'default')
Expand All @@ -77,6 +87,8 @@ public function testPassExtraConfigProperties()
$this->assertEquals('path/to/composer', $generatorConfig->get()['paths']['composerDir']);
$this->assertEquals('fakeConn', $generatorConfig->get()['runtime']['defaultConnection']);
$this->assertEquals(array('fakeConn', 'default'), $generatorConfig->get()['runtime']['connections']);
$this->assertEquals(array('adapter' => 'sqlite', 'classname' => '\Propel\Runtime\Connection\ConnectionWrapper',
'dsn' => 'sqlite:fakeDb.sqlite', 'user' => '', 'password' => ''), $generatorConfig->get()['database']['connections']['fakeConn']);
$this->assertEquals(array('adapter' => 'sqlite','classname' => 'Propel\Runtime\Connection\DebugPDO','dsn' => 'sqlite::memory:','user' => '',
'password' => ''), $generatorConfig->get()['database']['connections']['default']);
}
Expand Down

0 comments on commit 4857392

Please sign in to comment.