Split TemplateLoader's cache business into another interface and class.
[hoplite.git] / testing / tests / views / file_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/file_cache_backend.php';
21
22 class TestFileCacheBackend extends views\FileCacheBackend
23 {
24 public function T_CachePath($path)
25 {
26 return $this->_CachePath($path);
27 }
28 }
29
30 class FileCacheBackendTest extends \PHPUnit_Framework_TestCase
31 {
32 public function setUp()
33 {
34 $path = dirname(__FILE__) . '/cache/';
35 $this->fixture = new TestFileCacheBackend($path);
36
37 $files = scandir($path);
38 foreach ($files as $file)
39 if ($file[0] != '.')
40 unlink($path . $file);
41 }
42
43 public function testCacheCompiled()
44 {
45 $files = scandir($this->fixture->cache_path());
46 $this->assertEquals(2, count($files)); // Only dotfiles.
47
48 $this->assertNull($this->fixture->GetTemplateDataForName('cache_test', time()));
49
50 $string = 'This is a test.';
51
52 $this->fixture->StoreCompiledTemplate('cache_test', time(), $string);
53 $files = scandir($this->fixture->cache_path());
54 $this->assertEquals(3, count($files));
55
56 $actual = file_get_contents($this->fixture->cache_path() . '/cache_test.phpi');
57 $this->assertEquals($string, $actual);
58 }
59
60 public function testCacheHit()
61 {
62 $path = $this->fixture->cache_path() . '/cache_test.phpi';
63 $expected = 'Cache hit!';
64 file_put_contents($path, $expected);
65
66 $files = scandir($this->fixture->cache_path());
67 $this->assertEquals(3, count($files));
68
69 $actual = $this->fixture->GetTemplateDataForName('cache_test', filemtime($path));
70 $this->assertEquals($expected, $actual);
71 }
72
73 public function testCacheMiss()
74 {
75 file_put_contents($this->fixture->cache_path() . '/cache_test.phpi', 'Invalid template data');
76 $files = scandir($this->fixture->cache_path());
77 $this->assertEquals(3, count($files));
78
79 $actual = $this->fixture->GetTemplateDataForName('cache_test', time() + 60);
80 $this->assertNull($actual);
81 }
82 }