Added most of the Cache tests

Dropped some unnecessary try/catch blocks and updated the Database class to
not use any data sources that lack a driver.
This commit is contained in:
Joshua Sherman 2014-01-14 02:19:40 -05:00
parent 7b7fd901c9
commit 6257f89b18
3 changed files with 111 additions and 70 deletions

View file

@ -146,8 +146,6 @@ class Cache extends Object
*/
public function get($keys)
{
set_error_handler('cacheErrorHandler');
if (is_array($keys))
{
foreach ($keys as $index => $key)
@ -160,18 +158,7 @@ class Cache extends Object
$keys = strtoupper($this->namespace . $keys);
}
try
{
$return = $this->connection->get($keys);
}
catch (Exception $exception)
{
$return = false;
}
restore_error_handler();
return $return;
return $this->connection->get($keys);
}
/**
@ -191,22 +178,9 @@ class Cache extends Object
*/
public function set($key, $value, $expire = Time::DAY)
{
set_error_handler('cacheErrorHandler');
$key = strtoupper($key);
try
{
$return = $this->connection->set(strtoupper($this->namespace . $key), $value, 0, $expire);
}
catch (Exception $exception)
{
$return = false;
}
restore_error_handler();
return $return;
return $this->connection->set(strtoupper($this->namespace . $key), $value, 0, $expire);
}
/**
@ -218,10 +192,6 @@ class Cache extends Object
* @return boolean status of deleting the key
*/
public function delete($keys)
{
set_error_handler('cacheErrorHandler');
try
{
if (!is_array($keys))
{
@ -234,16 +204,7 @@ class Cache extends Object
$this->connection->delete(strtoupper($this->namespace . $key));
}
$return = true;
}
catch (Exception $exception)
{
$return = false;
}
restore_error_handler();
return $return;
return true;
}
/**
@ -257,26 +218,8 @@ class Cache extends Object
*/
public function increment($key)
{
set_error_handler('cacheErrorHandler');
try
{
$return = $this->connection->increment(strtoupper($this->namespace . $key));
return $this->connection->increment(strtoupper($this->namespace . $key));
}
catch (Exception $exception)
{
$return = false;
}
restore_error_handler();
return $return;
}
}
function cacheErrorHandler($errno, $errstr, $errfile, $errline)
{
throw new Exception($errstr);
}
?>

View file

@ -138,7 +138,14 @@ class Database extends Object
elseif (is_array($config->datasources))
{
$datasources = $config->datasources;
$datasource_name = key($datasources);
foreach ($datasources as $name => $datasource)
{
if (isset($datasource['driver']))
{
$datasource_name = $name;
}
}
}
}
@ -156,6 +163,7 @@ class Database extends Object
if (!isset($datasource['driver']))
{
var_Dump($datasource);
throw new Exception('The specified datasource lacks a driver.');
}

View file

@ -0,0 +1,90 @@
<?php
/**
* @runTestsInSeparateProcesses
*/
class CacheTest extends PHPUnit_Framework_TestCase
{
private $config;
private $cache;
public function setUp()
{
$this->config = Config::getInstance();
$this->config->data['pickles']['cache'] = 'mc';
$this->config->data['datasources']['mc'] = [
'type' => 'memcache',
'hostname' => 'localhost',
'port' => 11211,
];
$this->cache = Cache::getInstance();
}
public function testGetInstance()
{
$this->assertInstanceOf('Cache', $this->cache);
}
public function testSetAndGet()
{
$key = String::random();
$value = String::random();
$this->cache->set($key, $value);
$this->assertEquals($value, $this->cache->get($key));
}
public function testSetAndGetMultiple()
{
$keys = $values = $expected = [];
for ($i = 0; $i < 5; $i++)
{
$keys[] = String::random();
$values[] = String::random();
}
foreach ($keys as $key => $key_name)
{
$value = $values[$key];
$expected[strtoupper($key_name)] = $value;
$this->cache->set($key_name, $value);
}
$this->assertEquals($expected, $this->cache->get($keys));
}
public function testDelete()
{
$key = String::random();
$value = String::random();
$this->cache->set($key, $value);
$this->cache->delete($key);
$this->assertFalse($this->cache->get($key));
}
public function testIncrement()
{
$key = String::random();
$this->assertFalse($this->cache->increment($key));
$this->cache->set($key, 1);
$this->assertEquals(2, $this->cache->increment($key));
$this->assertEquals(3, $this->cache->increment($key));
$this->assertEquals(4, $this->cache->increment($key));
}
// Doesn't do much but test that the destructor doesn't explode
public function testDestructor()
{
$this->cache->__destruct();
}
}
?>