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() : void {
parent::setUp();
$this->connection = Database::getConnection();
$connection_info = Database::getConnectionInfo();
Database::addConnectionInfo('default', 'monitor', $connection_info['default']);
$this->monitor = Database::getConnection('monitor');
}
protected function getQuery() {
if ($this->connection
->databaseType() == 'pgsql') {
return [
'connection_id' => 'SELECT pg_backend_pid()',
'processlist' => 'SELECT pid FROM pg_stat_activity',
'show_tables' => 'SELECT * FROM pg_catalog.pg_tables',
];
}
else {
return [
'connection_id' => 'SELECT CONNECTION_ID()',
'processlist' => 'SHOW PROCESSLIST',
'show_tables' => 'SHOW TABLES',
];
}
}
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($this
->getQuery()['connection_id'])
->fetchField();
}
protected function assertConnection(int $id) : void {
$list = $this->monitor
->query($this
->getQuery()['processlist'])
->fetchAllKeyed(0, 0);
$this
->assertTrue(isset($list[$id]), new FormattableMarkup('Connection ID @id found.', [
'@id' => $id,
]));
}
protected function assertNoConnection(int $id) : void {
$list = $this->monitor
->query($this
->getQuery()['processlist'])
->fetchAllKeyed(0, 0);
$this
->assertFalse(isset($list[$id]), new FormattableMarkup('Connection ID @id not found.', [
'@id' => $id,
]));
}
public function testOpenClose() {
$database_type = $this->connection
->databaseType();
if ($database_type != 'mysql' && $database_type != 'pgsql') {
$this
->markTestSkipped("This tests only runs on MySQL and PostgreSQL");
}
$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() {
$database_type = $this->connection
->databaseType();
if ($database_type != 'mysql' && $database_type != 'pgsql') {
$this
->markTestSkipped("This tests only runs on MySQL and PostgreSQL");
}
$this
->addConnection();
$id = $this
->getConnectionId();
Database::getConnection(static::TEST_TARGET_CONNECTION);
$this
->assertConnection($id);
Database::getConnection(static::TEST_TARGET_CONNECTION)
->query($this
->getQuery()['show_tables']);
Database::closeConnection(static::TEST_TARGET_CONNECTION);
usleep(20000);
$this
->assertNoConnection($id);
}
public function testOpenQueryPrefetchClose() {
$database_type = $this->connection
->databaseType();
if ($database_type != 'mysql' && $database_type != 'pgsql') {
$this
->markTestSkipped("This tests only runs on MySQL and PostgreSQL");
}
$this
->addConnection();
$id = $this
->getConnectionId();
Database::getConnection(static::TEST_TARGET_CONNECTION);
$this
->assertConnection($id);
Database::getConnection(static::TEST_TARGET_CONNECTION)
->query($this
->getQuery()['show_tables'])
->fetchCol();
Database::closeConnection(static::TEST_TARGET_CONNECTION);
usleep(20000);
$this
->assertNoConnection($id);
}
public function testOpenSelectQueryClose() {
$database_type = $this->connection
->databaseType();
if ($database_type != 'mysql' && $database_type != 'pgsql') {
$this
->markTestSkipped("This tests only runs on MySQL and PostgreSQL");
}
$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');
}
}