Skip to content

Commit 1e0d1d7

Browse files
alexander-schranzgreg0ireostrolucky
authored
Fix default MySQL charset with doctrine/dbal > 2 (#1481)
Co-authored-by: Grégoire Paris <postmaster@greg0ire.fr> Co-authored-by: Gabriel Ostrolucký <gabriel.ostrolucky@gmail.com>
1 parent eb7f3a2 commit 1e0d1d7

File tree

3 files changed

+18
-11
lines changed

3 files changed

+18
-11
lines changed

ConnectionFactory.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@
55
use Doctrine\Common\EventManager;
66
use Doctrine\DBAL\Configuration;
77
use Doctrine\DBAL\Connection;
8-
use Doctrine\DBAL\Driver\AbstractMySQLDriver;
98
use Doctrine\DBAL\DriverManager;
109
use Doctrine\DBAL\Exception as DBALException;
1110
use Doctrine\DBAL\Exception\DriverException;
11+
use Doctrine\DBAL\Platforms\AbstractMySQLPlatform;
1212
use Doctrine\DBAL\Platforms\AbstractPlatform;
13+
use Doctrine\DBAL\Platforms\MySqlPlatform;
1314
use Doctrine\DBAL\Types\Type;
1415

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

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

7982
/* PARAM_ASCII_STR_ARRAY is defined since doctrine/dbal 3.3

Tests/ConnectionFactoryTest.php

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,13 @@
1313
use Doctrine\DBAL\Exception\DriverException;
1414
use Doctrine\DBAL\Platforms\AbstractPlatform;
1515
use Doctrine\DBAL\Platforms\MySQLPlatform;
16+
use Doctrine\DBAL\Platforms\SqlitePlatform;
1617
use Doctrine\DBAL\Schema\AbstractSchemaManager;
1718
use Exception;
18-
use Throwable;
1919

2020
use function array_intersect_key;
2121
use function class_exists;
2222
use function defined;
23-
use function strpos;
2423

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

4847
try {
4948
$factory->createConnection($params, $config, $eventManager, $mappingTypes);
50-
} catch (Throwable $e) {
51-
$this->assertTrue(strpos($e->getMessage(), 'can circumvent this by setting') > 0);
52-
53-
throw $e;
5449
} finally {
5550
FakeDriver::$exception = null;
5651
}
5752
}
5853

59-
public function testDefaultCharset(): void
54+
public function testDefaultCharsetNonMySql(): void
6055
{
61-
$factory = new ConnectionFactory([]);
62-
$params = [
56+
FakeDriver::$platform = new SqlitePlatform();
57+
$factory = new ConnectionFactory([]);
58+
$params = [
6359
'driverClass' => FakeDriver::class,
6460
'wrapperClass' => FakeConnection::class,
6561
];

psalm.xml.dist

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,13 @@
4545
<file name="Tests/ConnectionFactoryTest.php"/>
4646
</errorLevel>
4747
</InvalidArrayOffset>
48+
<UndefinedDocblockClass>
49+
<errorLevel type="suppress">
50+
<!-- https://github.com/symfony/symfony/issues/45609 -->
51+
<referencedClass name="UnitEnum" />
52+
<directory name="DependencyInjection"/>
53+
<directory name="Tests/DependencyInjection"/>
54+
</errorLevel>
55+
</UndefinedDocblockClass>
4856
</issueHandlers>
4957
</psalm>

0 commit comments

Comments
 (0)