Skip to content

Commit

Permalink
Merge pull request #7690 from xintesa/3.1-port-7428
Browse files Browse the repository at this point in the history
Forward port #7428
  • Loading branch information
lorenzo committed Nov 28, 2015
2 parents b5206cc + 2180fd0 commit 380a466
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/Database/Driver/PDODriverTrait.php
Expand Up @@ -76,6 +76,26 @@ public function disconnect()
$this->_connection = null;
}

/**
* Check whether or not the driver is connected.
*
* @return bool
*/
public function isConnected()
{
if ($this->_connection === null) {
$connected = false;
} else {
try {
$connected = $this->_connection->query('SELECT 1');
} catch (\PDOException $e) {
$connected = false;
}
}
$this->connected = !empty($connected);
return $this->connected;
}

/**
* Prepares a sql statement to be executed
*
Expand Down
14 changes: 14 additions & 0 deletions tests/TestCase/Database/Driver/MysqlTest.php
Expand Up @@ -128,4 +128,18 @@ public function testConnectionConfigCustom()
->will($this->returnValue($connection));
$driver->connect($config);
}

/**
* Test isConnected
*
* @return void
*/
public function testIsConnected() {
$connection = ConnectionManager::get('test');
$connection->disconnect();
$this->assertFalse($connection->isConnected(), 'Not connected now.');

$connection->connect();
$this->assertTrue($connection->isConnected(), 'Should be connected.');
}
}

0 comments on commit 380a466

Please sign in to comment.