]>
src.bluestatic.org Git - hoplite.git/blob - testing/tests/views/template_loader_test.php
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\Template;
20 require_once TEST_ROOT . '/tests/views.php';
22 class ViewTest extends \PHPUnit_Framework_TestCase
24 public $saved_singleton = array();
26 public function setUp()
28 $this->saved_singleton['template_path'] = View::template_path();
29 $this->saved_singleton['cache_path'] = View::cache_path();
31 $path = dirname(__FILE__) . '/data/cache/';
32 $files = scandir($path);
33 foreach ($files as $file)
35 unlink($path . $file);
38 public function tearDown()
40 View::set_template_path($this->saved_singleton['template_path']);
41 View::set_cache_path($this->saved_singleton['cache_path']);
44 public function testTemplatePath()
46 $this->assertEquals('%s.tpl', View::template_path());
48 $path = '/webapp/views/%s.tpl';
49 View::set_template_path($path);
50 $this->assertEquals($path, View::template_path());
53 public function testSetCachePath()
55 $this->assertEquals('/tmp/phalanx_views', View::cache_path());
57 $path = '/cache/path';
58 View::set_cache_path($path);
59 $this->assertEquals($path, View::cache_path());
62 public function testCtorAndTemplateName()
64 $view = $this->getMock('phalanx\views\View', NULL, array('test_tpl'));
65 $this->assertEquals('test_tpl', $view->template_name());
67 $this->assertType('phalanx\base\Dictionary', $view->vars());
68 $this->assertEquals(0, $view->vars()->Count());
71 public function testProcessTemplateEntities()
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));
78 public function testProcessTemplateMacro()
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));
86 public function testProcessTemplateShortTags()
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));
94 public function testMagicGetterSetter()
96 $view = new View('test');
98 $this->assertEquals('abc', $view->foo);
99 $this->assertEquals('abc', $view->vars()->Get('foo'));
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'));
107 public function testCachePath()
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()));
114 public function testCacheMiss()
116 TestView::SetupPaths();
117 $view = new TestView('cache_test');
119 $files = scandir(View::cache_path());
120 $this->assertEquals(2, count($files)); // Only dotfiles.
124 $files = scandir(View::cache_path());
125 $this->assertEquals(3, count($files));
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);
132 public function testCacheHit()
134 $expected = 'Cache hit!';
135 TestView::SetupPaths();
136 file_put_contents(View::cache_path() . '/cache_test.phpi', $expected);
137 $view = new TestView('cache_test');
139 $files = scandir(View::cache_path());
140 $this->assertEquals(3, count($files));
144 $files = scandir(View::cache_path());
145 $this->assertEquals(3, count($files));
147 $actual = file_get_contents(View::cache_path() . '/cache_test.phpi');
148 $this->assertEquals($expected, $actual);
151 public function testCacheInvalidate()
153 TestView::SetupPaths();
154 file_put_contents(View::cache_path() . '/cache_test.phpi', 'Invalid template data');
155 $view = new TestView('cache_test');
157 // Need to wait for the mtime to make a difference.
161 // Touch the template to update its mtime.
162 touch(sprintf(View::template_path(), 'cache_test'));
164 $files = scandir(View::cache_path());
165 $this->assertEquals(3, count($files));
169 $files = scandir(View::cache_path());
170 $this->assertEquals(3, count($files));
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);
177 public function testRender()
179 TestView::SetupPaths();
181 $view = new TestView('render_test');
186 $actual = ob_get_contents();
189 $this->assertEquals('Hi, my name is Rob. This is undefined: .', $actual);