3 require_once 'PHPUnit/Framework.php';
5 class TemplateTest
extends PHPUnit_Framework_TestCase
10 public function setUp()
12 require_once ISSO
. '/App.php';
14 $this->fixture
= BSApp
::LoadModule('Template');
15 $this->fixture
->setTemplateDirectory(TEST_TEMPLATE_PATH
);
16 $this->fixture
->setExtension('test.tpl');
18 if ($this->db
== null)
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
;
27 public function tearDown()
29 $this->db
->query("DROP TABLE IF EXISTS template");
30 @unlink(TEST_TEMPLATE_PATH
. '/db.cache.test.tpl');
33 public function testFetchInvalidTemplate()
37 $this->fixture
->fetch('--invalid--');
38 $this->fail('exception expected');
44 public function testFetch()
46 $result = $this->fixture
->fetch('fetch.lang');
47 $this->assertEquals('this is a test of " . gettext(\'language\') . " strings', $result);
49 $result = $this->fixture
->fetch('fetch.if');
50 $this->assertEquals('this " . (($a == $b) ? "is cool" : "sucks") . "!', $result);
52 $result = $this->fixture
->fetch('fetch.php');
53 $this->assertEquals('this is " . (date(\'Y\', time()) . " today\'s year', $result);
56 // $result = $this->fixture->fetch('fetch.php.nested');
57 // $this->assertEquals('this is a " . strpos(\'somestr{\', \'}\') . " nested token', $result);
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);
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);
65 $result = $this->fixture
->fetch('fetch.if.noelse');
66 $this->assertEquals('this is a test " . (($foo != \'moo\') ? "without else" : "") . "!', $result);
70 $this->fixture
->fetch('fetch.lang.invalid');
71 $this->fail('exception expected');
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);
80 public function testPreParseHook()
82 $func = create_function('$tpl', 'return "this template is now cleared";');
83 $this->fixture->setPreParseHook($func);
85 $result = $this->fixture->fetch('hook
');
86 $this->assertEquals('this template is now cleared
', $result);
89 public function testStandardCache()
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
');
97 $result = $this->fixture->fetch('cache
');
98 $this->assertEquals('this file is retreived from the cache
', $result);
102 $this->fixture('unexpected exception
');
107 $this->fixture->cache(array('hook
'));
108 $this->fail('exception expected
');
114 public function testDbCache()
116 $this->fixture->setDatabaseCache('template
');
118 file_put_contents(TEST_TEMPLATE_PATH . '/db
.cache
.test
.tpl
', 'store in the database
');
119 $this->fixture->cache(array('db
.cache
'));
121 $template = $this->db->queryFirst("SELECT * FROM template");
122 $this->assertNotEquals(null, $template);
125 $this->fixture->setDatabaseCache('template
');
127 $this->fixture->cache(array('db
.cache
'));
128 $this->assertEquals('store in the database
', $this->fixture->fetch('db
.cache
'));
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
');
135 $this->fixture->setDatabaseCache('template
');
136 $this->fixture->cache(array('db
.cache
'));
138 $this->fixture->setDatabaseCache('template
');
140 $this->fixture->cache(array('db
.cache
'));
141 $this->assertEquals('store in the database
, retouched
', $this->fixture->fetch('db
.cache
'));
144 public function testFlush()
148 $this->fixture->flush('');
149 $this->fail('exception expected
');
156 $this->fixture->flush($this->fixture->fetch('flush
'));
157 $data = ob_get_contents();
159 $this->assertEquals('this is a
$a test
', $data);
163 $this->fixture->flush($this->fixture->fetch('flush
'));
164 $this->fail('exception expected
');