Allow an array of variables to be passed to Template::Render()
[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 return $template->Render();
27 }
28
29 public function testRenderSimple()
30 {
31 $template = Template::NewWithData('Hello World');
32 $this->assertEquals('Hello World', $this->_Render($template));
33 }
34
35 public function testRender1Var()
36 {
37 $template = Template::NewWithData('Hello, {% $name | str %}');
38 $template->name = 'Robert';
39 $this->assertEquals('Hello, Robert', $this->_Render($template));
40 }
41
42 public function testRender2Vars()
43 {
44 $template = Template::NewWithData('Hello, {% $name %}. Today is the {% $date->day | int %} of July.');
45 $date = new \stdClass();
46 $date->day = 26;
47 $template->name = 'Robert';
48 $template->date = $date;
49 $this->assertEquals('Hello, Robert. Today is the 26 of July.', $this->_Render($template));
50 }
51
52 public function testRenderIf()
53 {
54 $template = Template::NewWithData(
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));
60
61 $template->user->logged_in = FALSE;
62 $this->assertEquals('You are not logged in', $this->_Render($template));
63 }
64
65 public function testExceptions()
66 {
67 try {
68 $catch = FALSE;
69 $template = Template::NewWithData('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);
74 $catch = TRUE;
75 }
76 $this->assertTrue($catch);
77
78 try {
79 $catch = FALSE;
80 $template = Template::NewWithData("Salve\n{% {%");
81 } catch (\hoplite\views\TemplateException $e) {
82 $message = $e->GetMessage();
83 $this->assertTrue(strpos($message, '2:4') !== FALSE);
84 $catch = TRUE;
85 }
86 $this->assertTrue($catch);
87
88 try {
89 $catch = FALSE;
90 $template = Template::NewWithData("Salve\n\n{% \$name {!%");
91 } catch (\hoplite\views\TemplateException $e) {
92 $message = $e->GetMessage();
93 $this->assertTrue(strpos($message, '3:10') !== FALSE);
94 $catch = TRUE;
95 }
96 $this->assertTrue($catch);
97 }
98
99 public function testRenderVars()
100 {
101 $template = Template::NewWithData('Some {% $v %}');
102 $this->assertEquals('Some value', $template->Render(array('v' => 'value')));
103
104 $template->v = 'other';
105 $this->assertEquals('Some thing', $template->Render(array('v' => 'thing')));
106
107 $this->assertEquals('Some other', $template->Render());
108 }
109 }