3 require_once 'PHPUnit/Framework.php';
5 class TemplateTest
extends PHPUnit_Framework_TestCase
9 public function setUp()
11 require_once ISSO
. '/App.php';
12 require_once ISSO
. '/Template.php';
14 BSTemplate
::$templatePath = TEST_TEMPLATE_PATH
. '/%s.test.tpl';
16 if ($this->db
== null)
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
;
26 public function tearDown()
28 $this->db
->query("DROP TABLE IF EXISTS template");
29 @unlink(TEST_TEMPLATE_PATH
. '/db.cache.test.tpl');
32 public function testFetchInvalidTemplate()
36 new BSTemplate('--invalid--');
37 $this->fail('exception expected');
43 public function testFetch()
45 $result = BSTemplate
::fetch('fetch.lang');
46 $this->assertEquals('this is a test of <?php T(\'language\') ?> strings', $result->getTemplate());
48 $result = BSTemplate
::fetch('fetch.if');
49 $this->assertEquals('this <?php if ($a == $b): ?>is cool<?php else: ?>sucks<?php endif; ?>!', $result->getTemplate());
51 $result = BSTemplate
::fetch('fetch.php');
52 $this->assertEquals('this is <?php date(\'Y\', time() ?> today\'s year', $result->getTemplate());
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());
58 public function testPreParseHook()
60 $func = create_function('$tpl', 'return "this template is now cleared";');
61 BSTemplate::$preParseHook = $func;
63 $result = new BSTemplate('hook
');
64 $this->assertEquals('this template is now cleared
', $result->getTemplate());
65 BSTemplate::$preParseHook = null;
68 public function testStandardCache()
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
');
76 $result = BSTemplate::fetch('cache
');
80 $this->fail('unexpected exception
');
82 $this->assertEquals('this file is retreived from the cache
', $result->getTemplate());
86 BSTemplate::cache(array('hook
'));
87 $this->fail('exception expected
');
93 public function testDbCache()
95 BSTemplate::$dbCacheTable = 'template
';
97 file_put_contents(TEST_TEMPLATE_PATH . '/db
.cache
.test
.tpl
', 'store in the database
');
98 BSTemplate::fetch('db
.cache
');
100 $template = $this->db->queryFirst("SELECT * FROM template");
101 $this->assertNotEquals(null, $template);
103 BSTemplate::cache(array('db
.cache
'));
104 $this->assertEquals('store in the database
', BSTemplate::fetch('db
.cache
')->getTemplate());
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
');
111 $this->assertEquals('store in the database
, retouched
', BSTemplate::fetch('db
.cache
')->getTemplate());
113 BSTemplate::$dbCacheTable = null;
116 public function testFlush()
120 BSTemplate::fetch('flush
')->flush();
121 $data = ob_get_contents();
123 $this->assertEquals('this is a
$a test
', $data);