Add WIP TemplateLoader
[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\Template;
19
20 require_once TEST_ROOT . '/tests/views.php';
21
22 class ViewTest extends \PHPUnit_Framework_TestCase
23 {
24 public $saved_singleton = array();
25
26 public function setUp()
27 {
28 $this->saved_singleton['template_path'] = View::template_path();
29 $this->saved_singleton['cache_path'] = View::cache_path();
30
31 $path = dirname(__FILE__) . '/data/cache/';
32 $files = scandir($path);
33 foreach ($files as $file)
34 if ($file[0] != '.')
35 unlink($path . $file);
36 }
37
38 public function tearDown()
39 {
40 View::set_template_path($this->saved_singleton['template_path']);
41 View::set_cache_path($this->saved_singleton['cache_path']);
42 }
43
44 public function testTemplatePath()
45 {
46 $this->assertEquals('%s.tpl', View::template_path());
47
48 $path = '/webapp/views/%s.tpl';
49 View::set_template_path($path);
50 $this->assertEquals($path, View::template_path());
51 }
52
53 public function testSetCachePath()
54 {
55 $this->assertEquals('/tmp/phalanx_views', View::cache_path());
56
57 $path = '/cache/path';
58 View::set_cache_path($path);
59 $this->assertEquals($path, View::cache_path());
60 }
61
62 public function testCtorAndTemplateName()
63 {
64 $view = $this->getMock('phalanx\views\View', NULL, array('test_tpl'));
65 $this->assertEquals('test_tpl', $view->template_name());
66
67 $this->assertType('phalanx\base\Dictionary', $view->vars());
68 $this->assertEquals(0, $view->vars()->Count());
69 }
70
71 public function testProcessTemplateEntities()
72 {
73 $view = new TestView('test');
74 $data = '<strong>Some day, is it not, <'.'?php echo "Rob" ?'.'>?</strong>';
75 $this->assertEquals($data, $view->T_ProcessTemplate($data));
76 }
77
78 public function testProcessTemplateMacro()
79 {
80 $view = new TestView('test');
81 $in = 'foo $[some.value] bar';
82 $out = 'foo <?php echo $view->GetHTML("some.value") ?> bar';
83 $this->assertEquals($out, $view->T_ProcessTemplate($in));
84 }
85
86 public function testProcessTemplateShortTags()
87 {
88 $view = new TestView('test');
89 $in = 'foo <?php echo "Not this one"; ?> bar <? echo "But this one!" ?> moo';
90 $out = 'foo <?php echo "Not this one"; ?> bar <?php echo "But this one!" ?> moo';
91 $this->assertEquals($out, $view->T_ProcessTemplate($in));
92 }
93
94 public function testMagicGetterSetter()
95 {
96 $view = new View('test');
97 $view->foo = 'abc';
98 $this->assertEquals('abc', $view->foo);
99 $this->assertEquals('abc', $view->vars()->Get('foo'));
100
101 $view->foo = array();
102 $view->{"foo.bar"} = '123';
103 $this->assertEquals('123', $view->{"foo.bar"});
104 $this->assertEquals('123', $view->vars()->Get('foo.bar'));
105 }
106
107 public function testCachePath()
108 {
109 View::set_cache_path('/test/value');
110 $view = new TestView('name');
111 $this->assertEquals('/test/value/name.phpi', $view->T_CachePath($view->template_name()));
112 }
113
114 public function testCacheMiss()
115 {
116 TestView::SetupPaths();
117 $view = new TestView('cache_test');
118
119 $files = scandir(View::cache_path());
120 $this->assertEquals(2, count($files)); // Only dotfiles.
121
122 $view->T_Cache();
123
124 $files = scandir(View::cache_path());
125 $this->assertEquals(3, count($files));
126
127 $expected = file_get_contents(sprintf(View::template_path(), 'cache_test'));
128 $actual = file_get_contents(View::cache_path() . '/cache_test.phpi');
129 $this->assertEquals($expected, $actual);
130 }
131
132 public function testCacheHit()
133 {
134 $expected = 'Cache hit!';
135 TestView::SetupPaths();
136 file_put_contents(View::cache_path() . '/cache_test.phpi', $expected);
137 $view = new TestView('cache_test');
138
139 $files = scandir(View::cache_path());
140 $this->assertEquals(3, count($files));
141
142 $view->T_Cache();
143
144 $files = scandir(View::cache_path());
145 $this->assertEquals(3, count($files));
146
147 $actual = file_get_contents(View::cache_path() . '/cache_test.phpi');
148 $this->assertEquals($expected, $actual);
149 }
150
151 public function testCacheInvalidate()
152 {
153 TestView::SetupPaths();
154 file_put_contents(View::cache_path() . '/cache_test.phpi', 'Invalid template data');
155 $view = new TestView('cache_test');
156
157 // Need to wait for the mtime to make a difference.
158 sleep(1);
159 clearstatcache();
160
161 // Touch the template to update its mtime.
162 touch(sprintf(View::template_path(), 'cache_test'));
163
164 $files = scandir(View::cache_path());
165 $this->assertEquals(3, count($files));
166
167 $view->T_Cache();
168
169 $files = scandir(View::cache_path());
170 $this->assertEquals(3, count($files));
171
172 $expected = file_get_contents(sprintf(View::template_path(), 'cache_test'));
173 $actual = file_get_contents(View::cache_path() . '/cache_test.phpi');
174 $this->assertEquals($expected, $actual);
175 }
176
177 public function testRender()
178 {
179 TestView::SetupPaths();
180
181 $view = new TestView('render_test');
182 $view->name = 'Rob';
183
184 ob_start();
185 $view->Render();
186 $actual = ob_get_contents();
187 ob_end_clean();
188
189 $this->assertEquals('Hi, my name is Rob. This is undefined: .', $actual);
190 }
191 }
192 */