Unverified Commit c614747c authored by Maximilian Ruta's avatar Maximilian Ruta Committed by Luís Cobucci

Inherit charset from master connection if not set explicitly

parent 63115676
......@@ -224,7 +224,13 @@ class MasterSlaveConnection extends Connection
return $params['master'];
}
return $params['slaves'][array_rand($params['slaves'])];
$config = $params['slaves'][array_rand($params['slaves'])];
if ( ! isset($config['charset']) && isset($params['master']['charset'])) {
$config['charset'] = $params['master']['charset'];
}
return $config;
}
/**
......
......@@ -2,6 +2,7 @@
namespace Doctrine\Tests\DBAL\Functional;
use Doctrine\DBAL\Connections\MasterSlaveConnection;
use Doctrine\DBAL\DriverManager;
use Doctrine\Tests\DbalFunctionalTestCase;
......@@ -38,15 +39,52 @@ class MasterSlaveConnectionTest extends DbalFunctionalTestCase
$this->_conn->insert('master_slave_table', array('test_int' => 1));
}
public function createMasterSlaveConnection($keepSlave = false)
private function createMasterSlaveConnection(bool $keepSlave = false) : MasterSlaveConnection
{
return DriverManager::getConnection($this->createMasterSlaveConnectionParams($keepSlave));
}
private function createMasterSlaveConnectionParams(bool $keepSlave = false) : array
{
$params = $this->_conn->getParams();
$params['master'] = $params;
$params['slaves'] = array($params, $params);
$params['keepSlave'] = $keepSlave;
$params['wrapperClass'] = 'Doctrine\DBAL\Connections\MasterSlaveConnection';
$params['wrapperClass'] = MasterSlaveConnection::class;
return $params;
}
return DriverManager::getConnection($params);
public function testInheritCharsetFromMaster() : void
{
$charsets = [
'utf8',
'latin1'
];
foreach ($charsets as $charset) {
$params = $this->createMasterSlaveConnectionParams();
$params['master']['charset'] = $charset;
foreach ($params['slaves'] as $index => $slaveParams) {
if (isset($slaveParams['charset'])) {
unset($params['slaves'][$index]['charset']);
}
}
/** @var MasterSlaveConnection $conn */
$conn = DriverManager::getConnection($params);
$conn->connect('slave');
self::assertFalse($conn->isConnectedToMaster());
$clientCharset = $conn->fetchColumn('select @@character_set_client as c');
self::assertSame(
$charset,
substr(strtolower($clientCharset), 0, strlen($charset))
);
}
}
public function testMasterOnConnect()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment