3 // Copyright (c) 2011 Blue Static
5 // This program is free software: you can redistribute it and/or modify it
6 // under the terms of the GNU General Public License as published by the Free
7 // Software Foundation, either version 3 of the License, or any later version.
9 // This program is distributed in the hope that it will be useful, but WITHOUT
10 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 // You should have received a copy of the GNU General Public License along with
15 // this program. If not, see <http://www.gnu.org/licenses/>.
17 namespace hoplite
\test
;
18 use hoplite
\views\Template
;
20 require_once HOPLITE_ROOT
. '/views/template.php';
22 class TemplateTest
extends \PHPUnit_Framework_TestCase
24 private function _Render($template)
28 $data = ob_get_contents();
33 public function testRenderSimple()
35 $template = Template
::NewWithData('Hello World');
36 $this->assertEquals('Hello World', $this->_Render($template));
39 public function testRender1Var()
41 $template = Template
::NewWithData('Hello, {% $name | str %}');
42 $template->name
= 'Robert';
43 $this->assertEquals('Hello, Robert', $this->_Render($template));
46 public function testRender2Vars()
48 $template = Template
::NewWithData('Hello, {% $name %}. Today is the {% $date->day | int %} of July.');
49 $date = new \
stdClass();
51 $template->name
= 'Robert';
52 $template->date
= $date;
53 $this->assertEquals('Hello, Robert. Today is the 26 of July.', $this->_Render($template));
56 public function testRenderIf()
58 $template = Template
::NewWithData(
59 'You are {!% if (!$user->logged_in): %}not logged in{!% else: %}{% $user->name %}{!% endif %}');
60 $template->user
= new \
stdClass();
61 $template->user
->logged_in
= TRUE
;
62 $template->user
->name
= 'Robert';
63 $this->assertEquals('You are Robert', $this->_Render($template));
65 $template->user
->logged_in
= FALSE
;
66 $this->assertEquals('You are not logged in', $this->_Render($template));
69 public function testExceptions()
73 $template = Template
::NewWithData('Hello %}');
74 } catch (\hoplite
\views\TemplateException
$e) {
75 $message = $e->GetMessage();
76 // Check that the column number is correct.
77 $this->assertTrue(strpos($message, '1:6') !== FALSE
);
80 $this->assertTrue($catch);
84 $template = Template
::NewWithData("Salve\n{% {%");
85 } catch (\hoplite
\views\TemplateException
$e) {
86 $message = $e->GetMessage();
87 $this->assertTrue(strpos($message, '2:4') !== FALSE
);
90 $this->assertTrue($catch);
94 $template = Template
::NewWithData("Salve\n\n{% \$name {!%");
95 } catch (\hoplite
\views\TemplateException
$e) {
96 $message = $e->GetMessage();
97 $this->assertTrue(strpos($message, '3:10') !== FALSE
);
100 $this->assertTrue($catch);