1
0
mirror of https://github.com/phpbb/phpbb.git synced 2025-03-13 20:28:44 +01:00

[task/config-class] Implemented a config class to replace the global array.

There is a phpbb_config class which simply holds an array and does not persist
any data. It implements ArrayAccess, Countable and IteratorAggregate to allow
regular use of configuration as if it was still an array. The phpbb_config_db
class depends on an instance of the dbal and a cache driver. It obtains the
configuration data from cache and database as necessary and persists data to
the database.

The functions set_config and set_config_count remain for backward compatability
but they only call methods on the new config class now instead of directly
manipulating the database and cache.

PHPBB3-9988
This commit is contained in:
Nils Adermann 2011-01-10 02:27:18 +01:00
parent 5ea59ba322
commit fb2642bbc6
20 changed files with 703 additions and 154 deletions

View File

@ -227,7 +227,9 @@ $db->sql_connect($dbhost, $dbuser, $dbpasswd, $dbname, $dbport, false, defined('
unset($dbpasswd);
// Grab global variables, re-cache if necessary
$config = $cache->obtain_config();
$config = new phpbb_config_db($db, $cache_factory->get_driver());
set_config(null, null, null, $config);
set_config_count(null, null, null, $config);
// Add own hook handler
require($phpbb_root_path . 'includes/hooks/index.' . $phpEx);

View File

@ -70,7 +70,10 @@ if (isset($_GET['avatar']))
// worst-case default
$browser = (!empty($_SERVER['HTTP_USER_AGENT'])) ? htmlspecialchars((string) $_SERVER['HTTP_USER_AGENT']) : 'msie 6.0';
$config = $cache->obtain_config();
$config = new phpbb_config_db($db, $cache_factory->get_driver());
set_config(null, null, null, $config);
set_config_count(null, null, null, $config);
$filename = $_GET['avatar'];
$avatar_group = false;
$exit = false;

View File

@ -39,51 +39,6 @@ class phpbb_cache_service
return call_user_func_array(array($this->acm, $method), $arguments);
}
/**
* Get config values
*/
function obtain_config()
{
global $db;
if (($config = $this->acm->get('config')) !== false)
{
$sql = 'SELECT config_name, config_value
FROM ' . CONFIG_TABLE . '
WHERE is_dynamic = 1';
$result = $db->sql_query($sql);
while ($row = $db->sql_fetchrow($result))
{
$config[$row['config_name']] = $row['config_value'];
}
$db->sql_freeresult($result);
}
else
{
$config = $cached_config = array();
$sql = 'SELECT config_name, config_value, is_dynamic
FROM ' . CONFIG_TABLE;
$result = $db->sql_query($sql);
while ($row = $db->sql_fetchrow($result))
{
if (!$row['is_dynamic'])
{
$cached_config[$row['config_name']] = $row['config_value'];
}
$config[$row['config_name']] = $row['config_value'];
}
$db->sql_freeresult($result);
$this->acm->put('config', $cached_config);
}
return $config;
}
/**
* Obtain list of naughty words and build preg style replacement arrays for use by the
* calling script

View File

@ -152,9 +152,16 @@ class phpbb_captcha_gd extends phpbb_default_captcha
global $config;
$config_old = $config;
$config = new phpbb_config(array());
foreach ($old_config as $key => $value)
{
$config->set($key, $value);
}
foreach ($this->captcha_vars as $captcha_var => $template_var)
{
$config[$captcha_var] = request_var($captcha_var, (int) $config[$captcha_var]);
$config->set($captcha_var, request_var($captcha_var, (int) $config[$captcha_var]));
}
parent::execute_demo();
$config = $config_old;

View File

@ -0,0 +1,157 @@
<?php
/**
*
* @package phpBB3
* @copyright (c) 2010 phpBB Group
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/
/**
* @ignore
*/
if (!defined('IN_PHPBB'))
{
exit;
}
/**
* Configuration container class
* @package phpBB3
*/
class phpbb_config implements ArrayAccess, IteratorAggregate, Countable
{
/**
* The configuration data
* @var array(string => string)
*/
protected $config;
/**
* Creates a configuration container with a default set of values
*
* @param array(string => string) $config The configuration data.
*/
public function __construct(array $config)
{
$this->config = $config;
}
/**
* Retrieves an ArrayIterator over the configuration values.
*
* @return ArrayIterator An iterator over all config data
*/
public function getIterator()
{
return new ArrayIterator($this->config);
}
/**
* Checks if the specified config value exists.
*
* @param string $key The configuration option's name.
* @return bool Whether the configuration option exists.
*/
public function offsetExists($key)
{
return isset($this->config[$key]);
}
/**
* Retrieves a configuration value.
*
* @param string $key The configuration option's name.
* @return string The configuration value
*/
public function offsetGet($key)
{
return (isset($this->config[$key])) ? $this->config[$key] : '';
}
/**
* Temporarily overwrites the value of a configuration variable.
*
* The configuration change will not persist. It will be lost
* after the request.
*
* @param string $key The configuration option's name.
* @param string $value The temporary value.
*/
public function offsetSet($key, $value)
{
$this->config[$key] = $value;
}
/**
* Called when deleting a configuration value directly, triggers an error.
*
* @param string $key The configuration option's name.
*/
public function offsetUnset($key)
{
trigger_error('Config values have to be deleted explicitly with the phpbb_config::delete($key) method.', E_USER_ERROR);
}
/**
* Retrieves the number of configuration options currently set.
*
* @return int Number of config options
*/
public function count()
{
return count($this->config);
}
/**
* Sets a configuration option's value
*
* @param string $key The configuration option's name
* @param string $value New configuration value
* @param bool $cache Whether this variable should be cached or if it
* changes too frequently to be efficiently cached.
*/
public function set($key, $value, $cache = true)
{
$this->config[$key] = $value;
}
/**
* Sets a configuration option's value only if the old_value matches the
* current configuration value or the configuration value does not exist yet.
*
* @param string $key The configuration option's name
* @param string $old_value Current configuration value
* @param string $value New configuration value
* @param bool $cache Whether this variable should be cached or if it
* changes too frequently to be efficiently cached.
* @return bool True if the value was changed, false otherwise.
*/
public function set_atomic($key, $old_value, $value, $cache = true)
{
if (!isset($this->config[$key]) || $this->config[$key] == $old_value)
{
$this->config[$key] = $value;
return true;
}
return false;
}
/**
* Increments an integer configuration value.
*
* @param string $key The configuration option's name
* @param int $increment Amount to increment by
* @param bool $cache Whether this variable should be cached or if it
* changes too frequently to be efficiently cached.
*/
function increment($key, $increment, $cache = true)
{
if (!isset($this->config[$key]))
{
$this->config[$key] = 0;
}
$this->config[$key] += $increment;
}
}

View File

@ -0,0 +1,175 @@
<?php
/**
*
* @package phpBB3
* @copyright (c) 2010 phpBB Group
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/
/**
* @ignore
*/
if (!defined('IN_PHPBB'))
{
exit;
}
/**
* Configuration container class
* @package phpBB3
*/
class phpbb_config_db extends phpbb_config
{
/**
* Cache instance
* @var phpbb_cache_driver_interface
*/
protected $cache;
/**
* Database connection
* @var dbal
*/
protected $db;
/**
* Creates a configuration container with a default set of values
*
* @param phpbb_cache_driver_interface $cache Cache instance
* @param dbal $db Database connection
* @param string $table Configuration table name
*/
public function __construct(phpbb_cache_driver_interface $cache, dbal $db, $table)
{
$this->db = $db;
$this->cache = $cache;
$this->table = $table;
if (($config = $cache->get('config')) !== false)
{
$sql = 'SELECT config_name, config_value
FROM ' . $this->table . '
WHERE is_dynamic = 1';
$result = $this->db->sql_query($sql);
$config += $this->db->sql_fetchrowset($result);
$this->db->sql_freeresult($result);
}
else
{
$config = $cached_config = array();
$sql = 'SELECT config_name, config_value, is_dynamic
FROM ' . $this->table;
$result = $this->db->sql_query($sql);
while ($row = $this->db->sql_fetchrow($result))
{
if (!$row['is_dynamic'])
{
$cached_config[$row['config_name']] = $row['config_value'];
}
$config[$row['config_name']] = $row['config_value'];
}
$this->db->sql_freeresult($result);
$cache->put('config', $cached_config);
}
parent::__construct($config);
}
/**
* Sets a configuration option's value
*
* @param string $key The configuration option's name
* @param string $value New configuration value
* @param bool $cache Whether this variable should be cached or if it
* changes too frequently to be efficiently cached.
*/
public function set($key, $value, $cache = true)
{
$this->set_atomic($key, false, $value, $cache);
}
/**
* Sets a configuration option's value only if the old_value matches the
* current configuration value or the configuration value does not exist yet.
*
* @param string $key The configuration option's name
* @param mixed $old_value Current configuration value or false to ignore
* the old value
* @param string $new_value New configuration value
* @param bool $cache Whether this variable should be cached or if it
* changes too frequently to be efficiently cached
* @return bool True if the value was changed, false otherwise
*/
public function set_atomic($key, $old_value, $new_value, $cache = true)
{
$sql = 'UPDATE ' . $this->table . "
SET config_value = '" . $this->db->sql_escape($new_value) . "'
WHERE config_name = '" . $this->db->sql_escape($key) . "'";
if ($old_value !== false)
{
$sql .= " AND config_value = '" . $this->db->sql_escape($old_value) . "'";
}
$result = $this->db->sql_query($sql);
if (!$this->db->sql_affectedrows($result) && isset($this->config[$key]))
{
return false;
}
if (!isset($this->config[$key]))
{
$sql = 'INSERT INTO ' . $this->table . ' ' . $this->db->sql_build_array('INSERT', array(
'config_name' => $key,
'config_value' => $new_value,
'is_dynamic' => ($cache) ? 0 : 1));
$this->db->sql_query($sql);
}
if ($cache)
{
$this->cache->destroy('config');
}
$this->config[$key] = $new_value;
return true;
}
/**
* Increments an integer config value directly in the database.
*
* Using this method instead of setting the new value directly avoids race
* conditions and unlike set_atomic it cannot fail.
*
* @param string $key The configuration option's name
* @param int $increment Amount to increment by
* @param bool $cache Whether this variable should be cached or if it
* changes too frequently to be efficiently cached.
*/
function increment($key, $increment, $cache = true)
{
if (!isset($this->config[$key]))
{
$this->set($key, '0', $cache);
}
$sql_update = $this->db->cast_expr_to_string($this->db->cast_expr_to_bigint('config_value') . ' + ' . (int) $increment);
$this->db->sql_query('UPDATE ' . $this->table . '
SET config_value = ' . $sql_update . "
WHERE config_name = '" . $this->db->sql_escape($key) . "'");
if ($cache)
{
$this->cache->destroy('config');
}
$this->config[$key] += $increment;
}
}

View File

@ -447,6 +447,28 @@ class dbal
return $column_name . ' | ' . (1 << $bit) . (($compare) ? ' ' . $compare : '');
}
/**
* Returns SQL string to cast a string expression to an int.
*
* @param string $expression An expression evaluating to string
* @return string Expression returning an int
*/
function cast_expr_to_bigint($expression)
{
return $expression;
}
/**
* Returns SQL string to cast an integer expression to a string.
*
* @param string $expression An expression evaluating to int
* @return string Expression returning a string
*/
function cast_expr_to_string($expression)
{
return $expression;
}
/**
* Run more than one insert statement.
*

View File

@ -465,6 +465,22 @@ class dbal_firebird extends dbal
return 'BIN_OR(' . $column_name . ', ' . (1 << $bit) . ')' . (($compare) ? ' ' . $compare : '');
}
/**
* @inheritdoc
*/
function cast_expr_to_bigint($expression)
{
return 'CAST(' . $expression . ' as DECIMAL(255, 0))';
}
/**
* @inheritdoc
*/
function cast_expr_to_string($expression)
{
return 'CAST(' . $expression . ' as VARCHAR(255))';
}
/**
* return sql error array
* @access private

View File

@ -365,6 +365,22 @@ class dbal_postgres extends dbal
return $expression;
}
/**
* @inheritdoc
*/
function cast_expr_to_bigint($expression)
{
return 'CAST(' . $expression . ' as DECIMAL(255, 0))';
}
/**
* @inheritdoc
*/
function cast_expr_to_string($expression)
{
return 'CAST(' . $expression . ' as VARCHAR(255))';
}
/**
* return sql error array
* @access private

View File

@ -17,7 +17,11 @@ if (!defined('IN_PHPBB'))
}
// Common global functions
/**
* Casts a variable to the given type.
*
* @deprecated
*/
function set_var(&$result, $var, $type, $multibyte = false)
{
// no need for dependency injection here, if you have the object, call the method yourself!
@ -30,6 +34,7 @@ function set_var(&$result, $var, $type, $multibyte = false)
* See {@link phpbb_request_interface::variable phpbb_request_interface::variable} for
* documentation of this function's use.
*
* @deprecated
* @param mixed $var_name The form variable's name from which data shall be retrieved.
* If the value is an array this may be an array of indizes which will give
* direct access to a value at any depth. E.g. if the value of "var" is array(1 => "a")
@ -78,59 +83,46 @@ function request_var($var_name, $default, $multibyte = false, $cookie = false, p
/**
* Set config value. Creates missing config entry.
*
* @deprecated
*/
function set_config($config_name, $config_value, $is_dynamic = false)
function set_config($config_name, $config_value, $is_dynamic = false, phpbb_config $set_config = null)
{
global $db, $cache, $config;
static $config = null;
$sql = 'UPDATE ' . CONFIG_TABLE . "
SET config_value = '" . $db->sql_escape($config_value) . "'
WHERE config_name = '" . $db->sql_escape($config_name) . "'";
$db->sql_query($sql);
if (!$db->sql_affectedrows() && !isset($config[$config_name]))
if ($set_config !== null)
{
$sql = 'INSERT INTO ' . CONFIG_TABLE . ' ' . $db->sql_build_array('INSERT', array(
'config_name' => $config_name,
'config_value' => $config_value,
'is_dynamic' => ($is_dynamic) ? 1 : 0));
$db->sql_query($sql);
$config = $set_config;
if (empty($config_name))
{
return;
}
}
$config[$config_name] = $config_value;
if (!$is_dynamic)
{
$cache->destroy('config');
}
$config->set($config_name, $config_value, !$is_dynamic);
}
/**
* Set dynamic config value with arithmetic operation.
*
* @deprecated
*/
function set_config_count($config_name, $increment, $is_dynamic = false)
function set_config_count($config_name, $increment, $is_dynamic = false, phpbb_config $set_config = null)
{
global $db, $cache;
static $config = null;
switch ($db->sql_layer)
if ($set_config !== null)
{
case 'firebird':
case 'postgres':
$sql_update = 'CAST(CAST(config_value as DECIMAL(255, 0)) + ' . (int) $increment . ' as VARCHAR(255))';
break;
$config = $set_config;
// MySQL, SQlite, mssql, mssql_odbc, oracle
default:
$sql_update = 'config_value + ' . (int) $increment;
break;
if (empty($config_name))
{
return;
}
}
$db->sql_query('UPDATE ' . CONFIG_TABLE . ' SET config_value = ' . $sql_update . " WHERE config_name = '" . $db->sql_escape($config_name) . "'");
if (!$is_dynamic)
{
$cache->destroy('config');
}
$config->increment($config_name, $increment, !$is_dynamic);
}
/**
@ -3546,7 +3538,7 @@ function phpbb_checkdnsrr($host, $type = 'MX')
// but until 5.3.3 it only works for MX records
// See: http://bugs.php.net/bug.php?id=51844
// Call checkdnsrr() if
// Call checkdnsrr() if
// we're looking for an MX record or
// we're not on Windows or
// we're running a PHP version where #51844 has been fixed
@ -3566,7 +3558,7 @@ function phpbb_checkdnsrr($host, $type = 'MX')
// dns_get_record() is available since PHP 5; since PHP 5.3 also on Windows,
// but on Windows it does not work reliable for AAAA records before PHP 5.3.1
// Call dns_get_record() if
// Call dns_get_record() if
// we're not looking for an AAAA record or
// we're not on Windows or
// we're running a PHP version where AAAA lookups work reliable
@ -3596,7 +3588,7 @@ function phpbb_checkdnsrr($host, $type = 'MX')
foreach ($resultset as $result)
{
if (
isset($result['host']) && $result['host'] == $host &&
isset($result['host']) && $result['host'] == $host &&
isset($result['type']) && $result['type'] == $type
)
{

View File

@ -165,17 +165,9 @@ include($phpbb_root_path . 'language/' . $language . '/install.' . $phpEx);
$inline_update = (request_var('type', 0)) ? true : false;
// To let set_config() calls succeed, we need to make the config array available globally
$config = array();
$sql = 'SELECT *
FROM ' . CONFIG_TABLE;
$result = $db->sql_query($sql);
while ($row = $db->sql_fetchrow($result))
{
$config[$row['config_name']] = $row['config_value'];
}
$db->sql_freeresult($result);
$config = new phpbb_config_db($db, $cache_factory->get_driver());
set_config(null, null, null, $config);
set_config_count(null, null, null, $config);
// We do not include DB Tools here, because we can not be sure the file is up-to-date ;)
// Instead, this file defines a clean db_tools version (we are also not able to provide a different file, else the database update will not work standalone)

View File

@ -281,9 +281,9 @@ else
}
// Set some standard variables we want to force
$config = array(
$config = new phpbb_config(array(
'load_tplcompile' => '1'
);
));
$template->set_custom_template('../adm/style', 'admin');
$template->assign_var('T_TEMPLATE_PATH', '../adm/style');

View File

@ -130,16 +130,7 @@ class install_convert extends module
unset($dbpasswd);
// We need to fill the config to let internal functions correctly work
$sql = 'SELECT *
FROM ' . CONFIG_TABLE;
$result = $db->sql_query($sql);
$config = array();
while ($row = $db->sql_fetchrow($result))
{
$config[$row['config_name']] = $row['config_value'];
}
$db->sql_freeresult($result);
$config = new phpbb_config_db($db, new phpbb_cache_driver_null);
// Detect if there is already a conversion in progress at this point and offer to resume
// It's quite possible that the user will get disconnected during a large conversion so they need to be able to resume it
@ -350,16 +341,7 @@ class install_convert extends module
$this->page_title = $lang['STAGE_SETTINGS'];
// We need to fill the config to let internal functions correctly work
$sql = 'SELECT *
FROM ' . CONFIG_TABLE;
$result = $db->sql_query($sql);
$config = array();
while ($row = $db->sql_fetchrow($result))
{
$config[$row['config_name']] = $row['config_value'];
}
$db->sql_freeresult($result);
$config = new phpbb_config_db($db, new phpbb_cache_driver_null);
$convertor_tag = request_var('tag', '');
@ -597,16 +579,8 @@ class install_convert extends module
$db->sql_connect($dbhost, $dbuser, $dbpasswd, $dbname, $dbport, false, true);
unset($dbpasswd);
$sql = 'SELECT *
FROM ' . CONFIG_TABLE;
$result = $db->sql_query($sql);
$config = array();
while ($row = $db->sql_fetchrow($result))
{
$config[$row['config_name']] = $row['config_value'];
}
$db->sql_freeresult($result);
// We need to fill the config to let internal functions correctly work
$config = new phpbb_config_db($db, new phpbb_cache_driver_null);
// Override a couple of config variables for the duration
$config['max_quote_depth'] = 0;

View File

@ -1460,17 +1460,8 @@ class install_install extends module
include_once($phpbb_root_path . 'includes/constants.' . $phpEx);
include_once($phpbb_root_path . 'includes/search/fulltext_native.' . $phpEx);
// Fill the config array - it is needed by those functions we call
$sql = 'SELECT *
FROM ' . CONFIG_TABLE;
$result = $db->sql_query($sql);
$config = array();
while ($row = $db->sql_fetchrow($result))
{
$config[$row['config_name']] = $row['config_value'];
}
$db->sql_freeresult($result);
// We need to fill the config to let internal functions correctly work
$config = new phpbb_config_db($db, new phpbb_cache_driver_null);
$error = false;
$search = new fulltext_native($error);

View File

@ -101,17 +101,8 @@ class install_update extends module
// We do not need this any longer, unset for safety purposes
unset($dbpasswd);
$config = array();
$sql = 'SELECT config_name, config_value
FROM ' . CONFIG_TABLE;
$result = $db->sql_query($sql);
while ($row = $db->sql_fetchrow($result))
{
$config[$row['config_name']] = $row['config_value'];
}
$db->sql_freeresult($result);
// We need to fill the config to let internal functions correctly work
$config = new phpbb_config_db($db, new phpbb_cache_driver_null);
// Force template recompile
$config['load_tplcompile'] = 1;

View File

@ -82,7 +82,10 @@ if ($id)
}
unset($dbpasswd);
$config = $cache->obtain_config();
$config = new phpbb_config_db($db, $cache_factory->get_driver());
set_config(null, null, null, $config);
set_config_count(null, null, null, $config);
$user = false;
// try to get a session ID from REQUEST array

View File

@ -0,0 +1,112 @@
<?php
/**
*
* @package testing
* @copyright (c) 2010 phpBB Group
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/
class phpbb_config_test extends phpbb_test_case
{
public function test_offset_exists()
{
$config = new phpbb_config(array('foo' => 'bar'));
$this->assertTrue(isset($config['foo']));
$this->assertFalse(isset($config['foobar']));
}
public function test_offset_get()
{
$config = new phpbb_config(array('foo' => 'bar'));
$this->assertEquals('bar', $config['foo']);
}
public function test_offset_get_missing()
{
$config = new phpbb_config(array());
$this->assertEquals('', $config['foo']);
}
public function test_offset_set()
{
$config = new phpbb_config(array());
$config['foo'] = 'x';
$this->assertEquals('x', $config['foo']);
}
public function test_offset_unset_fails()
{
$this->setExpectedTriggerError(E_USER_ERROR);
$config = new phpbb_config(array('foo' => 'x'));
unset($config['foo']);
}
public function test_count()
{
$config = new phpbb_config(array('foo' => 'bar'));
$this->assertEquals(1, count($config));
}
public function test_iterate()
{
$vars = array('foo' => '23', 'bar' => '42');
$config = new phpbb_config($vars);
$count = 0;
foreach ($config as $key => $value)
{
$this->assertTrue(isset($vars[$key]));
$this->assertEquals($vars[$key], $value);
$count++;
}
$this->assertEquals(count($vars), $count);
}
public function test_set_overwrite()
{
$config = new phpbb_config(array('foo' => 'x'));
$config->set('foo', 'bar');
$this->assertEquals('bar', $config['foo']);
}
public function test_set_new()
{
$config = new phpbb_config(array());
$config->set('foo', 'bar');
$this->assertEquals('bar', $config['foo']);
}
public function test_set_atomic_overwrite()
{
$config = new phpbb_config(array('foo' => 'bar'));
$this->assertTrue($config->set_atomic('foo', 'bar', '23'));
$this->assertEquals('23', $config['foo']);
}
public function test_set_atomic_new()
{
$config = new phpbb_config(array());
$this->assertTrue($config->set_atomic('foo', false, '23'));
$this->assertEquals('23', $config['foo']);
}
public function test_set_atomic_failure()
{
$config = new phpbb_config(array('foo' => 'bar'));
$this->assertFalse($config->set_atomic('foo', 'wrong', '23'));
$this->assertEquals('bar', $config['foo']);
}
public function test_increment()
{
$config = new phpbb_config(array('foo' => '23'));
$config->increment('foo', 3);
$this->assertEquals(26, $config['foo']);
$config->increment('foo', 1);
$this->assertEquals(27, $config['foo']);
}
}

117
tests/config/db_test.php Normal file
View File

@ -0,0 +1,117 @@
<?php
/**
*
* @package testing
* @copyright (c) 2010 phpBB Group
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/
require_once __DIR__ . '/../mock/cache.php';
class phpbb_config_db_test extends phpbb_database_test_case
{
private $cache;
private $db;
private $config;
public function getDataSet()
{
return $this->createXMLDataSet(__DIR__ . '/fixtures/config.xml');
}
public function setUp()
{
parent::setUp();
$this->cache = new phpbb_mock_cache;
$this->db = $this->new_dbal();
$this->config = new phpbb_config_db($this->cache, $this->db, 'phpbb_config');
}
public function test_load_config()
{
$this->assertEquals('23', $this->config['foo']);
$this->assertEquals('42', $this->config['bar']);
}
public function test_offset_set()
{
$this->config['foo'] = 'x'; // temporary set
$this->assertEquals('x', $this->config['foo']);
$config2 = new phpbb_config_db($this->cache, $this->db, 'phpbb_config');
$this->assertEquals('23', $config2['foo']);
}
public function test_set_overwrite()
{
$this->config->set('foo', '17');
$this->assertEquals('17', $this->config['foo']);
// re-read config and populate cache
$config2 = new phpbb_config_db($this->cache, $this->db, 'phpbb_config');
$this->cache->checkVar($this, 'config', array('foo' => '17'));
}
public function test_set_overwrite_uncached()
{
$this->config->set('bar', '17', false);
// re-read config and populate cache
$config2 = new phpbb_config_db($this->cache, $this->db, 'phpbb_config');
$this->cache->checkVar($this, 'config', array('foo' => '23'));
}
public function test_set_new()
{
$this->config->set('foobar', '5');
$this->assertEquals('5', $this->config['foobar']);
// re-read config and populate cache
$config2 = new phpbb_config_db($this->cache, $this->db, 'phpbb_config');
$this->cache->checkVar($this, 'config', array('foo' => '23', 'foobar' => '5'));
}
public function test_set_new_uncached()
{
$this->config->set('foobar', '5', false);
$this->assertEquals('5', $this->config['foobar']);
// re-read config and populate cache
$config2 = new phpbb_config_db($this->cache, $this->db, 'phpbb_config');
$this->cache->checkVar($this, 'config', array('foo' => '23'));
}
public function test_set_atomic_overwrite()
{
$this->assertTrue($this->config->set_atomic('foo', '23', '17'));
$this->assertEquals('17', $this->config['foo']);
}
public function test_set_atomic_new()
{
$this->assertTrue($this->config->set_atomic('foobar', false, '5'));
$this->assertEquals('5', $this->config['foobar']);
}
public function test_set_atomic_failure()
{
$this->assertFalse($this->config->set_atomic('foo', 'wrong', '17'));
$this->assertEquals('23', $this->config['foo']);
}
public function test_increment()
{
$this->config->increment('foo', 3);
$this->assertEquals(26, $this->config['foo']);
$this->config->increment('foo', 1);
$this->assertEquals(27, $this->config['foo']);
}
public function test_increment_new()
{
$this->config->increment('foobar', 3);
$this->assertEquals(3, $this->config['foobar']);;
}
}

View File

@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8" ?>
<dataset>
<table name="phpbb_config">
<column>config_name</column>
<column>config_value</column>
<column>is_dynamic</column>
<row>
<value>foo</value>
<value>23</value>
<value>0</value>
</row>
<row>
<value>bar</value>
<value>42</value>
<value>1</value>
</row>
</table>
</dataset>

View File

@ -37,6 +37,11 @@ class phpbb_mock_cache implements phpbb_cache_driver_interface
$test->assertEquals($data, $this->data[$var_name]);
}
public function checkVarUnset(PHPUnit_Framework_Assert $test, $var_name)
{
$test->assertFalse(isset($this->data[$var_name]));
}
public function check(PHPUnit_Framework_Assert $test, $data)
{
$test->assertEquals($data, $this->data);
@ -59,6 +64,7 @@ class phpbb_mock_cache implements phpbb_cache_driver_interface
}
function destroy($var_name, $table = '')
{
unset($this->data[$var_name]);
}
public function _exists($var_name)
{