Skip to content

Commit

Permalink
Fix creating wrapped connection twice
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolas-grekas authored and alcaeus committed Dec 17, 2019
1 parent 68b3b0b commit 6f9489c
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 8 deletions.
31 changes: 25 additions & 6 deletions ConnectionFactory.php
Expand Up @@ -11,6 +11,7 @@
use Doctrine\DBAL\Exception\DriverException;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\Type;
use function is_subclass_of;

class ConnectionFactory
{
Expand Down Expand Up @@ -42,22 +43,40 @@ public function createConnection(array $params, Configuration $config = null, Ev
$this->initializeTypes();
}

$connection = DriverManager::getConnection($params, $config, $eventManager);

if (! isset($params['pdo']) && ! isset($params['charset'])) {
$params = $connection->getParams();
$params['charset'] = 'utf8';
$driver = $connection->getDriver();
$wrapperClass = null;
if (isset($params['wrapperClass'])) {
if (! is_subclass_of($params['wrapperClass'], Connection::class)) {
throw DBALException::invalidWrapperClass($params['wrapperClass']);
}

$wrapperClass = $params['wrapperClass'];
$params['wrapperClass'] = null;
}

$connection = DriverManager::getConnection($params, $config, $eventManager);
$params = $connection->getParams();
$driver = $connection->getDriver();

if ($driver instanceof AbstractMySQLDriver) {
$params['charset'] = 'utf8mb4';

if (! isset($params['defaultTableOptions']['collate'])) {
$params['defaultTableOptions']['collate'] = 'utf8mb4_unicode_ci';
}
} else {
$params['charset'] = 'utf8';
}

if ($wrapperClass !== null) {
$params['wrapperClass'] = $wrapperClass;
} else {
$wrapperClass = Connection::class;
}

$connection = new $connection($params, $driver, $connection->getConfiguration(), $connection->getEventManager());
$connection = new $wrapperClass($params, $driver, $config, $eventManager);
} else {
$connection = DriverManager::getConnection($params, $config, $eventManager);
}

if (! empty($mappingTypes)) {
Expand Down
23 changes: 21 additions & 2 deletions Tests/ConnectionFactoryTest.php
Expand Up @@ -41,11 +41,17 @@ public function testContainer()
public function testDefaultCharset()
{
$factory = new ConnectionFactory([]);
$params = ['driverClass' => FakeDriver::class];
$params = [
'driverClass' => FakeDriver::class,
'wrapperClass' => FakeConnection::class,
];

$connection = $factory->createConnection($params);
$creationCount = FakeConnection::$creationCount;
$connection = $factory->createConnection($params);

$this->assertInstanceof(FakeConnection::class, $connection);
$this->assertSame('utf8', $connection->getParams()['charset']);
$this->assertSame(1 + $creationCount, FakeConnection::$creationCount);
}

public function testDefaultCharsetMySql()
Expand Down Expand Up @@ -120,3 +126,16 @@ public function getDatabase(Connection $conn)
return 'fake_db';
}
}

class FakeConnection extends Connection
{
/** @var int */
public static $creationCount = 0;

public function __construct(array $params, FakeDriver $driver, ?Configuration $config = null, ?EventManager $eventManager = null)
{
++self::$creationCount;

parent::__construct($params, $driver, $config, $eventManager);
}
}

0 comments on commit 6f9489c

Please sign in to comment.