View source
<?php
namespace Drupal\KernelTests\Core\Database;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Core\Database\Database;
use Drupal\KernelTests\KernelTestBase;
class ConnectionUnitTest extends KernelTestBase {
const TEST_TARGET_CONNECTION = 'DatabaseConnectionUnitTest';
protected $connection;
protected $monitor;
protected function setUp() {
parent::setUp();
$this->connection = Database::getConnection();
$connection_info = Database::getConnectionInfo();
Database::addConnectionInfo('default', 'monitor', $connection_info['default']);
$this->monitor = Database::getConnection('monitor');
}
protected function addConnection() {
$connection_info = Database::getConnectionInfo();
Database::addConnectionInfo('default', static::TEST_TARGET_CONNECTION, $connection_info['default']);
$info = Database::getConnectionInfo();
$this
->assertSame($connection_info['default'], $info[static::TEST_TARGET_CONNECTION]);
}
protected function getConnectionId() {
return (int) Database::getConnection(static::TEST_TARGET_CONNECTION)
->query('SELECT CONNECTION_ID()')
->fetchField();
}
protected function assertConnection($id) {
$list = $this->monitor
->query('SHOW PROCESSLIST')
->fetchAllKeyed(0, 0);
return $this
->assertTrue(isset($list[$id]), new FormattableMarkup('Connection ID @id found.', [
'@id' => $id,
]));
}
protected function assertNoConnection($id) {
$list = $this->monitor
->query('SHOW PROCESSLIST')
->fetchAllKeyed(0, 0);
return $this
->assertFalse(isset($list[$id]), new FormattableMarkup('Connection ID @id not found.', [
'@id' => $id,
]));
}
public function testOpenClose() {
$driver = $this->connection
->driver();
if ($driver !== 'mysql') {
$this
->markTestSkipped("MySql tests can not run for driver '{$driver}'.");
}
$this
->addConnection();
$id = $this
->getConnectionId();
Database::getConnection(static::TEST_TARGET_CONNECTION);
$this
->assertConnection($id);
Database::closeConnection(static::TEST_TARGET_CONNECTION);
usleep(20000);
$this
->assertNoConnection($id);
}
public function testOpenQueryClose() {
$driver = $this->connection
->driver();
if ($driver !== 'mysql') {
$this
->markTestSkipped("MySql tests can not run for driver '{$driver}'.");
}
$this
->addConnection();
$id = $this
->getConnectionId();
Database::getConnection(static::TEST_TARGET_CONNECTION);
$this
->assertConnection($id);
Database::getConnection(static::TEST_TARGET_CONNECTION)
->query('SHOW TABLES');
Database::closeConnection(static::TEST_TARGET_CONNECTION);
usleep(20000);
$this
->assertNoConnection($id);
}
public function testOpenQueryPrefetchClose() {
$driver = $this->connection
->driver();
if ($driver !== 'mysql') {
$this
->markTestSkipped("MySql tests can not run for driver '{$driver}'.");
}
$this
->addConnection();
$id = $this
->getConnectionId();
Database::getConnection(static::TEST_TARGET_CONNECTION);
$this
->assertConnection($id);
Database::getConnection(static::TEST_TARGET_CONNECTION)
->query('SHOW TABLES')
->fetchCol();
Database::closeConnection(static::TEST_TARGET_CONNECTION);
usleep(20000);
$this
->assertNoConnection($id);
}
public function testOpenSelectQueryClose() {
$driver = $this->connection
->driver();
if ($driver !== 'mysql') {
$this
->markTestSkipped("MySql tests can not run for driver '{$driver}'.");
}
$this
->addConnection();
$id = $this
->getConnectionId();
Database::getConnection(static::TEST_TARGET_CONNECTION);
$this
->assertConnection($id);
$name = 'foo';
Database::getConnection(static::TEST_TARGET_CONNECTION)
->schema()
->createTable($name, [
'fields' => [
'name' => [
'type' => 'varchar',
'length' => 255,
],
],
]);
Database::getConnection(static::TEST_TARGET_CONNECTION)
->select('foo', 'f')
->fields('f', [
'name',
])
->execute()
->fetchAll();
Database::getConnection(static::TEST_TARGET_CONNECTION)
->schema()
->dropTable($name);
Database::closeConnection(static::TEST_TARGET_CONNECTION);
usleep(20000);
$this
->assertNoConnection($id);
}
public function testConnectionOpen() {
$reflection = new \ReflectionObject($this->connection);
$connection_property = $reflection
->getProperty('connection');
$connection_property
->setAccessible(TRUE);
if (get_class($connection_property
->getValue($this->connection)) !== 'PDO') {
$this
->markTestSkipped('Ignored PDO connection unit test for this driver because it does not implement PDO.');
}
$error_mode = $connection_property
->getValue($this->connection)
->getAttribute(\PDO::ATTR_ERRMODE);
$this
->assertSame(\PDO::ERRMODE_EXCEPTION, $error_mode);
$connection_info = Database::getConnectionInfo();
$connection_info['default']['pdo'][\PDO::ATTR_ERRMODE] = \PDO::ERRMODE_SILENT;
Database::addConnectionInfo('test', 'default', $connection_info['default']);
$test_connection = Database::getConnection('default', 'test');
$reflection = new \ReflectionObject($test_connection);
$connection_property = $reflection
->getProperty('connection');
$connection_property
->setAccessible(TRUE);
$error_mode = $connection_property
->getValue($test_connection)
->getAttribute(\PDO::ATTR_ERRMODE);
$this
->assertSame(\PDO::ERRMODE_SILENT, $error_mode);
Database::removeConnection('test');
}
}