diff --git a/core/modules/system/lib/Drupal/system/Tests/Database/ConnectionUnitTest.php b/core/modules/system/lib/Drupal/system/Tests/Database/ConnectionUnitTest.php index cd72f26..522a60f 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Database/ConnectionUnitTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Database/ConnectionUnitTest.php @@ -17,6 +17,8 @@ class ConnectionUnitTest extends UnitTestBase { protected $key; protected $target; + + protected $monitor; protected $originalCount; public static function getInfo() { @@ -34,18 +36,19 @@ function setUp() { $this->originalTarget = 'default'; $this->target = 'DatabaseConnectionUnitTest'; - // Retrieve current/original amount of connections. - $this->originalCount = $this->countConnections(); - } - - function tearDown() { - parent::tearDown(); - } - - protected function countConnections() { - return count(Database::getConnection()->query('SHOW PROCESSLIST')->fetchCol()); + // Create an additional connection to monitor the connections being opened + // and closed in this test. + // @see TestBase::changeDatabasePrefix() + $connection_info = Database::getConnectionInfo('default'); + Database::addConnectionInfo('default', 'monitor', $connection_info['default']); + global $databases; + $databases['default']['monitor'] = $connection_info['default']; + $this->monitor = Database::getConnection('monitor'); } + /** + * Adds a new database connection info to Database. + */ protected function addConnection() { // Add a new target to the connection, by cloning the current connection. $connection_info = Database::getConnectionInfo($this->key); @@ -57,21 +60,50 @@ protected function addConnection() { $this->assertIdentical($info[$this->target], $connection_info[$this->key], 'New connection info found.'); } - function testNoop() { - // Verify that retrieving the amount again does not increase it. - $this->assertIdentical($this->countConnections(), $this->originalCount); + /** + * Returns the connection ID of the current test connection. + * + * @return integer + */ + protected function getConnectionID() { + return (int) Database::getConnection($this->target, $this->key)->query('SELECT CONNECTION_ID()')->fetchField(); + } + + /** + * Asserts that a connection ID exists. + * + * @param integer $id + * The connection ID to verify. + */ + 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))); + } + + /** + * Asserts that a connection ID does not exist. + * + * @param integer $id + * The connection ID to verify. + */ + 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))); } /** - * Tests Database::closeConnection(). + * Tests Database::closeConnection() without query. + * + * @todo getConnectionID() executes a query. */ function testOpenClose() { // Add and open a new connection. $this->addConnection(); + $id = $this->getConnectionID(); Database::getConnection($this->target, $this->key); // Verify that there is a new connection. - $this->assertIdentical($this->countConnections(), $this->originalCount + 1); + $this->assertConnection($id); // Close the connection. Database::closeConnection($this->target, $this->key); @@ -79,19 +111,20 @@ function testOpenClose() { usleep(20000); // Verify that we are back to the original connection count. - $this->assertIdentical($this->countConnections(), $this->originalCount); + $this->assertNoConnection($id); } /** - * Tests Database::closeConnection(). + * Tests Database::closeConnection() with a query. */ function testOpenQueryClose() { // Add and open a new connection. $this->addConnection(); + $id = $this->getConnectionID(); Database::getConnection($this->target, $this->key); // Verify that there is a new connection. - $this->assertIdentical($this->countConnections(), $this->originalCount + 1); + $this->assertConnection($id); // Execute a query. Database::getConnection($this->target, $this->key)->query('SHOW TABLES'); @@ -102,19 +135,20 @@ function testOpenQueryClose() { usleep(20000); // Verify that we are back to the original connection count. - $this->assertIdentical($this->countConnections(), $this->originalCount); + $this->assertNoConnection($id); } /** - * Tests Database::closeConnection(). + * Tests Database::closeConnection() with a query and custom prefetch method. */ function testOpenQueryPrefetchClose() { // Add and open a new connection. $this->addConnection(); + $id = $this->getConnectionID(); Database::getConnection($this->target, $this->key); // Verify that there is a new connection. - $this->assertIdentical($this->countConnections(), $this->originalCount + 1); + $this->assertConnection($id); // Execute a query. Database::getConnection($this->target, $this->key)->query('SHOW TABLES')->fetchCol(); @@ -125,19 +159,20 @@ function testOpenQueryPrefetchClose() { usleep(20000); // Verify that we are back to the original connection count. - $this->assertIdentical($this->countConnections(), $this->originalCount); + $this->assertNoConnection($id); } /** - * Tests Database::closeConnection(). + * Tests Database::closeConnection() with a select query. */ function testOpenSelectQueryClose() { // Add and open a new connection. $this->addConnection(); + $id = $this->getConnectionID(); Database::getConnection($this->target, $this->key); // Verify that there is a new connection. - $this->assertIdentical($this->countConnections(), $this->originalCount + 1); + $this->assertConnection($id); // Create a table. $name = 'foo'; @@ -165,7 +200,7 @@ function testOpenSelectQueryClose() { usleep(20000); // Verify that we are back to the original connection count. - $this->assertIdentical($this->countConnections(), $this->originalCount); + $this->assertNoConnection($id); } }