diff options
Diffstat (limited to 'tests/config')
| -rw-r--r-- | tests/config/config_test.php | 120 | ||||
| -rw-r--r-- | tests/config/db_test.php | 164 | ||||
| -rw-r--r-- | tests/config/db_text_test.php | 115 | ||||
| -rw-r--r-- | tests/config/fixtures/config.xml | 18 | ||||
| -rw-r--r-- | tests/config/fixtures/config_text.xml | 19 | 
5 files changed, 436 insertions, 0 deletions
diff --git a/tests/config/config_test.php b/tests/config/config_test.php new file mode 100644 index 0000000000..5845cc4590 --- /dev/null +++ b/tests/config/config_test.php @@ -0,0 +1,120 @@ +<?php +/** +* +* @package testing +* @copyright (c) 2010 phpBB Group +* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* +*/ + +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']); +	} + +	public function test_delete() +	{ +		$config = new phpbb_config(array('foo' => 'bar')); + +		$config->delete('foo'); +		$this->assertFalse(isset($config['foo'])); +	} +} diff --git a/tests/config/db_test.php b/tests/config/db_test.php new file mode 100644 index 0000000000..0b8f73d53a --- /dev/null +++ b/tests/config/db_test.php @@ -0,0 +1,164 @@ +<?php +/** +* +* @package testing +* @copyright (c) 2010 phpBB Group +* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* +*/ + +class phpbb_config_db_test extends phpbb_database_test_case +{ +	private $cache; +	private $db; +	private $config; + +	public function getDataSet() +	{ +		return $this->createXMLDataSet(dirname(__FILE__) . '/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->db, $this->cache, 'phpbb_config'); +	} + +	public function test_load_config() +	{ +		$this->assertEquals('23', $this->config['foo']); +		$this->assertEquals('42', $this->config['bar']); +	} + +	public function test_load_cached() +	{ +		$cache = new phpbb_mock_cache(array('config' => array('x' => 'y'))); +		$this->config = new phpbb_config_db($this->db, $cache, 'phpbb_config'); + +		$this->assertTrue(!isset($this->config['foo'])); +		$this->assertEquals('42', $this->config['bar']); + +		$this->assertEquals('y', $this->config['x']); +	} + +	public function test_offset_set() +	{ +		$this->config['foo'] = 'x'; // temporary set +		$this->assertEquals('x', $this->config['foo']); + +		$config2 = new phpbb_config_db($this->db, $this->cache, '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->db, $this->cache, '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->db, $this->cache, '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->db, $this->cache, '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->db, $this->cache, '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']);; +	} + +	public function test_delete() +	{ +		$this->assertTrue(isset($this->config['foo'])); +		$this->config->delete('foo'); +		$this->cache->checkVarUnset($this, 'foo'); +		$this->assertFalse(isset($this->config['foo'])); + +		// re-read config and populate cache +		$cache2 = new phpbb_mock_cache; +		$config2 = new phpbb_config_db($this->db, $cache2, 'phpbb_config'); +		$cache2->checkVarUnset($this, 'foo'); +		$this->assertFalse(isset($config2['foo'])); +	} + +	public function test_delete_write_read_not_cacheable() +	{ +		// bar is dynamic +		$this->assertTrue(isset($this->config['bar'])); +		$this->config->delete('bar'); +		$this->cache->checkVarUnset($this, 'bar'); +		$this->assertFalse(isset($this->config['bar'])); +		 +		$this->config->set('bar', 'new bar', false); +		$this->assertEquals('new bar', $this->config['bar']); +	} + +	public function test_delete_write_read_cacheable() +	{ +		// foo is not dynamic +		$this->assertTrue(isset($this->config['foo'])); +		$this->config->delete('foo'); +		$this->cache->checkVarUnset($this, 'foo'); +		$this->assertFalse(isset($this->config['foo'])); +		 +		$this->config->set('foo', 'new foo', true); +		$this->assertEquals('new foo', $this->config['foo']); +	} +} diff --git a/tests/config/db_text_test.php b/tests/config/db_text_test.php new file mode 100644 index 0000000000..4818bba8c9 --- /dev/null +++ b/tests/config/db_text_test.php @@ -0,0 +1,115 @@ +<?php +/** +* +* @package testing +* @copyright (c) 2013 phpBB Group +* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* +*/ + +class phpbb_config_db_text_test extends phpbb_database_test_case +{ +	private $db; +	private $config_text; + +	public function getDataSet() +	{ +		return $this->createXMLDataSet(dirname(__FILE__) . '/fixtures/config_text.xml'); +	} + +	public function setUp() +	{ +		parent::setUp(); + +		$this->db = $this->new_dbal(); +		$this->config_text = new phpbb_config_db_text($this->db, 'phpbb_config_text'); +	} + +	public function test_get() +	{ +		$this->assertSame('23', $this->config_text->get('foo')); +		$this->assertSame('string-de-ding', $this->config_text->get('meh')); +	} + +	public function test_get_nonexisting() +	{ +		$this->assertNull($this->config_text->get('noooooo')); +	} + +	public function test_set_new_get() +	{ +		$this->config_text->set('barz', 'phpbb'); +		$this->assertSame('phpbb', $this->config_text->get('barz')); +	} + +	public function test_set_replace_get() +	{ +		$this->config_text->set('foo', '24'); +		$this->assertSame('24', $this->config_text->get('foo')); +	} + +	public function test_set_get_long_string() +	{ +		$expected = str_repeat('ABC', 10000); +		$this->config_text->set('long', $expected); +		$this->assertSame($expected, $this->config_text->get('long')); +	} + +	public function test_delete_get() +	{ +		$this->config_text->delete('foo'); +		$this->assertNull($this->config_text->get('foo')); + +		$this->assertSame('42', $this->config_text->get('bar')); +		$this->assertSame('string-de-ding', $this->config_text->get('meh')); +	} + +	public function test_get_array_empty() +	{ +		$this->assertEmpty($this->config_text->get_array(array('key1', 'key2'))); +	} + +	public function test_get_array_subset() +	{ +		$expected = array( +			'bar' => '42', +			'foo' => '23', +		); + +		$actual = $this->config_text->get_array(array_keys($expected)); +		ksort($actual); + +		$this->assertSame($expected, $actual); +	} + +	public function test_set_array_get_array_subset() +	{ +		$set_array_param = array( +			// New entry +			'baby' => 'phpBB', +			// Entry update +			'bar' => '64', +		); + +		$this->config_text->set_array($set_array_param); + +		$expected = array_merge($set_array_param, array( +			'foo' => '23', +		)); + +		$actual = $this->config_text->get_array(array_keys($expected)); +		ksort($actual); + +		$this->assertSame($expected, $actual); +	} + +	public function test_delete_array_get_remaining() +	{ +		$this->config_text->delete_array(array('foo', 'bar')); + +		$this->assertNull($this->config_text->get('bar')); +		$this->assertNull($this->config_text->get('foo')); + +		$this->assertSame('string-de-ding', $this->config_text->get('meh')); +	} +} diff --git a/tests/config/fixtures/config.xml b/tests/config/fixtures/config.xml new file mode 100644 index 0000000000..9d395b685c --- /dev/null +++ b/tests/config/fixtures/config.xml @@ -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> diff --git a/tests/config/fixtures/config_text.xml b/tests/config/fixtures/config_text.xml new file mode 100644 index 0000000000..5acac13ea3 --- /dev/null +++ b/tests/config/fixtures/config_text.xml @@ -0,0 +1,19 @@ +<?xml version="1.0" encoding="UTF-8" ?> +<dataset> +	<table name="phpbb_config_text"> +		<column>config_name</column> +		<column>config_value</column> +		<row> +			<value>foo</value> +			<value>23</value> +		</row> +		<row> +			<value>bar</value> +			<value>42</value> +		</row> +		<row> +			<value>meh</value> +			<value>string-de-ding</value> +		</row> +	</table> +</dataset>  | 
