3 require_once 'PHPUnit/Framework.php';
5 class TemplateTest
extends PHPUnit_Framework_TestCase
10 public function setUp()
12 require_once ISSO
. '/App.php';
13 require_once ISSO
. '/Template.php';
15 $this->fixture
= new BSTemplate();
16 $this->fixture
->setTemplateDirectory(TEST_TEMPLATE_PATH
);
17 $this->fixture
->setExtension('test.tpl');
19 if ($this->db
== null
)
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
;
29 public function tearDown()
31 $this->db
->query("DROP TABLE IF EXISTS template");
32 @unlink(TEST_TEMPLATE_PATH
. '/db.cache.test.tpl');
35 public function testFetchInvalidTemplate()
39 $this->fixture
->fetch('--invalid--');
40 $this->fail('exception expected');
46 public function testFetch()
48 $result = $this->fixture
->fetch('fetch.lang');
49 $this->assertEquals('this is a test of " . gettext(\'language\') . " strings', $result);
51 $result = $this->fixture
->fetch('fetch.if');
52 $this->assertEquals('this " . (($a == $b) ? "is cool" : "sucks") . "!', $result);
54 $result = $this->fixture
->fetch('fetch.php');
55 $this->assertEquals('this is " . (date(\'Y\', time()) . " today\'s year', $result);
58 // $result = $this->fixture->fetch('fetch.php.nested');
59 // $this->assertEquals('this is a " . strpos(\'somestr{\', \'}\') . " nested token', $result);
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);
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);
67 $result = $this->fixture
->fetch('fetch.if.noelse');
68 $this->assertEquals('this is a test " . (($foo != \'moo\') ? "without else" : "") . "!', $result);
72 $this->fixture
->fetch('fetch.lang.invalid');
73 $this->fail('exception expected');
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);
82 public function testPreParseHook()
84 $func = create_function('$tpl', 'return "this template is now cleared";');
85 $this->fixture
->setPreParseHook($func);
87 $result = $this->fixture
->fetch('hook');
88 $this->assertEquals('this template is now cleared', $result);
91 public function testStandardCache()
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');
99 $result = $this->fixture
->fetch('cache');
100 $this->assertEquals('this file is retreived from the cache', $result);
104 $this->fixture('unexpected exception');
109 $this->fixture
->cache(array('hook'));
110 $this->fail('exception expected');
116 public function testDbCache()
118 $this->fixture
->setDatabaseCache('template');
120 file_put_contents(TEST_TEMPLATE_PATH
. '/db.cache.test.tpl', 'store in the database');
121 $this->fixture
->cache(array('db.cache'));
123 $template = $this->db
->queryFirst("SELECT * FROM template");
124 $this->assertNotEquals(null
, $template);
127 $this->fixture
->setDatabaseCache('template');
129 $this->fixture
->cache(array('db.cache'));
130 $this->assertEquals('store in the database', $this->fixture
->fetch('db.cache'));
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');
137 $this->fixture
->setDatabaseCache('template');
138 $this->fixture
->cache(array('db.cache'));
140 $this->fixture
->setDatabaseCache('template');
142 $this->fixture
->cache(array('db.cache'));
143 $this->assertEquals('store in the database, retouched', $this->fixture
->fetch('db.cache'));
146 public function testFlush()
150 $this->fixture
->flush('');
151 $this->fail('exception expected');
158 $this->fixture
->flush($this->fixture
->fetch('flush'));
159 $data = ob_get_contents();
161 $this->assertEquals('this is a $a test', $data);
165 $this->fixture
->flush($this->fixture
->fetch('flush'));
166 $this->fail('exception expected');