Fix more bugs in TemplateLoader and a singleton helper
authorRobert Sesek <rsesek@bluestatic.org>
Sat, 6 Aug 2011 21:34:45 +0000 (17:34 -0400)
committerRobert Sesek <rsesek@bluestatic.org>
Sat, 6 Aug 2011 21:34:45 +0000 (17:34 -0400)
testing/tests/views/template_loader_test.php
views/template_loader.php

index a602573e46f80007ec8f29c74acc6c160bf7338f..a7844933793de05de53694c2f884df63eb12680f 100644 (file)
@@ -47,8 +47,8 @@ class TemplateLoaderTest extends \PHPUnit_Framework_TestCase
 
   protected function _SetUpPaths()
   {
-    $this->fixture->set_template_path(dirname(__FILE__) . '/');
-    $this->fixture->set_cache_path($this->fixture->template_path() . 'cache/');
+    $this->fixture->set_template_path(dirname(__FILE__) . '/%s.tpl');
+    $this->fixture->set_cache_path(dirname(__FILE__) . '/cache/');
   }
 
   public function testTemplatePath()
@@ -135,4 +135,17 @@ class TemplateLoaderTest extends \PHPUnit_Framework_TestCase
     $actual   = file_get_contents($this->fixture->cache_path() . '/cache_test.phpi');
     $this->assertEquals($expected, $actual);
   }
+
+  public function testSingleton()
+  {
+    $fixture = views\TemplateLoader::GetInstance();
+    $this->assertNotSame($fixture, $this->fixture);
+
+    views\TemplateLoader::SetInstance($this->fixture);
+    $this->assertSame(views\TemplateLoader::GetInstance(), $this->fixture);
+
+    $this->_SetUpPaths();
+    $template = views\TemplateLoader::Fetch('cache_test');
+    $this->assertEquals('This file should be cached.', $template->template());
+  }
 }
index 11ae2963625d385d6e4285b4f3796edb70c0819d..65c59b5ea3e4de371e1042079ad12e528fd8d5a7 100644 (file)
@@ -51,7 +51,7 @@ class TemplateLoader
       $class = get_called_class();
       self::$instance = new $class();
     }
-    return self::$instance();
+    return self::$instance;
   }
   /*! Sets the singleton instance. */
   static public function SetInstance($instance) { self::$instance = $instance; }
@@ -90,6 +90,12 @@ class TemplateLoader
     return clone $template;
   }
 
+  /*! Convenience function for loading templates. */
+  static public function Fetch($name)
+  {
+    return self::GetInstance()->Load($name);
+  }
+
   /*!
     Loads a cached filesystem template if it is up-to-date.
 
@@ -129,7 +135,7 @@ class TemplateLoader
 
     $data = @file_get_contents($tpl_path);
     if ($data === FALSE)
-      throw new TemplateLoaderException('Could not load template ' . $this->template_name);
+      throw new TemplateLoaderException('Could not load template ' . $name);
 
     $template = Template::NewWithData($data);