Skip to content

Commit

Permalink
Fix default MySQL charset with doctrine/dbal > 2 (#1481)
Browse files Browse the repository at this point in the history
Co-authored-by: Grégoire Paris <postmaster@greg0ire.fr>
Co-authored-by: Gabriel Ostrolucký <gabriel.ostrolucky@gmail.com>
  • Loading branch information
3 people committed Mar 5, 2022
1 parent eb7f3a2 commit 1e0d1d7
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 11 deletions.
7 changes: 5 additions & 2 deletions ConnectionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
use Doctrine\Common\EventManager;
use Doctrine\DBAL\Configuration;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Driver\AbstractMySQLDriver;
use Doctrine\DBAL\DriverManager;
use Doctrine\DBAL\Exception as DBALException;
use Doctrine\DBAL\Exception\DriverException;
use Doctrine\DBAL\Platforms\AbstractMySQLPlatform;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Platforms\MySqlPlatform;
use Doctrine\DBAL\Types\Type;

use function array_merge;
Expand Down Expand Up @@ -71,9 +72,11 @@ public function createConnection(array $params, ?Configuration $config = null, ?
$connection = DriverManager::getConnection($params, $config, $eventManager);
$params = $this->addDatabaseSuffix(array_merge($connection->getParams(), $overriddenOptions));
$driver = $connection->getDriver();
$platform = $driver->getDatabasePlatform();

if (! isset($params['charset'])) {
if ($driver instanceof AbstractMySQLDriver) {
/** @psalm-suppress UndefinedClass AbstractMySQLPlatform exists since DBAL 3.x only */
if ($platform instanceof AbstractMySQLPlatform || $platform instanceof MySqlPlatform) {
$params['charset'] = 'utf8mb4';

/* PARAM_ASCII_STR_ARRAY is defined since doctrine/dbal 3.3
Expand Down
14 changes: 5 additions & 9 deletions Tests/ConnectionFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,13 @@
use Doctrine\DBAL\Exception\DriverException;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Platforms\MySQLPlatform;
use Doctrine\DBAL\Platforms\SqlitePlatform;
use Doctrine\DBAL\Schema\AbstractSchemaManager;
use Exception;
use Throwable;

use function array_intersect_key;
use function class_exists;
use function defined;
use function strpos;

// Compatibility with DBAL < 3
class_exists(\Doctrine\DBAL\Platforms\MySqlPlatform::class);
Expand All @@ -47,19 +46,16 @@ public function testContainer(): void

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

throw $e;
} finally {
FakeDriver::$exception = null;
}
}

public function testDefaultCharset(): void
public function testDefaultCharsetNonMySql(): void
{
$factory = new ConnectionFactory([]);
$params = [
FakeDriver::$platform = new SqlitePlatform();
$factory = new ConnectionFactory([]);
$params = [
'driverClass' => FakeDriver::class,
'wrapperClass' => FakeConnection::class,
];
Expand Down
8 changes: 8 additions & 0 deletions psalm.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,13 @@
<file name="Tests/ConnectionFactoryTest.php"/>
</errorLevel>
</InvalidArrayOffset>
<UndefinedDocblockClass>
<errorLevel type="suppress">
<!-- https://github.com/symfony/symfony/issues/45609 -->
<referencedClass name="UnitEnum" />
<directory name="DependencyInjection"/>
<directory name="Tests/DependencyInjection"/>
</errorLevel>
</UndefinedDocblockClass>
</issueHandlers>
</psalm>

0 comments on commit 1e0d1d7

Please sign in to comment.