From 96f02f8769970b0f4e373d32e2537dbf2a4fb7cc Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Sat, 6 Aug 2011 17:02:31 -0400 Subject: [PATCH] Get TemplateLoader working and update the tests --- testing/tests/views/cache/.gitignore | 1 + testing/tests/views/cache_test.tpl | 1 + testing/tests/views/template_loader_test.php | 154 ++++++------------- views/template.php | 2 +- views/template_loader.php | 24 +-- 5 files changed, 67 insertions(+), 115 deletions(-) create mode 100644 testing/tests/views/cache/.gitignore create mode 100644 testing/tests/views/cache_test.tpl diff --git a/testing/tests/views/cache/.gitignore b/testing/tests/views/cache/.gitignore new file mode 100644 index 0000000..28bf811 --- /dev/null +++ b/testing/tests/views/cache/.gitignore @@ -0,0 +1 @@ +*.phpi \ No newline at end of file diff --git a/testing/tests/views/cache_test.tpl b/testing/tests/views/cache_test.tpl new file mode 100644 index 0000000..647b29c --- /dev/null +++ b/testing/tests/views/cache_test.tpl @@ -0,0 +1 @@ +This file should be cached. \ No newline at end of file diff --git a/testing/tests/views/template_loader_test.php b/testing/tests/views/template_loader_test.php index 0844696..a602573 100644 --- a/testing/tests/views/template_loader_test.php +++ b/testing/tests/views/template_loader_test.php @@ -13,180 +13,126 @@ // // You should have received a copy of the GNU General Public License along with // this program. If not, see . -/* + namespace hoplite\test; -use hoplite\views\Template; +use hoplite\views as views; -require_once TEST_ROOT . '/tests/views.php'; +require_once HOPLITE_ROOT . '/views/template_loader.php'; -class ViewTest extends \PHPUnit_Framework_TestCase +class TestTemplateLoader extends views\TemplateLoader { - public $saved_singleton = array(); + public function T_CachePath($path) + { + return $this->_CachePath($path); + } + + public function T_LoadIfCached($name) + { + return $this->_LoadIfCached($name); + } +} +class TemplateLoaderTest extends \PHPUnit_Framework_TestCase +{ public function setUp() { - $this->saved_singleton['template_path'] = View::template_path(); - $this->saved_singleton['cache_path'] = View::cache_path(); + $this->fixture = new TestTemplateLoader(); - $path = dirname(__FILE__) . '/data/cache/'; + $path = dirname(__FILE__) . '/cache/'; $files = scandir($path); foreach ($files as $file) if ($file[0] != '.') unlink($path . $file); } - public function tearDown() + protected function _SetUpPaths() { - View::set_template_path($this->saved_singleton['template_path']); - View::set_cache_path($this->saved_singleton['cache_path']); + $this->fixture->set_template_path(dirname(__FILE__) . '/'); + $this->fixture->set_cache_path($this->fixture->template_path() . 'cache/'); } public function testTemplatePath() { - $this->assertEquals('%s.tpl', View::template_path()); + $this->assertEquals('%s.tpl', $this->fixture->template_path()); $path = '/webapp/views/%s.tpl'; - View::set_template_path($path); - $this->assertEquals($path, View::template_path()); + $this->fixture->set_template_path($path); + $this->assertEquals($path, $this->fixture->template_path()); } public function testSetCachePath() { - $this->assertEquals('/tmp/phalanx_views', View::cache_path()); + $this->assertEquals('/tmp/phalanx_views', $this->fixture->cache_path()); $path = '/cache/path'; - View::set_cache_path($path); - $this->assertEquals($path, View::cache_path()); - } - - public function testCtorAndTemplateName() - { - $view = $this->getMock('phalanx\views\View', NULL, array('test_tpl')); - $this->assertEquals('test_tpl', $view->template_name()); - - $this->assertType('phalanx\base\Dictionary', $view->vars()); - $this->assertEquals(0, $view->vars()->Count()); - } - - public function testProcessTemplateEntities() - { - $view = new TestView('test'); - $data = 'Some day, is it not, <'.'?php echo "Rob" ?'.'>?'; - $this->assertEquals($data, $view->T_ProcessTemplate($data)); - } - - public function testProcessTemplateMacro() - { - $view = new TestView('test'); - $in = 'foo $[some.value] bar'; - $out = 'foo GetHTML("some.value") ?> bar'; - $this->assertEquals($out, $view->T_ProcessTemplate($in)); - } - - public function testProcessTemplateShortTags() - { - $view = new TestView('test'); - $in = 'foo bar moo'; - $out = 'foo bar moo'; - $this->assertEquals($out, $view->T_ProcessTemplate($in)); - } - - public function testMagicGetterSetter() - { - $view = new View('test'); - $view->foo = 'abc'; - $this->assertEquals('abc', $view->foo); - $this->assertEquals('abc', $view->vars()->Get('foo')); - - $view->foo = array(); - $view->{"foo.bar"} = '123'; - $this->assertEquals('123', $view->{"foo.bar"}); - $this->assertEquals('123', $view->vars()->Get('foo.bar')); + $this->fixture->set_cache_path($path); + $this->assertEquals($path, $this->fixture->cache_path()); } public function testCachePath() { - View::set_cache_path('/test/value'); - $view = new TestView('name'); - $this->assertEquals('/test/value/name.phpi', $view->T_CachePath($view->template_name())); + $this->fixture->set_cache_path('/test/value/'); + $this->assertEquals('/test/value/name.phpi', $this->fixture->T_CachePath('name')); } public function testCacheMiss() { - TestView::SetupPaths(); - $view = new TestView('cache_test'); + $this->_SetUpPaths(); - $files = scandir(View::cache_path()); + $files = scandir($this->fixture->cache_path()); $this->assertEquals(2, count($files)); // Only dotfiles. - $view->T_Cache(); + $this->fixture->Load('cache_test'); - $files = scandir(View::cache_path()); + $files = scandir($this->fixture->cache_path()); $this->assertEquals(3, count($files)); - $expected = file_get_contents(sprintf(View::template_path(), 'cache_test')); - $actual = file_get_contents(View::cache_path() . '/cache_test.phpi'); + $expected = file_get_contents(sprintf($this->fixture->template_path(), 'cache_test')); + $actual = file_get_contents($this->fixture->cache_path() . '/cache_test.phpi'); $this->assertEquals($expected, $actual); } public function testCacheHit() { + $this->_SetUpPaths(); + $expected = 'Cache hit!'; - TestView::SetupPaths(); - file_put_contents(View::cache_path() . '/cache_test.phpi', $expected); - $view = new TestView('cache_test'); + file_put_contents($this->fixture->cache_path() . '/cache_test.phpi', $expected); - $files = scandir(View::cache_path()); + $files = scandir($this->fixture->cache_path()); $this->assertEquals(3, count($files)); - $view->T_Cache(); + $this->fixture->Load('cache_test'); - $files = scandir(View::cache_path()); + $files = scandir($this->fixture->cache_path()); $this->assertEquals(3, count($files)); - $actual = file_get_contents(View::cache_path() . '/cache_test.phpi'); + $actual = file_get_contents($this->fixture->cache_path() . '/cache_test.phpi'); $this->assertEquals($expected, $actual); } public function testCacheInvalidate() { - TestView::SetupPaths(); - file_put_contents(View::cache_path() . '/cache_test.phpi', 'Invalid template data'); - $view = new TestView('cache_test'); + $this->_SetUpPaths(); + file_put_contents($this->fixture->cache_path() . '/cache_test.phpi', 'Invalid template data'); // Need to wait for the mtime to make a difference. sleep(1); clearstatcache(); // Touch the template to update its mtime. - touch(sprintf(View::template_path(), 'cache_test')); + touch(sprintf($this->fixture->template_path(), 'cache_test')); - $files = scandir(View::cache_path()); + $files = scandir($this->fixture->cache_path()); $this->assertEquals(3, count($files)); - $view->T_Cache(); + $this->fixture->Load('cache_test'); - $files = scandir(View::cache_path()); + $files = scandir($this->fixture->cache_path()); $this->assertEquals(3, count($files)); - $expected = file_get_contents(sprintf(View::template_path(), 'cache_test')); - $actual = file_get_contents(View::cache_path() . '/cache_test.phpi'); + $expected = file_get_contents(sprintf($this->fixture->template_path(), 'cache_test')); + $actual = file_get_contents($this->fixture->cache_path() . '/cache_test.phpi'); $this->assertEquals($expected, $actual); } - - public function testRender() - { - TestView::SetupPaths(); - - $view = new TestView('render_test'); - $view->name = 'Rob'; - - ob_start(); - $view->Render(); - $actual = ob_get_contents(); - ob_end_clean(); - - $this->assertEquals('Hi, my name is Rob. This is undefined: .', $actual); - } } -*/ \ No newline at end of file diff --git a/views/template.php b/views/template.php index 12b69ef..f43e099 100644 --- a/views/template.php +++ b/views/template.php @@ -51,7 +51,7 @@ class Template return $template; } - static public function NewFromCompiledData($data) + static public function NewWithCompiledData($data) { $template = new Template(''); $template->data = $data; diff --git a/views/template_loader.php b/views/template_loader.php index 6a9da4d..11ae296 100644 --- a/views/template_loader.php +++ b/views/template_loader.php @@ -47,19 +47,21 @@ class TemplateLoader /*! Gets the singleton instance. */ static public function GetInstance() { - if (!self::$instance) - self::$instance = new __class__(); + if (!self::$instance) { + $class = get_called_class(); + self::$instance = new $class(); + } return self::$instance(); } /*! Sets the singleton instance. */ static public function SetInstance($instance) { self::$instance = $instance; } /*! Accessors */ - public function set_template_path($path) { $this->$template_path = $path; } - function template_path() { return $this->$template_path; } + public function set_template_path($path) { $this->template_path = $path; } + function template_path() { return $this->template_path; } - public function set_cache_path($path) { $this->$cache_path = $path; } - public function cache_path() { return $this->$cache_path; } + public function set_cache_path($path) { $this->cache_path = $path; } + public function cache_path() { return $this->cache_path; } /*! Loads a template from a file, creates a Template object, and returns a copy @@ -101,7 +103,7 @@ class TemplateLoader $tpl_path = $this->_TemplatePath($name); // Make sure the cached file exists and hasn't gotten out-of-date. - if (!file_exists($cache_path) || filemtime($cache_path) < filemtime($tpl_path)) { + if (!file_exists($cache_path) || filemtime($cache_path) < filemtime($tpl_path)) return NULL; // Load the contents of the cache. @@ -132,8 +134,10 @@ class TemplateLoader $template = Template::NewWithData($data); // Cache the file. - if (!file_put_contents($cache_path, $this->cache_prefix . $template->template())) - throw new TemplateLoaderException('Could not cache ' . $this->template_name . ' to ' . $cache_path); + if (file_put_contents($cache_path, $this->cache_prefix . $template->template()) === FALSE) + throw new TemplateLoaderException('Could not cache ' . $name . ' to ' . $cache_path); + + return $template; } /*! Returns the template path for a given template name. */ @@ -145,7 +149,7 @@ class TemplateLoader /*! Returns the cache path for a given template name. */ protected function _CachePath($name) { - return self::$cache_path . '/' . $name . '.phpi'; + return $this->cache_path . $name . '.phpi'; } } -- 2.43.5