. namespace hoplite\test; use hoplite\views as views; require_once HOPLITE_ROOT . '/views/pdo_cache_backend.php'; class PDOCacheBackendTest extends \PHPUnit_Framework_TestCase { private $db; private $cache; public function setUp() { $this->db = new \PDO('sqlite::memory:'); $this->db->Exec(" CREATE TABLE test_template_cache ( filename string PRIMARY KEY, data string, mtime int ); "); $this->cache = new views\PDOCacheBackend( $this->db, 'test_template_cache', 'filename', 'data', 'mtime'); } private function _GetCount() { $stmt = $this->db->Query("SELECT COUNT(*) AS count FROM test_template_cache"); return $stmt->FetchObject()->count; } public function testCacheMiss() { $this->assertNull($this->cache->GetTemplateDataForName('test', 100)); $this->db->Exec("INSERT INTO test_template_cache (filename, data, mtime) VALUES ('test', 'hello world', 100)"); $this->assertEquals('hello world', $this->cache->GetTemplateDataForName('test', 100)); $this->assertEquals(1, $this->_GetCount()); $this->assertNull($this->cache->GetTemplateDataForName('test', 200)); $this->assertEquals(0, $this->_GetCount()); } public function testCacheStore() { $this->cache->StoreCompiledTemplate('name', 400, 'abcdefgh'); $this->assertEquals(1, $this->_GetCount()); $this->assertEquals('abcdefgh', $this->cache->GetTemplateDataForName('name', 400)); $this->assertEquals(1, $this->_GetCount()); $this->assertNull($this->cache->GetTemplateDataForName('name', 500)); } }