* Get the correct column number in parsing exceptions
[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 | int %} 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
69 public function testExceptions()
70 {
71 try {
72 $catch = FALSE;
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);
78 $catch = TRUE;
79 }
80 $this->assertTrue($catch);
81
82 try {
83 $catch = FALSE;
84 $template = Template::NewWithData("Salve\n{% {%");
85 } catch (\hoplite\views\TemplateException $e) {
86 $message = $e->GetMessage();
87 $this->assertTrue(strpos($message, '2:4') !== FALSE);
88 $catch = TRUE;
89 }
90 $this->assertTrue($catch);
91
92 try {
93 $catch = FALSE;
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);
98 $catch = TRUE;
99 }
100 $this->assertTrue($catch);
101 }
102 }