Get TemplateLoader working and update the tests
authorRobert Sesek <rsesek@bluestatic.org>
Sat, 6 Aug 2011 21:02:31 +0000 (17:02 -0400)
committerRobert Sesek <rsesek@bluestatic.org>
Sat, 6 Aug 2011 21:02:31 +0000 (17:02 -0400)
testing/tests/views/cache/.gitignore [new file with mode: 0644]
testing/tests/views/cache_test.tpl [new file with mode: 0644]
testing/tests/views/template_loader_test.php
views/template.php
views/template_loader.php

diff --git a/testing/tests/views/cache/.gitignore b/testing/tests/views/cache/.gitignore
new file mode 100644 (file)
index 0000000..28bf811
--- /dev/null
@@ -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 (file)
index 0000000..647b29c
--- /dev/null
@@ -0,0 +1 @@
+This file should be cached.
\ No newline at end of file
index 0844696c33e1b2658d5c28d450ef4e3f1b3eb54a..a602573e46f80007ec8f29c74acc6c160bf7338f 100644 (file)
 //
 // You should have received a copy of the GNU General Public License along with
 // this program.  If not, see <http://www.gnu.org/licenses/>.
-/*
+
 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 = '<strong>Some day, is it not, <'.'?php echo "Rob" ?'.'>?</strong>';
-    $this->assertEquals($data, $view->T_ProcessTemplate($data));
-  }
-
-  public function testProcessTemplateMacro()
-  {
-    $view = new TestView('test');
-    $in   = 'foo $[some.value] bar';
-    $out  = 'foo <?php echo $view->GetHTML("some.value") ?> bar';
-    $this->assertEquals($out, $view->T_ProcessTemplate($in));
-  }
-
-  public function testProcessTemplateShortTags()
-  {
-    $view = new TestView('test');
-    $in   = 'foo <?php echo "Not this one"; ?> bar <? echo "But this one!" ?> moo';
-    $out  = 'foo <?php echo "Not this one"; ?> bar <?php echo "But this one!" ?> 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
index 12b69efb5f6588d878dfbc874c22c78e0e5e3067..f43e0997f94aa94e935e261a6513e0d021025b85 100644 (file)
@@ -51,7 +51,7 @@ class Template
     return $template;
   }
 
-  static public function NewFromCompiledData($data)
+  static public function NewWithCompiledData($data)
   {
     $template = new Template('');
     $template->data = $data;
index 6a9da4d4c7a8607edfa9ac88406ee573add3e82e..11ae2963625d385d6e4285b4f3796edb70c0819d 100644 (file)
@@ -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';
   }
 }