. namespace hoplite\test; use hoplite\views\Template; require_once HOPLITE_ROOT . '/views/template.php'; class TemplateTest extends \PHPUnit_Framework_TestCase { private function _Render($template) { return $template->Render(); } public function testRenderSimple() { $template = Template::NewWithData('test', 'Hello World'); $this->assertEquals('Hello World', $this->_Render($template)); } public function testRender1Var() { $template = Template::NewWithData('test', 'Hello, {%= $name | str %}'); $template->name = 'Robert'; $this->assertEquals('Hello, Robert', $this->_Render($template)); } public function testRender2Vars() { $template = Template::NewWithData('test', 'Hello, {%= $name %}. Today is the {%= $date->day | int %} of July.'); $date = new \stdClass(); $date->day = 26; $template->name = 'Robert'; $template->date = $date; $this->assertEquals('Hello, Robert. Today is the 26 of July.', $this->_Render($template)); } public function testRenderIf() { $template = Template::NewWithData('test', 'You are {% if (!$user->logged_in): %}not logged in{% else: %}{%= $user->name %}{% endif %}'); $template->user = new \stdClass(); $template->user->logged_in = TRUE; $template->user->name = 'Robert'; $this->assertEquals('You are Robert', $this->_Render($template)); $template->user->logged_in = FALSE; $this->assertEquals('You are not logged in', $this->_Render($template)); } public function testExceptions() { try { $catch = FALSE; $template = Template::NewWithData('test', 'Hello %}'); } catch (\hoplite\views\TemplateException $e) { $message = $e->GetMessage(); // Check that the column number is correct. $this->assertTrue(strpos($message, '1:6') !== FALSE); $catch = TRUE; } $this->assertTrue($catch); try { $catch = FALSE; $template = Template::NewWithData('test', "Salve\n{% {%"); } catch (\hoplite\views\TemplateException $e) { $message = $e->GetMessage(); $this->assertTrue(strpos($message, '2:4') !== FALSE); $catch = TRUE; } $this->assertTrue($catch); try { $catch = FALSE; $template = Template::NewWithData('test', "Salve\n\n{%= \$name {%"); } catch (\hoplite\views\TemplateException $e) { $message = $e->GetMessage(); $this->assertTrue(strpos($message, '3:11') !== FALSE); $catch = TRUE; } $this->assertTrue($catch); } public function testRenderVars() { $template = Template::NewWithData('test', 'Some {%= $v %}'); $this->assertEquals('Some value', $template->Render(array('v' => 'value'))); $template->v = 'other'; $this->assertEquals('Some thing', $template->Render(array('v' => 'thing'))); $this->assertEquals('Some other', $template->Render()); } }