Update version.php to 3.3.0
[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 $db;
8
9 public function setUp()
10 {
11 require_once ISSO . '/App.php';
12 require_once ISSO . '/Template.php';
13
14 BSTemplate::$templatePath = TEST_TEMPLATE_PATH . '/%s.test.tpl';
15
16 if ($this->db == null)
17 {
18 require_once ISSO . '/DbMySql.php';
19 $this->db = new BSDbMySql();
20 $this->db->connect(TEST_DB_MYSQL_HOST, TEST_DB_MYSQL_USER, TEST_DB_MYSQL_PASSWORD, TEST_DB_MYSQL_DATABASE);
21 $this->db->query("CREATE TABLE template (filename VARCHAR (250) NOT NULL, template TEXT NOT NULL, timestamp INT NOT NULL);");
22 BSApp::$db = $this->db;
23 }
24 }
25
26 public function tearDown()
27 {
28 $this->db->query("DROP TABLE IF EXISTS template");
29 @unlink(TEST_TEMPLATE_PATH . '/db.cache.test.tpl');
30 }
31
32 public function testFetchInvalidTemplate()
33 {
34 try
35 {
36 new BSTemplate('--invalid--');
37 $this->fail('exception expected');
38 }
39 catch (Exception $e)
40 {}
41 }
42
43 public function testFetch()
44 {
45 $result = BSTemplate::fetch('fetch.lang');
46 $this->assertEquals('this is a test of <?php T(\'language\') ?> strings', $result->getTemplate());
47
48 $result = BSTemplate::fetch('fetch.if');
49 $this->assertEquals('this <?php if ($a == $b): ?>is cool<?php else: ?>sucks<?php endif; ?>!', $result->getTemplate());
50
51 $result = BSTemplate::fetch('fetch.php');
52 $this->assertEquals('this is <?php date(\'Y\', time() ?> today\'s year', $result->getTemplate());
53
54 $result = BSTemplate::fetch('fetch.if.nested');
55 $this->assertEquals('this <?php if ($c == $d): ?>foo <?php if ($e == $f): ?>e<?php else: ?>f<?php endif; ?><?php else: ?>moo <?php if ($g == $h): ?>g<?php else: ?>h<?php endif; ?><?php endif; ?> rules!', $result->getTemplate());
56 }
57
58 public function testPreParseHook()
59 {
60 $func = create_function('$tpl', 'return "this template is now cleared";');
61 BSTemplate::$preParseHook = $func;
62
63 $result = new BSTemplate('hook');
64 $this->assertEquals('this template is now cleared', $result->getTemplate());
65 BSTemplate::$preParseHook = null;
66 }
67
68 public function testStandardCache()
69 {
70 file_put_contents(TEST_TEMPLATE_PATH . '/cache.test.tpl', 'this file is retreived from the cache');
71 BSTemplate::fetch('cache');
72 unlink(TEST_TEMPLATE_PATH . '/cache.test.tpl');
73
74 try
75 {
76 $result = BSTemplate::fetch('cache');
77 }
78 catch (Exception $e)
79 {
80 $this->fail('unexpected exception');
81 }
82 $this->assertEquals('this file is retreived from the cache', $result->getTemplate());
83
84 try
85 {
86 BSTemplate::cache(array('hook'));
87 $this->fail('exception expected');
88 }
89 catch (Exception $e)
90 {}
91 }
92
93 public function testDbCache()
94 {
95 BSTemplate::$dbCacheTable = 'template';
96
97 file_put_contents(TEST_TEMPLATE_PATH . '/db.cache.test.tpl', 'store in the database');
98 BSTemplate::fetch('db.cache');
99
100 $template = $this->db->queryFirst("SELECT * FROM template");
101 $this->assertNotEquals(null, $template);
102
103 BSTemplate::cache(array('db.cache'));
104 $this->assertEquals('store in the database', BSTemplate::fetch('db.cache')->getTemplate());
105
106 file_put_contents(TEST_TEMPLATE_PATH . '/db.cache.test.tpl', 'store in the database, retouched');
107 sleep(1); // give the file system some time
108 touch(TEST_TEMPLATE_PATH . '/db.cache.test.tpl');
109 clearstatcache();
110
111 $this->assertEquals('store in the database, retouched', BSTemplate::fetch('db.cache')->getTemplate());
112
113 BSTemplate::$dbCacheTable = null;
114 }
115
116 public function testFlush()
117 {
118 $a = 'foo';
119 ob_start();
120 BSTemplate::fetch('flush')->flush();
121 $data = ob_get_contents();
122 ob_end_clean();
123 $this->assertEquals('this is a $a test', $data);
124 }
125 }
126
127 ?>