View source
<?php
namespace Drupal\system\Tests\Database;
use Drupal\Core\Database\Database;
use Drupal\simpletest\KernelTestBase;
class ConnectionUnitTest extends KernelTestBase {
protected $key;
protected $target;
protected $monitor;
protected $originalCount;
protected function setUp() {
parent::setUp();
$this->key = 'default';
$this->originalTarget = 'default';
$this->target = 'DatabaseConnectionUnitTest';
$connection_info = Database::getConnectionInfo('default');
$this->skipTest = (bool) ($connection_info['default']['driver'] != 'mysql');
if ($this->skipTest) {
$this
->pass('This test is only compatible with MySQL.');
}
Database::addConnectionInfo('default', 'monitor', $connection_info['default']);
$this->monitor = Database::getConnection('monitor');
}
protected function addConnection() {
$connection_info = Database::getConnectionInfo($this->key);
Database::addConnectionInfo($this->key, $this->target, $connection_info[$this->originalTarget]);
$info = Database::getConnectionInfo($this->key);
$this
->assertIdentical($info[$this->target], $connection_info[$this->key], 'New connection info found.');
}
protected function getConnectionID() {
return (int) Database::getConnection($this->target, $this->key)
->query('SELECT CONNECTION_ID()')
->fetchField();
}
protected function assertConnection($id) {
$list = $this->monitor
->query('SHOW PROCESSLIST')
->fetchAllKeyed(0, 0);
return $this
->assertTrue(isset($list[$id]), format_string('Connection ID @id found.', array(
'@id' => $id,
)));
}
protected function assertNoConnection($id) {
$list = $this->monitor
->query('SHOW PROCESSLIST')
->fetchAllKeyed(0, 0);
return $this
->assertFalse(isset($list[$id]), format_string('Connection ID @id not found.', array(
'@id' => $id,
)));
}
function testOpenClose() {
if ($this->skipTest) {
return;
}
$this
->addConnection();
$id = $this
->getConnectionID();
Database::getConnection($this->target, $this->key);
$this
->assertConnection($id);
Database::closeConnection($this->target, $this->key);
usleep(20000);
$this
->assertNoConnection($id);
}
function testOpenQueryClose() {
if ($this->skipTest) {
return;
}
$this
->addConnection();
$id = $this
->getConnectionID();
Database::getConnection($this->target, $this->key);
$this
->assertConnection($id);
Database::getConnection($this->target, $this->key)
->query('SHOW TABLES');
Database::closeConnection($this->target, $this->key);
usleep(20000);
$this
->assertNoConnection($id);
}
function testOpenQueryPrefetchClose() {
if ($this->skipTest) {
return;
}
$this
->addConnection();
$id = $this
->getConnectionID();
Database::getConnection($this->target, $this->key);
$this
->assertConnection($id);
Database::getConnection($this->target, $this->key)
->query('SHOW TABLES')
->fetchCol();
Database::closeConnection($this->target, $this->key);
usleep(20000);
$this
->assertNoConnection($id);
}
function testOpenSelectQueryClose() {
if ($this->skipTest) {
return;
}
$this
->addConnection();
$id = $this
->getConnectionID();
Database::getConnection($this->target, $this->key);
$this
->assertConnection($id);
$name = 'foo';
Database::getConnection($this->target, $this->key)
->schema()
->createTable($name, array(
'fields' => array(
'name' => array(
'type' => 'varchar',
'length' => 255,
),
),
));
Database::getConnection($this->target, $this->key)
->select('foo', 'f')
->fields('f', array(
'name',
))
->execute()
->fetchAll();
Database::getConnection($this->target, $this->key)
->schema()
->dropTable($name);
Database::closeConnection($this->target, $this->key);
usleep(20000);
$this
->assertNoConnection($id);
}
public function testConnectionOpen() {
$connection = Database::getConnection('default');
$reflection = new \ReflectionObject($connection);
$connection_property = $reflection
->getProperty('connection');
$connection_property
->setAccessible(TRUE);
$error_mode = $connection_property
->getValue($connection)
->getAttribute(\PDO::ATTR_ERRMODE);
$this
->assertEqual($error_mode, \PDO::ERRMODE_EXCEPTION, 'Ensure the default error mode is set to exception.');
$connection = Database::getConnectionInfo('default');
$connection['default']['pdo'][\PDO::ATTR_ERRMODE] = \PDO::ERRMODE_SILENT;
Database::addConnectionInfo('test', 'default', $connection['default']);
$connection = Database::getConnection('default', 'test');
$reflection = new \ReflectionObject($connection);
$connection_property = $reflection
->getProperty('connection');
$connection_property
->setAccessible(TRUE);
$error_mode = $connection_property
->getValue($connection)
->getAttribute(\PDO::ATTR_ERRMODE);
$this
->assertEqual($error_mode, \PDO::ERRMODE_SILENT, 'Ensure PDO connection options can be overridden.');
Database::removeConnection('test');
}
}