diff --git a/phpBB/includes/cache/driver/redis.php b/phpBB/includes/cache/driver/redis.php index d256b5600e..960735b673 100644 --- a/phpBB/includes/cache/driver/redis.php +++ b/phpBB/includes/cache/driver/redis.php @@ -39,13 +39,39 @@ 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')) { diff --git a/tests/RUNNING_TESTS.txt b/tests/RUNNING_TESTS.txt index 7c2a7c3fce..75a6fc94f6 100644 --- a/tests/RUNNING_TESTS.txt +++ b/tests/RUNNING_TESTS.txt @@ -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: + + createXMLDataSet(dirname(__FILE__) . '/fixtures/config.xml'); + } + + static public function setUpBeforeClass() + { + if (!extension_loaded('apc')) + { + self::markTestSkipped('APC extension is not loaded'); + } + + $php_ini = new phpbb_php_ini; + + if (!$php_ini->get_bool('apc.enabled')) + { + self::markTestSkipped('APC is not enabled. Make sure apc.enabled=1 in php.ini'); + } + + if (PHP_SAPI == 'cli' && !$php_ini->get_bool('apc.enable_cli')) + { + self::markTestSkipped('APC is not enabled for CLI. Set apc.enable_cli=1 in php.ini'); + } + } + + protected function setUp() + { + parent::setUp(); + + $this->driver = new phpbb_cache_driver_apc; + $this->driver->purge(); + } + + public function test_cache_sql() + { + global $db, $cache; + $db = $this->new_dbal(); + $cache = new phpbb_cache_service($this->driver); + + $sql = "SELECT * FROM phpbb_config + WHERE config_name = 'foo'"; + + $key = $this->driver->key_prefix . 'sql_' . md5(preg_replace('/[\n\r\s\t]+/', ' ', $sql)); + $this->assertFalse(apc_fetch($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((bool) apc_fetch($key)); + + $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(); + } +} diff --git a/tests/cache/common_test_case.php b/tests/cache/common_test_case.php new file mode 100644 index 0000000000..45cf80e424 --- /dev/null +++ b/tests/cache/common_test_case.php @@ -0,0 +1,64 @@ +assertFalse($this->driver->_exists('test_key')); + $this->assertSame(false, $this->driver->get('test_key')); + + $this->driver->put('test_key', 'test_value'); + + $this->assertTrue($this->driver->_exists('test_key')); + $this->assertEquals( + 'test_value', + $this->driver->get('test_key'), + 'File ACM put and get' + ); + } + + public function test_purge() + { + $this->driver->put('test_key', 'test_value'); + + $this->assertEquals( + 'test_value', + $this->driver->get('test_key'), + 'File ACM put and get' + ); + + $this->driver->purge(); + + $this->assertSame(false, $this->driver->get('test_key')); + } + + public function test_destroy() + { + $this->driver->put('first_key', 'first_value'); + $this->driver->put('second_key', 'second_value'); + + $this->assertEquals( + 'first_value', + $this->driver->get('first_key') + ); + $this->assertEquals( + 'second_value', + $this->driver->get('second_key') + ); + + $this->driver->destroy('first_key'); + + $this->assertFalse($this->driver->_exists('first_key')); + $this->assertEquals( + 'second_value', + $this->driver->get('second_key') + ); + } +} diff --git a/tests/cache/cache_test.php b/tests/cache/file_driver_test.php similarity index 57% rename from tests/cache/cache_test.php rename to tests/cache/file_driver_test.php index 285af5cd0c..2353940277 100644 --- a/tests/cache/cache_test.php +++ b/tests/cache/file_driver_test.php @@ -7,11 +7,12 @@ * */ -require_once dirname(__FILE__) . '/../../phpBB/includes/functions.php'; +require_once dirname(__FILE__) . '/common_test_case.php'; -class phpbb_cache_test extends phpbb_database_test_case +class phpbb_cache_file_driver_test extends phpbb_cache_common_test_case { private $cache_dir; + protected $driver; public function __construct() { @@ -34,6 +35,8 @@ class phpbb_cache_test extends phpbb_database_test_case $this->remove_cache_dir(); } $this->create_cache_dir(); + + $this->driver = new phpbb_cache_driver_file($this->cache_dir); } protected function tearDown() @@ -64,35 +67,24 @@ class phpbb_cache_test extends phpbb_database_test_case rmdir($this->cache_dir); } - public function test_cache_driver_file() - { - $driver = new phpbb_cache_driver_file($this->cache_dir); - $driver->put('test_key', 'test_value'); - $driver->save(); - - $this->assertEquals( - 'test_value', - $driver->get('test_key'), - 'File ACM put and get' - ); - } - public function test_cache_sql() { - $driver = new phpbb_cache_driver_file($this->cache_dir); - global $db, $cache; $db = $this->new_dbal(); - $cache = new phpbb_cache_service($driver); + $cache = new phpbb_cache_service($this->driver); $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); @@ -112,33 +104,4 @@ class phpbb_cache_test extends phpbb_database_test_case $db->sql_close(); } - - public function test_null_cache_sql() - { - $driver = new phpbb_cache_driver_null($this->cache_dir); - - global $db, $cache; - $db = $this->new_dbal(); - $cache = new phpbb_cache_service($driver); - - $sql = "SELECT * FROM phpbb_config - WHERE config_name = 'foo'"; - $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); - - $sql = 'DELETE FROM phpbb_config'; - $result = $db->sql_query($sql); - - // As null cache driver does not actually cache, - // this should return no results - $sql = "SELECT * FROM phpbb_config - WHERE config_name = 'foo'"; - $result = $db->sql_query($sql, 300); - - $this->assertSame(false, $db->sql_fetchrow($result)); - - $db->sql_close(); - } } diff --git a/tests/cache/null_driver_test.php b/tests/cache/null_driver_test.php new file mode 100644 index 0000000000..86553d4dc5 --- /dev/null +++ b/tests/cache/null_driver_test.php @@ -0,0 +1,74 @@ +createXMLDataSet(dirname(__FILE__) . '/fixtures/config.xml'); + } + + protected function setUp() + { + parent::setUp(); + + $this->driver = new phpbb_cache_driver_null; + } + + public function test_get_put() + { + $this->assertSame(false, $this->driver->get('key')); + + $this->driver->put('key', 'value'); + + // null driver does not cache + $this->assertSame(false, $this->driver->get('key')); + } + + public function test_purge() + { + // does nothing + $this->driver->purge(); + } + + public function test_destroy() + { + // does nothing + $this->driver->destroy('foo'); + } + + public function test_cache_sql() + { + global $db, $cache; + $db = $this->new_dbal(); + $cache = new phpbb_cache_service($this->driver); + + $sql = "SELECT * FROM phpbb_config + WHERE config_name = 'foo'"; + $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); + + $sql = 'DELETE FROM phpbb_config'; + $result = $db->sql_query($sql); + + // As null cache driver does not actually cache, + // this should return no results + $sql = "SELECT * FROM phpbb_config + WHERE config_name = 'foo'"; + $result = $db->sql_query($sql, 300); + + $this->assertSame(false, $db->sql_fetchrow($result)); + + $db->sql_close(); + } +} diff --git a/tests/cache/redis_driver_test.php b/tests/cache/redis_driver_test.php new file mode 100644 index 0000000000..cd24e33baf --- /dev/null +++ b/tests/cache/redis_driver_test.php @@ -0,0 +1,91 @@ +createXMLDataSet(dirname(__FILE__) . '/fixtures/config.xml'); + } + + static public function setUpBeforeClass() + { + if (!extension_loaded('redis')) + { + self::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; + self::$config = array('host' => $host, 'port' => $port); + } + else + { + $this->markTestSkipped('Test redis host/port is not specified'); + } + } + + protected function setUp() + { + parent::setUp(); + + $this->driver = new phpbb_cache_driver_redis(self::$config['host'], self::$config['port']); + $this->driver->purge(); + } + + public function test_cache_sql() + { + global $db, $cache; + $db = $this->new_dbal(); + $cache = new phpbb_cache_service($this->driver); + + $redis = new Redis(); + $ok = $redis->connect(self::$config['host'], self::$config['port']); + $this->assertTrue($ok); + + $sql = "SELECT * FROM phpbb_config + WHERE config_name = 'foo'"; + + $key = $this->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); + + $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(); + } +} diff --git a/tests/test_framework/phpbb_test_case_helpers.php b/tests/test_framework/phpbb_test_case_helpers.php index d10645a732..fe15eded90 100644 --- a/tests/test_framework/phpbb_test_case_helpers.php +++ b/tests/test_framework/phpbb_test_case_helpers.php @@ -91,6 +91,15 @@ class phpbb_test_case_helpers { $config['phpbb_functional_url'] = $phpbb_functional_url; } + + if (isset($phpbb_redis_host)) + { + $config['redis_host'] = $phpbb_redis_host; + } + if (isset($phpbb_redis_port)) + { + $config['redis_port'] = $phpbb_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; }