//
// 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
/*! 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
$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.
$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. */
/*! 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';
}
}