From 0f6c998109d577c6e56d62f10680dc840c01e6db Mon Sep 17 00:00:00 2001 From: "Timothy J. Warren" Date: Thu, 7 Apr 2016 12:34:57 -0400 Subject: [PATCH] Add tests for SQL based api cache --- src/Aviat/Ion/Cache/Driver/SQLDriver.php | 29 ++++++--------- tests/Ion/Cache/Driver/CacheDriverBase.php | 41 ++++++++++++++++++++++ tests/Ion/Cache/Driver/SQLDriverTest.php | 20 +++++++++++ 3 files changed, 71 insertions(+), 19 deletions(-) create mode 100644 tests/Ion/Cache/Driver/CacheDriverBase.php create mode 100644 tests/Ion/Cache/Driver/SQLDriverTest.php diff --git a/src/Aviat/Ion/Cache/Driver/SQLDriver.php b/src/Aviat/Ion/Cache/Driver/SQLDriver.php index 8495907f..a68ded41 100644 --- a/src/Aviat/Ion/Cache/Driver/SQLDriver.php +++ b/src/Aviat/Ion/Cache/Driver/SQLDriver.php @@ -33,16 +33,7 @@ class SQLDriver extends DB implements \Aviat\Ion\Cache\CacheDriverInterface { public function __construct(ContainerInterface $container) { parent::__construct($container); - - try - { - $this->db = \Query($this->db_config['collection']); - } - catch (\PDOException $e) - { - $this->valid_database = FALSE; - return FALSE; - } + $this->db = \Query($this->db_config['collection']); } /** @@ -94,11 +85,11 @@ class SQLDriver extends DB implements \Aviat\Ion\Cache\CacheDriverInterface { } /** - * Invalidate a cached value - * - * @param string $key - * @return CacheDriverInterface - */ + * Invalidate a cached value + * + * @param string $key + * @return CacheDriverInterface + */ public function invalidate($key) { $this->db->where('key', $key) @@ -108,10 +99,10 @@ class SQLDriver extends DB implements \Aviat\Ion\Cache\CacheDriverInterface { } /** - * Clear the contents of the cache - * - * @return void - */ + * Clear the contents of the cache + * + * @return void + */ public function invalidateAll() { $this->db->truncate('cache'); diff --git a/tests/Ion/Cache/Driver/CacheDriverBase.php b/tests/Ion/Cache/Driver/CacheDriverBase.php new file mode 100644 index 00000000..e0ba7a94 --- /dev/null +++ b/tests/Ion/Cache/Driver/CacheDriverBase.php @@ -0,0 +1,41 @@ + [ + 'baz' => 'foobar' + ] + ]; + + protected $bar = 'secondvalue'; + + public function testHasCacheDriver() + { + $this->assertTrue((bool) $this->driver); + } + + public function testDriverGetSet() + { + $this->driver->set('foo', $this->foo); + $this->assertEquals($this->driver->get('foo'), $this->foo); + } + + public function testInvalidate() + { + $this->driver->set('foo', $this->foo); + $this->driver->invalidate('foo'); + $this->assertEmpty($this->driver->get('foo')); + } + + public function testInvalidateAll() + { + $this->driver->set('foo', $this->foo); + $this->driver->set('bar', $this->bar); + + $this->driver->invalidateAll(); + + $this->assertEmpty($this->driver->get('foo')); + $this->assertEmpty($this->driver->get('bar')); + } +} \ No newline at end of file diff --git a/tests/Ion/Cache/Driver/SQLDriverTest.php b/tests/Ion/Cache/Driver/SQLDriverTest.php new file mode 100644 index 00000000..6eb2c349 --- /dev/null +++ b/tests/Ion/Cache/Driver/SQLDriverTest.php @@ -0,0 +1,20 @@ +driver = new SQLDriver($this->container); + $friend = new Friend($this->driver); + $friend->db->query('CREATE TABLE IF NOT EXISTS "cache" ("key" TEXT NULL, "value" TEXT NULL, PRIMARY KEY ("key"))'); + } +} \ No newline at end of file