Issue #3065166 by mondrake: Modernize Drupal\KernelTests\Core\Database\ConnectionUnitTest
parent
12dcba2532
commit
98daa59973
|
@ -13,34 +13,36 @@ use Drupal\KernelTests\KernelTestBase;
|
||||||
*/
|
*/
|
||||||
class ConnectionUnitTest extends KernelTestBase {
|
class ConnectionUnitTest extends KernelTestBase {
|
||||||
|
|
||||||
protected $key;
|
/**
|
||||||
protected $target;
|
* A target connection identifier to be used for testing.
|
||||||
|
*/
|
||||||
|
const TEST_TARGET_CONNECTION = 'DatabaseConnectionUnitTest';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The default database connection used for testing.
|
||||||
|
*
|
||||||
|
* @var \Drupal\Core\Database\Connection
|
||||||
|
*/
|
||||||
|
protected $connection;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A database connection used for monitoring processes.
|
||||||
|
*
|
||||||
|
* @var \Drupal\Core\Database\Connection
|
||||||
|
*/
|
||||||
protected $monitor;
|
protected $monitor;
|
||||||
protected $originalCount;
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
protected function setUp() {
|
protected function setUp() {
|
||||||
parent::setUp();
|
parent::setUp();
|
||||||
|
|
||||||
$this->key = 'default';
|
$this->connection = Database::getConnection();
|
||||||
$this->originalTarget = 'default';
|
|
||||||
$this->target = 'DatabaseConnectionUnitTest';
|
|
||||||
|
|
||||||
// Determine whether the database driver is MySQL. If it is not, the test
|
|
||||||
// methods will not be executed.
|
|
||||||
// @todo Make this test driver-agnostic, or find a proper way to skip it.
|
|
||||||
// See https://www.drupal.org/node/1273478.
|
|
||||||
$connection_info = Database::getConnectionInfo('default');
|
|
||||||
$this->skipTest = (bool) ($connection_info['default']['driver'] != 'mysql');
|
|
||||||
if ($this->skipTest) {
|
|
||||||
// Insert an assertion to prevent Simpletest from interpreting the test
|
|
||||||
// as failure.
|
|
||||||
$this->pass('This test is only compatible with MySQL.');
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create an additional connection to monitor the connections being opened
|
// Create an additional connection to monitor the connections being opened
|
||||||
// and closed in this test.
|
// and closed in this test.
|
||||||
// @see TestBase::changeDatabasePrefix()
|
$connection_info = Database::getConnectionInfo();
|
||||||
Database::addConnectionInfo('default', 'monitor', $connection_info['default']);
|
Database::addConnectionInfo('default', 'monitor', $connection_info['default']);
|
||||||
$this->monitor = Database::getConnection('monitor');
|
$this->monitor = Database::getConnection('monitor');
|
||||||
}
|
}
|
||||||
|
@ -50,13 +52,13 @@ class ConnectionUnitTest extends KernelTestBase {
|
||||||
*/
|
*/
|
||||||
protected function addConnection() {
|
protected function addConnection() {
|
||||||
// Add a new target to the connection, by cloning the current connection.
|
// Add a new target to the connection, by cloning the current connection.
|
||||||
$connection_info = Database::getConnectionInfo($this->key);
|
$connection_info = Database::getConnectionInfo();
|
||||||
Database::addConnectionInfo($this->key, $this->target, $connection_info[$this->originalTarget]);
|
Database::addConnectionInfo('default', static::TEST_TARGET_CONNECTION, $connection_info['default']);
|
||||||
|
|
||||||
// Verify that the new target exists.
|
// Verify that the new target exists.
|
||||||
$info = Database::getConnectionInfo($this->key);
|
$info = Database::getConnectionInfo();
|
||||||
// Note: Custom assertion message to not expose database credentials.
|
// New connection info found.
|
||||||
$this->assertIdentical($info[$this->target], $connection_info[$this->key], 'New connection info found.');
|
$this->assertSame($connection_info['default'], $info[static::TEST_TARGET_CONNECTION]);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -65,7 +67,7 @@ class ConnectionUnitTest extends KernelTestBase {
|
||||||
* @return int
|
* @return int
|
||||||
*/
|
*/
|
||||||
protected function getConnectionId() {
|
protected function getConnectionId() {
|
||||||
return (int) Database::getConnection($this->target, $this->key)->query('SELECT CONNECTION_ID()')->fetchField();
|
return (int) Database::getConnection(static::TEST_TARGET_CONNECTION)->query('SELECT CONNECTION_ID()')->fetchField();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -96,19 +98,22 @@ class ConnectionUnitTest extends KernelTestBase {
|
||||||
* @todo getConnectionId() executes a query.
|
* @todo getConnectionId() executes a query.
|
||||||
*/
|
*/
|
||||||
public function testOpenClose() {
|
public function testOpenClose() {
|
||||||
if ($this->skipTest) {
|
// Only run this test for the 'mysql' driver.
|
||||||
return;
|
$driver = $this->connection->driver();
|
||||||
|
if ($driver !== 'mysql') {
|
||||||
|
$this->markTestSkipped("MySql tests can not run for driver '$driver'.");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add and open a new connection.
|
// Add and open a new connection.
|
||||||
$this->addConnection();
|
$this->addConnection();
|
||||||
$id = $this->getConnectionId();
|
$id = $this->getConnectionId();
|
||||||
Database::getConnection($this->target, $this->key);
|
Database::getConnection(static::TEST_TARGET_CONNECTION);
|
||||||
|
|
||||||
// Verify that there is a new connection.
|
// Verify that there is a new connection.
|
||||||
$this->assertConnection($id);
|
$this->assertConnection($id);
|
||||||
|
|
||||||
// Close the connection.
|
// Close the connection.
|
||||||
Database::closeConnection($this->target, $this->key);
|
Database::closeConnection(static::TEST_TARGET_CONNECTION);
|
||||||
// Wait 20ms to give the database engine sufficient time to react.
|
// Wait 20ms to give the database engine sufficient time to react.
|
||||||
usleep(20000);
|
usleep(20000);
|
||||||
|
|
||||||
|
@ -120,22 +125,25 @@ class ConnectionUnitTest extends KernelTestBase {
|
||||||
* Tests Database::closeConnection() with a query.
|
* Tests Database::closeConnection() with a query.
|
||||||
*/
|
*/
|
||||||
public function testOpenQueryClose() {
|
public function testOpenQueryClose() {
|
||||||
if ($this->skipTest) {
|
// Only run this test for the 'mysql' driver.
|
||||||
return;
|
$driver = $this->connection->driver();
|
||||||
|
if ($driver !== 'mysql') {
|
||||||
|
$this->markTestSkipped("MySql tests can not run for driver '$driver'.");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add and open a new connection.
|
// Add and open a new connection.
|
||||||
$this->addConnection();
|
$this->addConnection();
|
||||||
$id = $this->getConnectionId();
|
$id = $this->getConnectionId();
|
||||||
Database::getConnection($this->target, $this->key);
|
Database::getConnection(static::TEST_TARGET_CONNECTION);
|
||||||
|
|
||||||
// Verify that there is a new connection.
|
// Verify that there is a new connection.
|
||||||
$this->assertConnection($id);
|
$this->assertConnection($id);
|
||||||
|
|
||||||
// Execute a query.
|
// Execute a query.
|
||||||
Database::getConnection($this->target, $this->key)->query('SHOW TABLES');
|
Database::getConnection(static::TEST_TARGET_CONNECTION)->query('SHOW TABLES');
|
||||||
|
|
||||||
// Close the connection.
|
// Close the connection.
|
||||||
Database::closeConnection($this->target, $this->key);
|
Database::closeConnection(static::TEST_TARGET_CONNECTION);
|
||||||
// Wait 20ms to give the database engine sufficient time to react.
|
// Wait 20ms to give the database engine sufficient time to react.
|
||||||
usleep(20000);
|
usleep(20000);
|
||||||
|
|
||||||
|
@ -147,22 +155,25 @@ class ConnectionUnitTest extends KernelTestBase {
|
||||||
* Tests Database::closeConnection() with a query and custom prefetch method.
|
* Tests Database::closeConnection() with a query and custom prefetch method.
|
||||||
*/
|
*/
|
||||||
public function testOpenQueryPrefetchClose() {
|
public function testOpenQueryPrefetchClose() {
|
||||||
if ($this->skipTest) {
|
// Only run this test for the 'mysql' driver.
|
||||||
return;
|
$driver = $this->connection->driver();
|
||||||
|
if ($driver !== 'mysql') {
|
||||||
|
$this->markTestSkipped("MySql tests can not run for driver '$driver'.");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add and open a new connection.
|
// Add and open a new connection.
|
||||||
$this->addConnection();
|
$this->addConnection();
|
||||||
$id = $this->getConnectionId();
|
$id = $this->getConnectionId();
|
||||||
Database::getConnection($this->target, $this->key);
|
Database::getConnection(static::TEST_TARGET_CONNECTION);
|
||||||
|
|
||||||
// Verify that there is a new connection.
|
// Verify that there is a new connection.
|
||||||
$this->assertConnection($id);
|
$this->assertConnection($id);
|
||||||
|
|
||||||
// Execute a query.
|
// Execute a query.
|
||||||
Database::getConnection($this->target, $this->key)->query('SHOW TABLES')->fetchCol();
|
Database::getConnection(static::TEST_TARGET_CONNECTION)->query('SHOW TABLES')->fetchCol();
|
||||||
|
|
||||||
// Close the connection.
|
// Close the connection.
|
||||||
Database::closeConnection($this->target, $this->key);
|
Database::closeConnection(static::TEST_TARGET_CONNECTION);
|
||||||
// Wait 20ms to give the database engine sufficient time to react.
|
// Wait 20ms to give the database engine sufficient time to react.
|
||||||
usleep(20000);
|
usleep(20000);
|
||||||
|
|
||||||
|
@ -174,20 +185,23 @@ class ConnectionUnitTest extends KernelTestBase {
|
||||||
* Tests Database::closeConnection() with a select query.
|
* Tests Database::closeConnection() with a select query.
|
||||||
*/
|
*/
|
||||||
public function testOpenSelectQueryClose() {
|
public function testOpenSelectQueryClose() {
|
||||||
if ($this->skipTest) {
|
// Only run this test for the 'mysql' driver.
|
||||||
return;
|
$driver = $this->connection->driver();
|
||||||
|
if ($driver !== 'mysql') {
|
||||||
|
$this->markTestSkipped("MySql tests can not run for driver '$driver'.");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add and open a new connection.
|
// Add and open a new connection.
|
||||||
$this->addConnection();
|
$this->addConnection();
|
||||||
$id = $this->getConnectionId();
|
$id = $this->getConnectionId();
|
||||||
Database::getConnection($this->target, $this->key);
|
Database::getConnection(static::TEST_TARGET_CONNECTION);
|
||||||
|
|
||||||
// Verify that there is a new connection.
|
// Verify that there is a new connection.
|
||||||
$this->assertConnection($id);
|
$this->assertConnection($id);
|
||||||
|
|
||||||
// Create a table.
|
// Create a table.
|
||||||
$name = 'foo';
|
$name = 'foo';
|
||||||
Database::getConnection($this->target, $this->key)->schema()->createTable($name, [
|
Database::getConnection(static::TEST_TARGET_CONNECTION)->schema()->createTable($name, [
|
||||||
'fields' => [
|
'fields' => [
|
||||||
'name' => [
|
'name' => [
|
||||||
'type' => 'varchar',
|
'type' => 'varchar',
|
||||||
|
@ -197,16 +211,16 @@ class ConnectionUnitTest extends KernelTestBase {
|
||||||
]);
|
]);
|
||||||
|
|
||||||
// Execute a query.
|
// Execute a query.
|
||||||
Database::getConnection($this->target, $this->key)->select('foo', 'f')
|
Database::getConnection(static::TEST_TARGET_CONNECTION)->select('foo', 'f')
|
||||||
->fields('f', ['name'])
|
->fields('f', ['name'])
|
||||||
->execute()
|
->execute()
|
||||||
->fetchAll();
|
->fetchAll();
|
||||||
|
|
||||||
// Drop the table.
|
// Drop the table.
|
||||||
Database::getConnection($this->target, $this->key)->schema()->dropTable($name);
|
Database::getConnection(static::TEST_TARGET_CONNECTION)->schema()->dropTable($name);
|
||||||
|
|
||||||
// Close the connection.
|
// Close the connection.
|
||||||
Database::closeConnection($this->target, $this->key);
|
Database::closeConnection(static::TEST_TARGET_CONNECTION);
|
||||||
// Wait 20ms to give the database engine sufficient time to react.
|
// Wait 20ms to give the database engine sufficient time to react.
|
||||||
usleep(20000);
|
usleep(20000);
|
||||||
|
|
||||||
|
@ -218,31 +232,32 @@ class ConnectionUnitTest extends KernelTestBase {
|
||||||
* Tests pdo options override.
|
* Tests pdo options override.
|
||||||
*/
|
*/
|
||||||
public function testConnectionOpen() {
|
public function testConnectionOpen() {
|
||||||
$connection = Database::getConnection('default');
|
$reflection = new \ReflectionObject($this->connection);
|
||||||
$reflection = new \ReflectionObject($connection);
|
|
||||||
$connection_property = $reflection->getProperty('connection');
|
$connection_property = $reflection->getProperty('connection');
|
||||||
$connection_property->setAccessible(TRUE);
|
$connection_property->setAccessible(TRUE);
|
||||||
// Skip this test when a database driver does not implement PDO.
|
// Skip this test when a database driver does not implement PDO.
|
||||||
// An alternative database driver that does not implement PDO
|
// An alternative database driver that does not implement PDO
|
||||||
// should implement its own connection test.
|
// should implement its own connection test.
|
||||||
if (get_class($connection_property->getValue($connection)) !== 'PDO') {
|
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.');
|
$this->markTestSkipped('Ignored PDO connection unit test for this driver because it does not implement PDO.');
|
||||||
}
|
}
|
||||||
$error_mode = $connection_property->getValue($connection)
|
$error_mode = $connection_property->getValue($this->connection)
|
||||||
->getAttribute(\PDO::ATTR_ERRMODE);
|
->getAttribute(\PDO::ATTR_ERRMODE);
|
||||||
$this->assertEqual($error_mode, \PDO::ERRMODE_EXCEPTION, 'Ensure the default error mode is set to exception.');
|
// Ensure the default error mode is set to exception.
|
||||||
|
$this->assertSame(\PDO::ERRMODE_EXCEPTION, $error_mode);
|
||||||
|
|
||||||
$connection = Database::getConnectionInfo('default');
|
$connection_info = Database::getConnectionInfo();
|
||||||
$connection['default']['pdo'][\PDO::ATTR_ERRMODE] = \PDO::ERRMODE_SILENT;
|
$connection_info['default']['pdo'][\PDO::ATTR_ERRMODE] = \PDO::ERRMODE_SILENT;
|
||||||
Database::addConnectionInfo('test', 'default', $connection['default']);
|
Database::addConnectionInfo('test', 'default', $connection_info['default']);
|
||||||
$connection = Database::getConnection('default', 'test');
|
$test_connection = Database::getConnection('default', 'test');
|
||||||
|
|
||||||
$reflection = new \ReflectionObject($connection);
|
$reflection = new \ReflectionObject($test_connection);
|
||||||
$connection_property = $reflection->getProperty('connection');
|
$connection_property = $reflection->getProperty('connection');
|
||||||
$connection_property->setAccessible(TRUE);
|
$connection_property->setAccessible(TRUE);
|
||||||
$error_mode = $connection_property->getValue($connection)
|
$error_mode = $connection_property->getValue($test_connection)
|
||||||
->getAttribute(\PDO::ATTR_ERRMODE);
|
->getAttribute(\PDO::ATTR_ERRMODE);
|
||||||
$this->assertEqual($error_mode, \PDO::ERRMODE_SILENT, 'Ensure PDO connection options can be overridden.');
|
// Ensure PDO connection options can be overridden.
|
||||||
|
$this->assertSame(\PDO::ERRMODE_SILENT, $error_mode);
|
||||||
|
|
||||||
Database::removeConnection('test');
|
Database::removeConnection('test');
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue