diff --git a/classes/Cache.php b/classes/Cache.php index 1469b8f..af7a11a 100644 --- a/classes/Cache.php +++ b/classes/Cache.php @@ -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); -} - ?> diff --git a/classes/Database.php b/classes/Database.php index ce8927a..41358af 100644 --- a/classes/Database.php +++ b/classes/Database.php @@ -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.'); } diff --git a/tests/classes/CacheTest.php b/tests/classes/CacheTest.php new file mode 100644 index 0000000..0b27836 --- /dev/null +++ b/tests/classes/CacheTest.php @@ -0,0 +1,90 @@ +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(); + } +} + +?>