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:
parent
7b7fd901c9
commit
6257f89b18
3 changed files with 111 additions and 70 deletions
|
@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -219,31 +193,18 @@ class Cache extends Object
|
|||
*/
|
||||
public function delete($keys)
|
||||
{
|
||||
set_error_handler('cacheErrorHandler');
|
||||
|
||||
try
|
||||
if (!is_array($keys))
|
||||
{
|
||||
if (!is_array($keys))
|
||||
{
|
||||
$keys = array($keys);
|
||||
}
|
||||
|
||||
// Memcache() doesn't let you pass an array to delete all records the same way you can with get()
|
||||
foreach ($keys as $key)
|
||||
{
|
||||
$this->connection->delete(strtoupper($this->namespace . $key));
|
||||
}
|
||||
|
||||
$return = true;
|
||||
}
|
||||
catch (Exception $exception)
|
||||
{
|
||||
$return = false;
|
||||
$keys = array($keys);
|
||||
}
|
||||
|
||||
restore_error_handler();
|
||||
// Memcache() doesn't let you pass an array to delete all records the same way you can with get()
|
||||
foreach ($keys as $key)
|
||||
{
|
||||
$this->connection->delete(strtoupper($this->namespace . $key));
|
||||
}
|
||||
|
||||
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));
|
||||
}
|
||||
catch (Exception $exception)
|
||||
{
|
||||
$return = false;
|
||||
}
|
||||
|
||||
restore_error_handler();
|
||||
|
||||
return $return;
|
||||
return $this->connection->increment(strtoupper($this->namespace . $key));
|
||||
}
|
||||
}
|
||||
|
||||
function cacheErrorHandler($errno, $errstr, $errfile, $errline)
|
||||
{
|
||||
throw new Exception($errstr);
|
||||
}
|
||||
|
||||
?>
|
||||
|
|
|
@ -137,8 +137,15 @@ class Database extends Object
|
|||
}
|
||||
elseif (is_array($config->datasources))
|
||||
{
|
||||
$datasources = $config->datasources;
|
||||
$datasource_name = key($datasources);
|
||||
$datasources = $config->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.');
|
||||
}
|
||||
|
||||
|
|
90
tests/classes/CacheTest.php
Normal file
90
tests/classes/CacheTest.php
Normal 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();
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
Loading…
Add table
Add a link
Reference in a new issue