mirror of
https://github.com/phpbb/phpbb.git
synced 2025-01-18 22:58:10 +01:00
[ticket/9983] Add redis cache driver tests.
In order to not overwrite data in default redis store, at least one of redis host or post must be explicitly specified. Redis cache driver constructor has been modified to accept host and port as parameters. This was not added to public API as there are more parameters being passed via global constants. PHPBB3-9983
This commit is contained in:
parent
3a702084e4
commit
c852044d6e
27
phpBB/includes/cache/driver/redis.php
vendored
27
phpBB/includes/cache/driver/redis.php
vendored
@ -39,13 +39,38 @@ class phpbb_cache_driver_redis extends phpbb_cache_driver_memory
|
||||
|
||||
var $redis;
|
||||
|
||||
/**
|
||||
* Creates a redis cache driver.
|
||||
*
|
||||
* The following global constants affect operation:
|
||||
*
|
||||
* PHPBB_ACM_REDIS_HOST
|
||||
* PHPBB_ACM_REDIS_PORT
|
||||
* PHPBB_ACM_REDIS_PASSWORD
|
||||
* PHPBB_ACM_REDIS_DB
|
||||
*
|
||||
* There are no publicly documented constructor parameters.
|
||||
*/
|
||||
function __construct()
|
||||
{
|
||||
// Call the parent constructor
|
||||
parent::__construct();
|
||||
|
||||
$this->redis = new Redis();
|
||||
$this->redis->connect(PHPBB_ACM_REDIS_HOST, PHPBB_ACM_REDIS_PORT);
|
||||
|
||||
$args = func_get_args();
|
||||
if (!empty($args))
|
||||
{
|
||||
$ok = call_user_func_array(array($this->redis, 'connect'), $args);
|
||||
}
|
||||
else
|
||||
{
|
||||
$ok = $this->redis->connect(PHPBB_ACM_REDIS_HOST, PHPBB_ACM_REDIS_PORT);
|
||||
}
|
||||
if (!$ok)
|
||||
{
|
||||
trigger_error('Could not connect to redis server');
|
||||
}
|
||||
|
||||
if (defined('PHPBB_ACM_REDIS_PASSWORD'))
|
||||
{
|
||||
|
@ -72,6 +72,21 @@ to connect to that database in phpBB.
|
||||
Additionally, you will need to be running the DbUnit fork from
|
||||
https://github.com/phpbb/dbunit/tree/phpbb.
|
||||
|
||||
Redis
|
||||
-----
|
||||
|
||||
In order to run tests for the Redis cache driver, at least one of Redis host
|
||||
or port must be specified in test configuration. This can be done via
|
||||
test_config.php as follows:
|
||||
|
||||
<?
|
||||
$redis_host = 'localhost';
|
||||
$redis_port = 6379;
|
||||
|
||||
Or via environment variables as follows:
|
||||
|
||||
$ PHPBB_TEST_REDIS_HOST=localhost PHPBB_TEST_REDIS_PORT=6379 phpunit
|
||||
|
||||
Running
|
||||
=======
|
||||
|
||||
|
68
tests/cache/cache_test.php
vendored
68
tests/cache/cache_test.php
vendored
@ -77,7 +77,7 @@ class phpbb_cache_test extends phpbb_database_test_case
|
||||
);
|
||||
}
|
||||
|
||||
public function test_cache_sql()
|
||||
public function test_cache_sql_file()
|
||||
{
|
||||
$driver = new phpbb_cache_driver_file($this->cache_dir);
|
||||
|
||||
@ -87,12 +87,76 @@ class phpbb_cache_test extends phpbb_database_test_case
|
||||
|
||||
$sql = "SELECT * FROM phpbb_config
|
||||
WHERE config_name = 'foo'";
|
||||
|
||||
$cache_path = $this->cache_dir . 'sql_' . md5(preg_replace('/[\n\r\s\t]+/', ' ', $sql)) . '.php';
|
||||
$this->assertFileNotExists($cache_path);
|
||||
|
||||
$result = $db->sql_query($sql, 300);
|
||||
$first_result = $db->sql_fetchrow($result);
|
||||
$expected = array('config_name' => 'foo', 'config_value' => '23', 'is_dynamic' => 0);
|
||||
$this->assertEquals($expected, $first_result);
|
||||
|
||||
$this->assertFileExists($this->cache_dir . 'sql_' . md5(preg_replace('/[\n\r\s\t]+/', ' ', $sql)) . '.php');
|
||||
$this->assertFileExists($cache_path);
|
||||
|
||||
$sql = 'DELETE FROM phpbb_config';
|
||||
$result = $db->sql_query($sql);
|
||||
|
||||
$sql = "SELECT * FROM phpbb_config
|
||||
WHERE config_name = 'foo'";
|
||||
$result = $db->sql_query($sql, 300);
|
||||
|
||||
$this->assertEquals($expected, $db->sql_fetchrow($result));
|
||||
|
||||
$sql = "SELECT * FROM phpbb_config
|
||||
WHERE config_name = 'foo'";
|
||||
$result = $db->sql_query($sql);
|
||||
|
||||
$no_cache_result = $db->sql_fetchrow($result);
|
||||
$this->assertSame(false, $no_cache_result);
|
||||
|
||||
$db->sql_close();
|
||||
}
|
||||
|
||||
public function test_cache_sql_redis()
|
||||
{
|
||||
if (!extension_loaded('redis'))
|
||||
{
|
||||
$this->markTestSkipped('redis extension is not loaded');
|
||||
}
|
||||
|
||||
$config = phpbb_test_case_helpers::get_test_config();
|
||||
if (isset($config['redis_host']) || isset($config['redis_port']))
|
||||
{
|
||||
$host = isset($config['redis_host']) ? $config['redis_host'] : 'localhost';
|
||||
$port = isset($config['redis_port']) ? $config['redis_port'] : 6379;
|
||||
}
|
||||
else
|
||||
{
|
||||
$this->markTestSkipped('Test redis host/port is not specified');
|
||||
}
|
||||
$driver = new phpbb_cache_driver_redis($host, $port);
|
||||
$driver->purge();
|
||||
|
||||
global $db, $cache;
|
||||
$db = $this->new_dbal();
|
||||
$cache = new phpbb_cache_service($driver);
|
||||
|
||||
$redis = new Redis();
|
||||
$ok = $redis->connect($host, $port);
|
||||
$this->assertTrue($ok);
|
||||
|
||||
$sql = "SELECT * FROM phpbb_config
|
||||
WHERE config_name = 'foo'";
|
||||
|
||||
$key = $driver->key_prefix . 'sql_' . md5(preg_replace('/[\n\r\s\t]+/', ' ', $sql));
|
||||
$this->assertFalse($redis->exists($key));
|
||||
|
||||
$result = $db->sql_query($sql, 300);
|
||||
$first_result = $db->sql_fetchrow($result);
|
||||
$expected = array('config_name' => 'foo', 'config_value' => '23', 'is_dynamic' => 0);
|
||||
$this->assertEquals($expected, $first_result);
|
||||
|
||||
$this->assertTrue($redis->exists($key));
|
||||
|
||||
$sql = 'DELETE FROM phpbb_config';
|
||||
$result = $db->sql_query($sql);
|
||||
|
@ -91,6 +91,15 @@ class phpbb_test_case_helpers
|
||||
{
|
||||
$config['phpbb_functional_url'] = $phpbb_functional_url;
|
||||
}
|
||||
|
||||
if (isset($redis_host))
|
||||
{
|
||||
$config['redis_host'] = $redis_host;
|
||||
}
|
||||
if (isset($redis_port))
|
||||
{
|
||||
$config['redis_port'] = $redis_port;
|
||||
}
|
||||
}
|
||||
|
||||
if (isset($_SERVER['PHPBB_TEST_DBMS']))
|
||||
@ -113,6 +122,16 @@ class phpbb_test_case_helpers
|
||||
));
|
||||
}
|
||||
|
||||
if (isset($_SERVER['PHPBB_TEST_REDIS_HOST']))
|
||||
{
|
||||
$config['redis_host'] = $_SERVER['PHPBB_TEST_REDIS_HOST'];
|
||||
}
|
||||
|
||||
if (isset($_SERVER['PHPBB_TEST_REDIS_PORT']))
|
||||
{
|
||||
$config['redis_port'] = $_SERVER['PHPBB_TEST_REDIS_PORT'];
|
||||
}
|
||||
|
||||
return $config;
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user