Changing the way we include framework files. Instead of modifying the include_path...
[isso.git] / UnitTest / TemplateTest.php
1 <?php
2
3 require_once 'PHPUnit/Framework.php';
4
5 class TemplateTest extends PHPUnit_Framework_TestCase
6 {
7 private $fixture;
8 private $db;
9
10 public function setUp()
11 {
12 require_once ISSO . '/App.php';
13
14 $this->fixture = BSApp::LoadModule('Template');
15 $this->fixture->setTemplateDirectory(TEST_TEMPLATE_PATH);
16 $this->fixture->setExtension('test.tpl');
17
18 if ($this->db == null)
19 {
20 $this->db = BSApp::LoadModule('DbMySql');
21 $this->db->connect(TEST_DB_MYSQL_HOST, TEST_DB_MYSQL_USER, TEST_DB_MYSQL_PASSWORD, TEST_DB_MYSQL_DATABASE);
22 $this->db->query("CREATE TABLE template (filename VARCHAR (250) NOT NULL, template TEXT NOT NULL, timestamp INT NOT NULL);");
23 BSApp::Registry()->db = $this->db;
24 }
25 }
26
27 public function tearDown()
28 {
29 $this->db->query("DROP TABLE IF EXISTS template");
30 @unlink(TEST_TEMPLATE_PATH . '/db.cache.test.tpl');
31 }
32
33 public function testFetchInvalidTemplate()
34 {
35 try
36 {
37 $this->fixture->fetch('--invalid--');
38 $this->fail('exception expected');
39 }
40 catch (Exception $e)
41 {}
42 }
43
44 public function testFetch()
45 {
46 $result = $this->fixture->fetch('fetch.lang');
47 $this->assertEquals('this is a test of " . gettext(\'language\') . " strings', $result);
48
49 $result = $this->fixture->fetch('fetch.if');
50 $this->assertEquals('this " . (($a == $b) ? "is cool" : "sucks") . "!', $result);
51
52 $result = $this->fixture->fetch('fetch.php');
53 $this->assertEquals('this is " . (date(\'Y\', time()) . " today\'s year', $result);
54
55 // TODO: pass test
56 // $result = $this->fixture->fetch('fetch.php.nested');
57 // $this->assertEquals('this is a " . strpos(\'somestr{\', \'}\') . " nested token', $result);
58
59 $result = $this->fixture->fetch('fetch.lang.tag');
60 $this->assertEquals('this is a " . sprintf(\'" . gettext(\'a %1$s b %2$s\') . "\', "hello", "there") . " test', $result);
61
62 $result = $this->fixture->fetch('fetch.if.nested');
63 $this->assertEquals('this " . (($c == $d) ? "foo " . (($e == $f) ? "e" : "f") . "" : "moo " . (($g == $h) ? "g" : "h") . "") . " rules!', $result);
64
65 $result = $this->fixture->fetch('fetch.if.noelse');
66 $this->assertEquals('this is a test " . (($foo != \'moo\') ? "without else" : "") . "!', $result);
67
68 try
69 {
70 $this->fixture->fetch('fetch.lang.invalid');
71 $this->fail('exception expected');
72 }
73 catch (Exception $e)
74 {}
75
76 $result = $this->fixture->fetch('fetch.lang.indexmissing');
77 $this->assertEquals('missing " . sprintf(\'" . gettext(\'1 %1$s 2 %2$s 3 %3$s\') . "\', "a", "<strong>[MISSING SUBSTITUTION INDEX: 2]</strong>", "b") . " index', $result);
78 }
79
80 public function testPreParseHook()
81 {
82 $func = create_function('$tpl', 'return "this template is now cleared";');
83 $this->fixture->setPreParseHook($func);
84
85 $result = $this->fixture->fetch('hook');
86 $this->assertEquals('this template is now cleared', $result);
87 }
88
89 public function testStandardCache()
90 {
91 file_put_contents(TEST_TEMPLATE_PATH . '/cache.test.tpl', 'this file is retreived from the cache');
92 $this->fixture->cache(array('cache'));
93 unlink(TEST_TEMPLATE_PATH . '/cache.test.tpl');
94
95 try
96 {
97 $result = $this->fixture->fetch('cache');
98 $this->assertEquals('this file is retreived from the cache', $result);
99 }
100 catch (Exception $e)
101 {
102 $this->fixture('unexpected exception');
103 }
104
105 try
106 {
107 $this->fixture->cache(array('hook'));
108 $this->fail('exception expected');
109 }
110 catch (Exception $e)
111 {}
112 }
113
114 public function testDbCache()
115 {
116 $this->fixture->setDatabaseCache('template');
117
118 file_put_contents(TEST_TEMPLATE_PATH . '/db.cache.test.tpl', 'store in the database');
119 $this->fixture->cache(array('db.cache'));
120
121 $template = $this->db->queryFirst("SELECT * FROM template");
122 $this->assertNotEquals(null, $template);
123
124 $this->setUp();
125 $this->fixture->setDatabaseCache('template');
126
127 $this->fixture->cache(array('db.cache'));
128 $this->assertEquals('store in the database', $this->fixture->fetch('db.cache'));
129
130 file_put_contents(TEST_TEMPLATE_PATH . '/db.cache.test.tpl', 'store in the database, retouched');
131 sleep(1); // give the file system some time
132 touch(TEST_TEMPLATE_PATH . '/db.cache.test.tpl');
133 clearstatcache();
134 $this->setUp();
135 $this->fixture->setDatabaseCache('template');
136 $this->fixture->cache(array('db.cache'));
137 $this->setUp();
138 $this->fixture->setDatabaseCache('template');
139
140 $this->fixture->cache(array('db.cache'));
141 $this->assertEquals('store in the database, retouched', $this->fixture->fetch('db.cache'));
142 }
143
144 public function testFlush()
145 {
146 try
147 {
148 $this->fixture->flush('');
149 $this->fail('exception expected');
150 }
151 catch (Exception $e)
152 {}
153
154 $a = 'foo';
155 ob_start();
156 $this->fixture->flush($this->fixture->fetch('flush'));
157 $data = ob_get_contents();
158 ob_end_clean();
159 $this->assertEquals('this is a $a test', $data);
160
161 try
162 {
163 $this->fixture->flush($this->fixture->fetch('flush'));
164 $this->fail('exception expected');
165 }
166 catch (Exception $e)
167 {}
168 }
169 }
170
171 ?>