Write a CacheBackend implementation for PDO.
[hoplite.git] / testing / tests / views / pdo_cache_backend_test.php
1 <?php
2 // Hoplite
3 // Copyright (c) 2013 Blue Static
4 //
5 // This program is free software: you can redistribute it and/or modify it
6 // under the terms of the GNU General Public License as published by the Free
7 // Software Foundation, either version 3 of the License, or any later version.
8 //
9 // This program is distributed in the hope that it will be useful, but WITHOUT
10 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 // more details.
13 //
14 // You should have received a copy of the GNU General Public License along with
15 // this program. If not, see <http://www.gnu.org/licenses/>.
16
17 namespace hoplite\test;
18 use hoplite\views as views;
19
20 require_once HOPLITE_ROOT . '/views/pdo_cache_backend.php';
21
22 class PDOCacheBackendTest extends \PHPUnit_Framework_TestCase
23 {
24 private $db;
25 private $cache;
26
27 public function setUp()
28 {
29 $this->db = new \PDO('sqlite::memory:');
30 $this->db->Exec("
31 CREATE TABLE test_template_cache (
32 filename string PRIMARY KEY,
33 data string,
34 mtime int
35 );
36 ");
37
38 $this->cache = new views\PDOCacheBackend(
39 $this->db, 'test_template_cache', 'filename', 'data', 'mtime');
40 }
41
42 private function _GetCount()
43 {
44 $stmt = $this->db->Query("SELECT COUNT(*) AS count FROM test_template_cache");
45 return $stmt->FetchObject()->count;
46 }
47
48 public function testCacheMiss()
49 {
50 $this->assertNull($this->cache->GetTemplateDataForName('test', 100));
51 $this->db->Exec("INSERT INTO test_template_cache (filename, data, mtime) VALUES ('test', 'hello world', 100)");
52 $this->assertEquals('hello world', $this->cache->GetTemplateDataForName('test', 100));
53 $this->assertEquals(1, $this->_GetCount());
54
55 $this->assertNull($this->cache->GetTemplateDataForName('test', 200));
56 $this->assertEquals(0, $this->_GetCount());
57 }
58
59 public function testCacheStore()
60 {
61 $this->cache->StoreCompiledTemplate('name', 400, 'abcdefgh');
62 $this->assertEquals(1, $this->_GetCount());
63
64 $this->assertEquals('abcdefgh', $this->cache->GetTemplateDataForName('name', 400));
65 $this->assertEquals(1, $this->_GetCount());
66
67 $this->assertNull($this->cache->GetTemplateDataForName('name', 500));
68 }
69 }