Add basic template parser and test
[hoplite.git] / testing / tests / views / template_test.php
1 <?php
2 // Hoplite
3 // Copyright (c) 2011 Blue Static
4 //
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.
8 //
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
12 // more details.
13 //
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/>.
16
17 namespace hoplite\test;
18 use hoplite\views\Template;
19
20 require_once HOPLITE_ROOT . '/views/template.php';
21
22 class TemplateTest extends \PHPUnit_Framework_TestCase
23 {
24 private function _Render($template)
25 {
26 ob_start();
27 $template->Render();
28 $data = ob_get_contents();
29 ob_end_clean();
30 return $data;
31 }
32
33 public function testRenderSimple()
34 {
35 $template = Template::NewWithData('Hello World');
36 $this->assertEquals('Hello World', $this->_Render($template));
37 }
38
39 public function testRender1Var()
40 {
41 $template = Template::NewWithData('Hello, {% $name | str %}');
42 $template->name = 'Robert';
43 $this->assertEquals('Hello, Robert', $this->_Render($template));
44 }
45
46 public function testRender2Vars()
47 {
48 $template = Template::NewWithData('Hello, {% $name %}. Today is the {% $date->day %} of July.');
49 $date = new \stdClass();
50 $date->day = 26;
51 $template->name = 'Robert';
52 $template->date = $date;
53 $this->assertEquals('Hello, Robert. Today is the 26 of July.', $this->_Render($template));
54 }
55
56 public function testRenderIf()
57 {
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));
64
65 $template->user->logged_in = FALSE;
66 $this->assertEquals('You are not logged in', $this->_Render($template));
67 }
68 }