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)
26 return $template->Render();
29 public function testRenderSimple()
31 $template = Template
::NewWithData('test', 'Hello World');
32 $this->assertEquals('Hello World', $this->_Render($template));
35 public function testRender1Var()
37 $template = Template
::NewWithData('test', 'Hello, {% $name | str %}');
38 $template->name
= 'Robert';
39 $this->assertEquals('Hello, Robert', $this->_Render($template));
42 public function testRender2Vars()
44 $template = Template
::NewWithData('test', 'Hello, {% $name %}. Today is the {% $date->day | int %} of July.');
45 $date = new \
stdClass();
47 $template->name
= 'Robert';
48 $template->date
= $date;
49 $this->assertEquals('Hello, Robert. Today is the 26 of July.', $this->_Render($template));
52 public function testRenderIf()
54 $template = Template
::NewWithData('test',
55 'You are {!% if (!$user->logged_in): %}not logged in{!% else: %}{% $user->name %}{!% endif %}');
56 $template->user
= new \
stdClass();
57 $template->user
->logged_in
= TRUE
;
58 $template->user
->name
= 'Robert';
59 $this->assertEquals('You are Robert', $this->_Render($template));
61 $template->user
->logged_in
= FALSE
;
62 $this->assertEquals('You are not logged in', $this->_Render($template));
65 public function testExceptions()
69 $template = Template
::NewWithData('test', 'Hello %}');
70 } catch (\hoplite
\views\TemplateException
$e) {
71 $message = $e->GetMessage();
72 // Check that the column number is correct.
73 $this->assertTrue(strpos($message, '1:6') !== FALSE
);
76 $this->assertTrue($catch);
80 $template = Template
::NewWithData('test', "Salve\n{% {%");
81 } catch (\hoplite
\views\TemplateException
$e) {
82 $message = $e->GetMessage();
83 $this->assertTrue(strpos($message, '2:4') !== FALSE
);
86 $this->assertTrue($catch);
90 $template = Template
::NewWithData('test', "Salve\n\n{% \$name {!%");
91 } catch (\hoplite
\views\TemplateException
$e) {
92 $message = $e->GetMessage();
93 $this->assertTrue(strpos($message, '3:10') !== FALSE
);
96 $this->assertTrue($catch);
99 public function testRenderVars()
101 $template = Template
::NewWithData('test', 'Some {% $v %}');
102 $this->assertEquals('Some value', $template->Render(array('v' => 'value')));
104 $template->v
= 'other';
105 $this->assertEquals('Some thing', $template->Render(array('v' => 'thing')));
107 $this->assertEquals('Some other', $template->Render());