1
0
mirror of https://github.com/phpbb/phpbb.git synced 2025-04-21 00:02:18 +02:00

Merge remote-tracking branch 'p/ticket/9983' into develop

* p/ticket/9983:
  [ticket/9983] Also check generic APC enable/disable.
  [ticket/9983] Use APC instead of apc in error messages.
  [ticket/9983] Skip tests if APC is not enabled for CLI.
  [ticket/9983] Test for apc cache driver.
  [ticket/9983] Add phpbb prefix to global variables.
  [ticket/9983] Empty line by request.
  [ticket/9983] Indeed, it is <?php.
  [ticket/9983] Add a test for destroy.
  [ticket/9983] Exercise exists also.
  [ticket/9983] Add a purge test.
  [ticket/9983] Rename test methods.
  [ticket/9983] get/put cache test moved to a base class.
  [ticket/9983] Create driver in setup in null driver test.
  [ticket/9983] Split cache test into per-driver files.
  [ticket/9983] Add redis cache driver tests.
This commit is contained in:
Andreas Fischer 2012-12-03 17:17:50 +01:00
commit 49f2925900
8 changed files with 392 additions and 49 deletions

View File

@ -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'))
{

View File

@ -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:
<?php
$phpbb_redis_host = 'localhost';
$phpbb_redis_port = 6379;
Or via environment variables as follows:
$ PHPBB_TEST_REDIS_HOST=localhost PHPBB_TEST_REDIS_PORT=6379 phpunit
Running
=======

91
tests/cache/apc_driver_test.php vendored Normal file
View File

@ -0,0 +1,91 @@
<?php
/**
*
* @package testing
* @copyright (c) 2012 phpBB Group
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
*
*/
// Important: apc.enable_cli=1 must be in php.ini.
// http://forums.devshed.com/php-development-5/apc-problem-561290.html
// http://php.net/manual/en/apc.configuration.php
require_once dirname(__FILE__) . '/common_test_case.php';
class phpbb_cache_apc_driver_test extends phpbb_cache_common_test_case
{
protected static $config;
protected $driver;
public function getDataSet()
{
return $this->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();
}
}

64
tests/cache/common_test_case.php vendored Normal file
View File

@ -0,0 +1,64 @@
<?php
/**
*
* @package testing
* @copyright (c) 2012 phpBB Group
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
*
*/
abstract class phpbb_cache_common_test_case extends phpbb_database_test_case
{
public function test_get_put_exists()
{
$this->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')
);
}
}

View File

@ -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();
}
}

74
tests/cache/null_driver_test.php vendored Normal file
View File

@ -0,0 +1,74 @@
<?php
/**
*
* @package testing
* @copyright (c) 2012 phpBB Group
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
*
*/
class phpbb_cache_null_driver_test extends phpbb_database_test_case
{
protected $driver;
public function getDataSet()
{
return $this->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();
}
}

91
tests/cache/redis_driver_test.php vendored Normal file
View File

@ -0,0 +1,91 @@
<?php
/**
*
* @package testing
* @copyright (c) 2012 phpBB Group
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
*
*/
require_once dirname(__FILE__) . '/common_test_case.php';
class phpbb_cache_redis_driver_test extends phpbb_cache_common_test_case
{
protected static $config;
protected $driver;
public function getDataSet()
{
return $this->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();
}
}

View File

@ -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;
}