3 // Copyright (c) 2013 Blue Static
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.
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
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/>.
17 namespace hoplite\test
;
18 use hoplite\views
as views
;
20 require_once HOPLITE_ROOT
. '/views/file_cache_backend.php';
22 class TestFileCacheBackend
extends views\FileCacheBackend
24 public function T_CachePath($path)
26 return $this->_CachePath($path);
30 class FileCacheBackendTest
extends \PHPUnit_Framework_TestCase
32 public function setUp()
34 $path = dirname(__FILE__
) . '/cache/';
35 $this->fixture
= new TestFileCacheBackend($path);
37 $files = scandir($path);
38 foreach ($files as $file)
40 unlink($path . $file);
43 public function testCacheCompiled()
45 $files = scandir($this->fixture
->cache_path());
46 $this->assertEquals(2, count($files)); // Only dotfiles.
48 $this->assertNull($this->fixture
->GetTemplateDataForName('cache_test', time()));
50 $string = 'This is a test.';
52 $this->fixture
->StoreCompiledTemplate('cache_test', time(), $string);
53 $files = scandir($this->fixture
->cache_path());
54 $this->assertEquals(3, count($files));
56 $actual = file_get_contents($this->fixture
->cache_path() . '/cache_test.phpi');
57 $this->assertEquals($string, $actual);
60 public function testCacheHit()
62 $path = $this->fixture
->cache_path() . '/cache_test.phpi';
63 $expected = 'Cache hit!';
64 file_put_contents($path, $expected);
66 $files = scandir($this->fixture
->cache_path());
67 $this->assertEquals(3, count($files));
69 $actual = $this->fixture
->GetTemplateDataForName('cache_test', filemtime($path));
70 $this->assertEquals($expected, $actual);
73 public function testCacheMiss()
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));
79 $actual = $this->fixture
->GetTemplateDataForName('cache_test', time() +
60);
80 $this->assertNull($actual);