Fix more bugs in TemplateLoader and a singleton helper
[hoplite.git] / testing / tests / views / template_loader_test.php
1 <?php
2 // Hoplite
3 // Copyright (c) 2011 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/template_loader.php';
21
22 class TestTemplateLoader extends views\TemplateLoader
23 {
24 public function T_CachePath($path)
25 {
26 return $this->_CachePath($path);
27 }
28
29 public function T_LoadIfCached($name)
30 {
31 return $this->_LoadIfCached($name);
32 }
33 }
34
35 class TemplateLoaderTest extends \PHPUnit_Framework_TestCase
36 {
37 public function setUp()
38 {
39 $this->fixture = new TestTemplateLoader();
40
41 $path = dirname(__FILE__) . '/cache/';
42 $files = scandir($path);
43 foreach ($files as $file)
44 if ($file[0] != '.')
45 unlink($path . $file);
46 }
47
48 protected function _SetUpPaths()
49 {
50 $this->fixture->set_template_path(dirname(__FILE__) . '/%s.tpl');
51 $this->fixture->set_cache_path(dirname(__FILE__) . '/cache/');
52 }
53
54 public function testTemplatePath()
55 {
56 $this->assertEquals('%s.tpl', $this->fixture->template_path());
57
58 $path = '/webapp/views/%s.tpl';
59 $this->fixture->set_template_path($path);
60 $this->assertEquals($path, $this->fixture->template_path());
61 }
62
63 public function testSetCachePath()
64 {
65 $this->assertEquals('/tmp/phalanx_views', $this->fixture->cache_path());
66
67 $path = '/cache/path';
68 $this->fixture->set_cache_path($path);
69 $this->assertEquals($path, $this->fixture->cache_path());
70 }
71
72 public function testCachePath()
73 {
74 $this->fixture->set_cache_path('/test/value/');
75 $this->assertEquals('/test/value/name.phpi', $this->fixture->T_CachePath('name'));
76 }
77
78 public function testCacheMiss()
79 {
80 $this->_SetUpPaths();
81
82 $files = scandir($this->fixture->cache_path());
83 $this->assertEquals(2, count($files)); // Only dotfiles.
84
85 $this->fixture->Load('cache_test');
86
87 $files = scandir($this->fixture->cache_path());
88 $this->assertEquals(3, count($files));
89
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);
93 }
94
95 public function testCacheHit()
96 {
97 $this->_SetUpPaths();
98
99 $expected = 'Cache hit!';
100 file_put_contents($this->fixture->cache_path() . '/cache_test.phpi', $expected);
101
102 $files = scandir($this->fixture->cache_path());
103 $this->assertEquals(3, count($files));
104
105 $this->fixture->Load('cache_test');
106
107 $files = scandir($this->fixture->cache_path());
108 $this->assertEquals(3, count($files));
109
110 $actual = file_get_contents($this->fixture->cache_path() . '/cache_test.phpi');
111 $this->assertEquals($expected, $actual);
112 }
113
114 public function testCacheInvalidate()
115 {
116 $this->_SetUpPaths();
117 file_put_contents($this->fixture->cache_path() . '/cache_test.phpi', 'Invalid template data');
118
119 // Need to wait for the mtime to make a difference.
120 sleep(1);
121 clearstatcache();
122
123 // Touch the template to update its mtime.
124 touch(sprintf($this->fixture->template_path(), 'cache_test'));
125
126 $files = scandir($this->fixture->cache_path());
127 $this->assertEquals(3, count($files));
128
129 $this->fixture->Load('cache_test');
130
131 $files = scandir($this->fixture->cache_path());
132 $this->assertEquals(3, count($files));
133
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);
137 }
138
139 public function testSingleton()
140 {
141 $fixture = views\TemplateLoader::GetInstance();
142 $this->assertNotSame($fixture, $this->fixture);
143
144 views\TemplateLoader::SetInstance($this->fixture);
145 $this->assertSame(views\TemplateLoader::GetInstance(), $this->fixture);
146
147 $this->_SetUpPaths();
148 $template = views\TemplateLoader::Fetch('cache_test');
149 $this->assertEquals('This file should be cached.', $template->template());
150 }
151 }