Skip to content

Commit

Permalink
Merge pull request #677 from sebastianfeldmann/master
Browse files Browse the repository at this point in the history
Enhance exception message
  • Loading branch information
kimhemsoe authored Jul 25, 2017
2 parents 97a4fc3 + 88d67f1 commit 4f603e1
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 3 deletions.
38 changes: 36 additions & 2 deletions ConnectionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@

use Doctrine\Common\EventManager;
use Doctrine\DBAL\Configuration;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\DriverManager;
use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Exception\DriverException;
use Doctrine\DBAL\Types\Type;

/**
Expand Down Expand Up @@ -57,13 +60,13 @@ public function createConnection(array $params, Configuration $config = null, Ev
$connection = DriverManager::getConnection($params, $config, $eventManager);

if (!empty($mappingTypes)) {
$platform = $connection->getDatabasePlatform();
$platform = $this->getDatabasePlatform($connection);
foreach ($mappingTypes as $dbType => $doctrineType) {
$platform->registerDoctrineTypeMapping($dbType, $doctrineType);
}
}
if (!empty($this->commentedTypes)) {
$platform = $connection->getDatabasePlatform();
$platform = $this->getDatabasePlatform($connection);
foreach ($this->commentedTypes as $type) {
$platform->markDoctrineTypeCommented(Type::getType($type));
}
Expand All @@ -72,6 +75,37 @@ public function createConnection(array $params, Configuration $config = null, Ev
return $connection;
}

/**
* Try to get the database platform.
*
* This could fail if types should be registered to an predefined/unused connection
* and the platform version is unknown.
* For details have a look at DoctrineBundle issue #673.
*
* @param \Doctrine\DBAL\Connection $connection
*
* @return \Doctrine\DBAL\Platforms\AbstractPlatform
* @throws \Doctrine\DBAL\DBALException
*/
private function getDatabasePlatform(Connection $connection)
{
try {
return $connection->getDatabasePlatform();
} catch (DBALException $driverException) {
if ($driverException instanceof DriverException) {
throw new DBALException(
"An exception occured while establishing a connection to figure out your platform version." . PHP_EOL .
"You can circumvent this by setting a 'server_version' configuration value" . PHP_EOL . PHP_EOL .
"For further information have a look at:" . PHP_EOL .
"https://github.com/doctrine/DoctrineBundle/issues/673",
0,
$driverException
);
}
throw $driverException;
}
}

/**
* initialize the types
*/
Expand Down
106 changes: 106 additions & 0 deletions Tests/ConnectionFactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?php

/*
* This file is part of the Doctrine Bundle
*
* The code was originally distributed inside the Symfony framework.
*
* (c) Fabien Potencier <fabien@symfony.com>
* (c) Doctrine Project, Benjamin Eberlei <kontakt@beberlei.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Doctrine\Bundle\DoctrineBundle\Tests;

use Doctrine\Bundle\DoctrineBundle\ConnectionFactory;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Driver;
use Doctrine\DBAL\Exception\DriverException;

class ConnectionFactoryTest extends TestCase
{
protected function setUp()
{
parent::setUp();

if (!class_exists('Doctrine\\ORM\\Version')) {
$this->markTestSkipped('Doctrine ORM is not available.');
}
}

/**
* @expectedException \Doctrine\DBAL\DBALException
*/
public function testContainer()
{
$typesConfig = [];
$factory = new ConnectionFactory($typesConfig);
$params = ['driverClass' => '\\Doctrine\\Bundle\\DoctrineBundle\\Tests\\FakeDriver'];
$config = null;
$eventManager = null;
$mappingTypes = [0];
$exception = new DriverException('', $this->createMock('\Doctrine\DBAL\Driver\DriverException'));

// put the mock into the fake driver
FakeDriver::$exception = $exception;

try {
$factory->createConnection($params, $config, $eventManager, $mappingTypes);
} catch (\Exception $e) {
$this->assertTrue(strpos($e->getMessage(), 'can circumvent this by setting') > 0);
throw $e;
}
}
}

/**
* FakeDriver class to simulate a problem discussed in DoctrineBundle issue #673
* In order to not use a real database driver we have to create our own fake/mock implementation.
*
* @link https://github.com/doctrine/DoctrineBundle/issues/673
*/
class FakeDriver implements Driver
{
/**
* Exception Mock
*
* @var \Doctrine\DBAL\Exception\DriverException
*/
public static $exception;

/**
* This method gets called to determine the database version which in our case leeds to the problem.
* So we have to fake the exception a driver would normally throw.
*
*
* @link https://github.com/doctrine/DoctrineBundle/issues/673
*/
public function getDatabasePlatform()
{
throw self::$exception;
}

// ----- below this line follow only dummy methods to satisfy the interface requirements ----

public function connect(array $params, $username = null, $password = null, array $driverOptions = [])
{
throw new \Exception('not implemented');
}

public function getSchemaManager(Connection $conn)
{
throw new \Exception('not implemented');
}

public function getName()
{
return 'FakeDriver';
}

public function getDatabase(Connection $conn)
{
return 'fake_db';
}
}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"symfony/framework-bundle": "~2.7|~3.0|~4.0",
"symfony/console": "~2.7|~3.0|~4.0",
"symfony/dependency-injection": "~2.7|~3.0|~4.0",
"doctrine/dbal": "~2.3",
"doctrine/dbal": "^2.5.12",
"jdorn/sql-formatter": "~1.1",
"symfony/doctrine-bridge": "~2.7|~3.0|~4.0",
"doctrine/doctrine-cache-bundle": "~1.2"
Expand Down

0 comments on commit 4f603e1

Please sign in to comment.