AbstractUnitTestCase.php in Redis 7.2
File
lib/Redis/Tests/AbstractUnitTestCase.php
View source
<?php
abstract class Redis_Tests_AbstractUnitTestCase extends DrupalUnitTestCase {
protected static $loaderEnabled = false;
protected static function enableAutoload() {
if (self::$loaderEnabled) {
return;
}
if (class_exists('Redis_Client')) {
return;
}
spl_autoload_register(function ($className) {
$parts = explode('_', $className);
if ('Redis' === $parts[0]) {
$filename = __DIR__ . '/../lib/' . implode('/', $parts) . '.php';
return (bool) (include_once $filename);
}
return false;
}, null, true);
self::$loaderEnabled = true;
}
private $originalConf = array(
'cache_lifetime' => null,
'cache_prefix' => null,
'redis_client_interface' => null,
'redis_eval_enabled' => null,
'redis_flush_mode' => null,
'redis_perm_ttl' => null,
);
protected abstract function getClientInterface();
private final function prepareDrupalEnvironment() {
global $conf;
foreach (array_keys($this->originalConf) as $key) {
if (isset($conf[$key])) {
$this->originalConf[$key] = $conf[$key];
unset($conf[$key]);
}
}
$conf['cache_prefix'] = $this->testId;
}
private final function restoreDrupalEnvironment() {
$GLOBALS['conf'] = $this->originalConf + $GLOBALS['conf'];
}
private final function prepareClientManager() {
$interface = $this
->getClientInterface();
if (null === $interface) {
throw new \Exception("Test skipped due to missing driver");
}
$GLOBALS['conf']['redis_client_interface'] = $interface;
Redis_Client::reset();
}
private final function restoreClientManager() {
Redis_Client::reset();
}
public function setUp() {
self::enableAutoload();
$this
->prepareDrupalEnvironment();
$this
->prepareClientManager();
parent::setUp();
drupal_install_schema('system');
drupal_install_schema('locale');
}
public function tearDown() {
drupal_uninstall_schema('locale');
drupal_uninstall_schema('system');
$this
->restoreDrupalEnvironment();
$this
->restoreClientManager();
parent::tearDown();
}
}