1
1
Fork 0

Introduce test optimization

- Add static connection for whole tests
- Introduce ExtendedPDO class to enable nested transactions
- Add rollback logic for tests to ensure reliability and increase speed
This commit is contained in:
Philipp Holzer 2019-07-27 14:37:24 +02:00
commit 37d03bbeae
No known key found for this signature in database
GPG key ID: D8365C3D36B77D90
5 changed files with 246 additions and 50 deletions

View file

@ -0,0 +1,97 @@
<?php
namespace Friendica\Test\Util\Database;
use PDO;
use PDOException;
/**
* This class extends native PDO one but allow nested transactions
* by using the SQL statements `SAVEPOINT', 'RELEASE SAVEPOINT' AND 'ROLLBACK SAVEPOINT'
*/
class ExtendedPDO extends PDO
{
/**
* @var array Database drivers that support SAVEPOINT * statements.
*/
protected static $_supportedDrivers = array("pgsql", "mysql");
/**
* @var int the current transaction depth
*/
protected $_transactionDepth = 0;
/**
* @return int
*/
public function getTransactionDepth()
{
return $this->_transactionDepth;
}
/**
* Test if database driver support savepoints
*
* @return bool
*/
protected function hasSavepoint()
{
return in_array($this->getAttribute(PDO::ATTR_DRIVER_NAME),
self::$_supportedDrivers);
}
/**
* Start transaction
*
* @return bool|void
*/
public function beginTransaction()
{
if($this->_transactionDepth == 0 || !$this->hasSavepoint()) {
parent::beginTransaction();
} else {
$this->exec("SAVEPOINT LEVEL{$this->_transactionDepth}");
}
$this->_transactionDepth++;
}
/**
* Commit current transaction
*
* @return bool|void
*/
public function commit()
{
$this->_transactionDepth--;
if($this->_transactionDepth == 0 || !$this->hasSavepoint()) {
parent::commit();
} else {
$this->exec("RELEASE SAVEPOINT LEVEL{$this->_transactionDepth}");
}
}
/**
* Rollback current transaction,
*
* @throws PDOException if there is no transaction started
* @return bool|void
*/
public function rollBack()
{
if ($this->_transactionDepth == 0) {
throw new PDOException('Rollback error : There is no transaction started');
}
$this->_transactionDepth--;
if($this->_transactionDepth == 0 || !$this->hasSavepoint()) {
parent::rollBack();
} else {
$this->exec("ROLLBACK TO SAVEPOINT LEVEL{$this->_transactionDepth}");
}
}
}

View file

@ -0,0 +1,126 @@
<?php
namespace Friendica\Test\Util\Database;
use Friendica\Database\Database;
use PDO;
use PDOException;
class StaticDatabase extends Database
{
/**
* @var ExtendedPDO
*/
private static $staticConnection;
/**
* Override the behaviour of connect, due there is just one, static connection at all
*
* @return bool|void
*/
public function connect()
{
if (!is_null($this->connection) && $this->connected()) {
return true;
}
if (!isset(self::$staticConnection)) {
$port = 0;
$serveraddr = trim($this->configCache->get('database', 'hostname'));
$serverdata = explode(':', $serveraddr);
$server = $serverdata[0];
if (count($serverdata) > 1) {
$port = trim($serverdata[1]);
}
$server = trim($server);
$user = trim($this->configCache->get('database', 'username'));
$pass = trim($this->configCache->get('database', 'password'));
$db = trim($this->configCache->get('database', 'database'));
$charset = trim($this->configCache->get('database', 'charset'));
if (!(strlen($server) && strlen($user))) {
return false;
}
$connect = "mysql:host=" . $server . ";dbname=" . $db;
if ($port > 0) {
$connect .= ";port=" . $port;
}
if ($charset) {
$connect .= ";charset=" . $charset;
}
try {
self::$staticConnection = @new ExtendedPDO($connect, $user, $pass);
self::$staticConnection->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
} catch (PDOException $e) {
/// @TODO At least log exception, don't ignore it!
}
}
$this->driver = 'pdo';
$this->connection = self::$staticConnection;
$this->connected = true;
return $this->connected;
}
/**
* Override the transaction since there are now hierachical transactions possible
*
* @return bool
*/
public function transaction()
{
if (!$this->connection->inTransaction() && !$this->connection->beginTransaction()) {
return false;
}
$this->in_transaction = true;
return true;
}
/**
* @brief Does a commit
*
* @return boolean Was the command executed successfully?
*/
public function commit()
{
if (!$this->performCommit()) {
return false;
}
$this->in_transaction = false;
return true;
}
/**
* @return ExtendedPDO
*/
public static function getGlobConnection()
{
return self::$staticConnection;
}
public static function statCommit()
{
if (isset(self::$staticConnection)) {
while (self::$staticConnection->getTransactionDepth() > 0) {
self::$staticConnection->commit();
}
}
}
public static function statRollback()
{
if (isset(self::$staticConnection)) {
while (self::$staticConnection->getTransactionDepth() > 0) {
self::$staticConnection->rollBack();
}
}
}
}