[ticket/9983] Split cache test into per-driver files.

PHPBB3-9983
This commit is contained in:
Oleg Pudeyev 2012-12-01 15:03:44 -05:00
parent c852044d6e
commit 1e3dff83b3
4 changed files with 251 additions and 208 deletions

View file

@ -1,208 +0,0 @@
<?php
/**
*
* @package testing
* @copyright (c) 2010 phpBB Group
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
*
*/
require_once dirname(__FILE__) . '/../../phpBB/includes/functions.php';
class phpbb_cache_test extends phpbb_database_test_case
{
private $cache_dir;
public function __construct()
{
$this->cache_dir = dirname(__FILE__) . '/../tmp/cache/';
}
public function getDataSet()
{
return $this->createXMLDataSet(dirname(__FILE__) . '/fixtures/config.xml');
}
protected function setUp()
{
parent::setUp();
if (file_exists($this->cache_dir))
{
// cache directory possibly left after aborted
// or failed run earlier
$this->remove_cache_dir();
}
$this->create_cache_dir();
}
protected function tearDown()
{
if (file_exists($this->cache_dir))
{
$this->remove_cache_dir();
}
parent::tearDown();
}
private function create_cache_dir()
{
$this->get_test_case_helpers()->makedirs($this->cache_dir);
}
private function remove_cache_dir()
{
$iterator = new DirectoryIterator($this->cache_dir);
foreach ($iterator as $file)
{
if ($file != '.' && $file != '..')
{
unlink($this->cache_dir . '/' . $file);
}
}
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_file()
{
$driver = new phpbb_cache_driver_file($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'";
$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($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);
$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_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();
}
}

117
tests/cache/file_driver_test.php vendored Normal file
View file

@ -0,0 +1,117 @@
<?php
/**
*
* @package testing
* @copyright (c) 2010 phpBB Group
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
*
*/
class phpbb_cache_file_driver_test extends phpbb_database_test_case
{
private $cache_dir;
protected $driver;
public function __construct()
{
$this->cache_dir = dirname(__FILE__) . '/../tmp/cache/';
}
public function getDataSet()
{
return $this->createXMLDataSet(dirname(__FILE__) . '/fixtures/config.xml');
}
protected function setUp()
{
parent::setUp();
if (file_exists($this->cache_dir))
{
// cache directory possibly left after aborted
// or failed run earlier
$this->remove_cache_dir();
}
$this->create_cache_dir();
$this->driver = new phpbb_cache_driver_file($this->cache_dir);
}
protected function tearDown()
{
if (file_exists($this->cache_dir))
{
$this->remove_cache_dir();
}
parent::tearDown();
}
private function create_cache_dir()
{
$this->get_test_case_helpers()->makedirs($this->cache_dir);
}
private function remove_cache_dir()
{
$iterator = new DirectoryIterator($this->cache_dir);
foreach ($iterator as $file)
{
if ($file != '.' && $file != '..')
{
unlink($this->cache_dir . '/' . $file);
}
}
rmdir($this->cache_dir);
}
public function test_cache_driver_file()
{
$this->driver->put('test_key', 'test_value');
$this->driver->save();
$this->assertEquals(
'test_value',
$this->driver->get('test_key'),
'File ACM put and get'
);
}
public function test_cache_sql_file()
{
global $db, $cache;
$db = $this->new_dbal();
$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($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();
}
}

45
tests/cache/null_driver_test.php vendored Normal file
View file

@ -0,0 +1,45 @@
<?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
{
public function getDataSet()
{
return $this->createXMLDataSet(dirname(__FILE__) . '/fixtures/config.xml');
}
public function test_null_cache_sql()
{
$driver = new phpbb_cache_driver_null;
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();
}
}

89
tests/cache/redis_driver_test.php vendored Normal file
View file

@ -0,0 +1,89 @@
<?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_redis_driver_test extends phpbb_database_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_redis()
{
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();
}
}