+ Moved variables resolution in the compiler Parser. (Temp. solution until the new...
[haanga.git] / tests / templateTest.php
blob11bc74e258f702caceaa7c5a424cee68041711b4
1 <?php
3 class Foo_Bar {
4 static $Bar = 'haanga';
5 static $Arr = array('foo', 'Bar' => 'Foo');
7 static function something()
9 return 'something';
13 /**
14 * @runTestsInSeparateProcess
16 class templateTest extends PHPUnit_Framework_TestCase
18 public function testInit()
20 /* setup */
21 @mkdir("tmp/");
22 foreach (glob("tmp/*/*") as $file) {
23 @unlink($file);
25 TestSuite::init();
28 public function init($test_file, &$expected)
30 if ($test_file == 'assert_templates/strip_whitespace.tpl') {
31 Haanga_Compiler::setOption('strip_whitespace', TRUE);
32 $expected = rtrim($expected). ' '; /* weird output */
33 } else {
34 Haanga_Compiler::setOption('strip_whitespace', FALSE);
37 /**
38 * @dataProvider tplProvider
40 public function testRuntime($test_file, $data, $expected)
42 $this->init($test_file, $expected);
43 $output = Haanga::Load($test_file, $data, TRUE);
44 $this->assertEquals($output, $expected);
47 /**
48 * @dataProvider tplProvider
50 public function testLambda($test_file, $data, $expected)
52 $this->init($test_file, $expected);
53 $callback = Haanga::compile(file_get_contents($test_file), $data);
54 $output = $callback($data);
55 $this->assertEquals($output, $expected);
59 /**
60 * @dataProvider tplProvider
62 public function testIsCached($test_file, $data, $expected)
64 /* same as above, but we ensure that the file wasn't compiled */
65 $this->init($test_file, $expected);
66 $output = Haanga::Load($test_file, $data, TRUE);
67 $this->assertEquals($output, $expected);
68 $this->assertFalse(Haanga::$has_compiled);
71 /**
72 * @dataProvider tplProvider
73 * /
74 public function testCLICompiler($test_file, $data, $expected)
76 TestSuite::init();
77 $GLOBALS['argv'][1] = $test_file;
78 $GLOBALS['argv'][2] = '--notags';
80 ob_start();
81 Haanga_Compiler::main_cli();
82 $code = ob_get_clean();
84 eval($code);
86 $file = basename($test_file);
87 $pos = strpos($file,'.');
88 $function = substr($file, 0, $pos).'_template';
89 $output = call_user_func($function, $data, TRUE);
91 $this->assertEquals($output, $expected);
93 /* */
95 public function tplProvider()
97 $datas = array();
98 foreach (glob("assert_templates/*.tpl") as $test_file) {
99 $data = array();
100 $data_file = substr($test_file, 0, -3)."php";
101 $expected = substr($test_file, 0, -3)."html";
102 if (!is_file($expected)) {
103 if (!is_file($expected.".php")) {
104 continue;
106 $expected .= ".php";
107 ob_start();
108 require $expected;
109 $expected = ob_get_clean();
110 } else {
111 $expected = file_get_contents($expected);
114 if (is_file($data_file)) {
115 try {
116 include $data_file;
117 } Catch (Exception $e) {
118 continue;
121 $datas[] = array($test_file, $data, $expected);
124 return $datas;