Skip to content

Commit

Permalink
Better check for invalid config (#41)
Browse files Browse the repository at this point in the history
* Better check for invalid config

* Added changelog
  • Loading branch information
Nyholm authored Mar 4, 2018
1 parent 39612b7 commit fc8597b
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 2 deletions.
7 changes: 7 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Changelog

## 2.1.1

### Fixed

- Allow to only use "dsn" without configure "type".
- Add better error message when both "dsn" and "type" is missing.

## 2.1.0

### Added
Expand Down
9 changes: 8 additions & 1 deletion DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,14 @@ public function getConfigTreeBuilder()
->validate()
->ifTrue(function ($databases) {
$valid = true;
foreach ($databases as $d) {
foreach ($databases as $name => $d) {
if (isset($d['dsn'])) {
// "Fake" "type" for now.
$d['type'] = substr($d['dsn'], 0, strpos($d['dsn'], ':'));
}
if (!isset($d['type'])) {
throw new InvalidConfigurationException(sprintf('You must define a "type" or "dsn" for database "%s"', $name));
}
if ($d['type'] !== 'mysql') {
// If not "mysql" we have to make sure these parameter are set to default
$valid = $valid && empty($d['ignoreTables']) && empty($d['ssl']) && empty($d['singleTransaction']);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ public function testReplacementOfConfig()
$this->assertContainerBuilderHasServiceDefinitionWithArgument('backup_manager.config_database', 0, $dbConfig);
}


public function testDsn()
{
$storageConfig = ['local'=>['type'=>'Local', 'root'=>'/foo']];
Expand Down
29 changes: 29 additions & 0 deletions Tests/Unit/DependencyInjection/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,4 +134,33 @@ public function testSingleTransactionOnValid()
)
);
}

public function testDsn()
{
$this->assertConfigurationIsInvalid(array(
[
'database'=>[
'dev'=>[
'dsn' => 'pgsql://root:[email protected]:5432/test_db',
'singleTransaction' => true,
],
],
]
)
);
}
public function testNoType()
{
$this->assertConfigurationIsInvalid(array(
[
'database'=>[
'dev'=>[

],
],
]
),
'You must define a "type" or "dsn" for database "dev"'
);
}
}

0 comments on commit fc8597b

Please sign in to comment.