3 // Copyright (c) 2011 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/template_loader.php';
22 class TestTemplateLoader
extends views\TemplateLoader
24 public function T_CachePath($path)
26 return $this->_CachePath($path);
29 public function T_LoadIfCached($name)
31 return $this->_LoadIfCached($name);
35 class TemplateLoaderTest
extends \PHPUnit_Framework_TestCase
37 public function setUp()
39 $this->fixture
= new TestTemplateLoader();
41 $path = dirname(__FILE__
) . '/cache/';
42 $files = scandir($path);
43 foreach ($files as $file)
45 unlink($path . $file);
48 protected function _SetUpPaths()
50 $this->fixture
->set_template_path(dirname(__FILE__
) . '/%s.tpl');
51 $this->fixture
->set_cache_path(dirname(__FILE__
) . '/cache/');
54 public function testTemplatePath()
56 $this->assertEquals('%s.tpl', $this->fixture
->template_path());
58 $path = '/webapp/views/%s.tpl';
59 $this->fixture
->set_template_path($path);
60 $this->assertEquals($path, $this->fixture
->template_path());
63 public function testSetCachePath()
65 $this->assertEquals('/tmp/phalanx_views', $this->fixture
->cache_path());
67 $path = '/cache/path';
68 $this->fixture
->set_cache_path($path);
69 $this->assertEquals($path, $this->fixture
->cache_path());
72 public function testCachePath()
74 $this->fixture
->set_cache_path('/test/value/');
75 $this->assertEquals('/test/value/name.phpi', $this->fixture
->T_CachePath('name'));
78 public function testCacheMiss()
82 $files = scandir($this->fixture
->cache_path());
83 $this->assertEquals(2, count($files)); // Only dotfiles.
85 $this->fixture
->Load('cache_test');
87 $files = scandir($this->fixture
->cache_path());
88 $this->assertEquals(3, count($files));
90 $expected = file_get_contents(sprintf($this->fixture
->template_path(), 'cache_test'));
91 $actual = file_get_contents($this->fixture
->cache_path() . '/cache_test.phpi');
92 $this->assertEquals($expected, $actual);
95 public function testCacheHit()
99 $expected = 'Cache hit!';
100 file_put_contents($this->fixture
->cache_path() . '/cache_test.phpi', $expected);
102 $files = scandir($this->fixture
->cache_path());
103 $this->assertEquals(3, count($files));
105 $this->fixture
->Load('cache_test');
107 $files = scandir($this->fixture
->cache_path());
108 $this->assertEquals(3, count($files));
110 $actual = file_get_contents($this->fixture
->cache_path() . '/cache_test.phpi');
111 $this->assertEquals($expected, $actual);
114 public function testCacheInvalidate()
116 $this->_SetUpPaths();
117 file_put_contents($this->fixture
->cache_path() . '/cache_test.phpi', 'Invalid template data');
119 // Need to wait for the mtime to make a difference.
123 // Touch the template to update its mtime.
124 touch(sprintf($this->fixture
->template_path(), 'cache_test'));
126 $files = scandir($this->fixture
->cache_path());
127 $this->assertEquals(3, count($files));
129 $this->fixture
->Load('cache_test');
131 $files = scandir($this->fixture
->cache_path());
132 $this->assertEquals(3, count($files));
134 $expected = file_get_contents(sprintf($this->fixture
->template_path(), 'cache_test'));
135 $actual = file_get_contents($this->fixture
->cache_path() . '/cache_test.phpi');
136 $this->assertEquals($expected, $actual);
139 public function testSingleton()
141 $fixture = views\TemplateLoader
::GetInstance();
142 $this->assertNotSame($fixture, $this->fixture
);
144 views\TemplateLoader
::SetInstance($this->fixture
);
145 $this->assertSame(views\TemplateLoader
::GetInstance(), $this->fixture
);
147 $this->_SetUpPaths();
148 $template = views\TemplateLoader
::Fetch('cache_test');
149 $this->assertEquals('This file should be cached.', $template->template());