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