From 50f5584c641ba27a4862d80d60cf3cc48c52908a Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Sun, 19 Aug 2007 17:58:38 +0000 Subject: [PATCH] Added tests for template preparse hooks and the cache system * UnitTest/TemplateTest.php --- UnitTest/TemplateTest.php | 78 ++++++++++++++++++++++++++++++++ UnitTest/templates/hook.test.tpl | 1 + 2 files changed, 79 insertions(+) create mode 100644 UnitTest/templates/hook.test.tpl diff --git a/UnitTest/TemplateTest.php b/UnitTest/TemplateTest.php index 935c3de..42a1039 100644 --- a/UnitTest/TemplateTest.php +++ b/UnitTest/TemplateTest.php @@ -5,6 +5,7 @@ require_once 'PHPUnit/Framework.php'; class TemplateTest extends PHPUnit_Framework_TestCase { private $fixture; + private $db; public function setUp() { @@ -13,6 +14,20 @@ class TemplateTest extends PHPUnit_Framework_TestCase $this->fixture = BSApp::LoadModule('Template'); $this->fixture->setTemplateDirectory(TEST_TEMPLATE_PATH); $this->fixture->setExtension('test.tpl'); + + if ($this->db == null) + { + $this->db = BSApp::LoadModule('DbMySql'); + $this->db->connect(TEST_DB_MYSQL_HOST, TEST_DB_MYSQL_USER, TEST_DB_MYSQL_PASSWORD, TEST_DB_MYSQL_DATABASE); + $this->db->query("CREATE TABLE template (filename VARCHAR (250) NOT NULL, template TEXT NOT NULL, timestamp INT NOT NULL);"); + BSApp::Registry()->db = $this->db; + } + } + + public function tearDown() + { + $this->db->query("DROP TABLE IF EXISTS template"); + @unlink(TEST_TEMPLATE_PATH . '/db.cache.test.tpl'); } public function testFetchInvalidTemplate() @@ -61,6 +76,69 @@ class TemplateTest extends PHPUnit_Framework_TestCase $result = $this->fixture->fetch('fetch.lang.indexmissing'); $this->assertEquals('missing " . sprintf(\'" . gettext(\'1 %1$s 2 %2$s 3 %3$s\') . "\', "a", "[MISSING SUBSTITUTION INDEX: 2]", "b") . " index', $result); } + + public function testPreParseHook() + { + $func = create_function('$tpl', 'return "this template is now cleared";'); + $this->fixture->setPreParseHook($func); + + $result = $this->fixture->fetch('hook'); + $this->assertEquals('this template is now cleared', $result); + } + + public function testStandardCache() + { + file_put_contents(TEST_TEMPLATE_PATH . '/cache.test.tpl', 'this file is retreived from the cache'); + $this->fixture->cache(array('cache')); + unlink(TEST_TEMPLATE_PATH . '/cache.test.tpl'); + + try + { + $result = $this->fixture->fetch('cache'); + $this->assertEquals('this file is retreived from the cache', $result); + } + catch (Exception $e) + { + $this->fixture('unexpected exception'); + } + + try + { + $this->fixture->cache(array('hook')); + $this->fail('exception expected'); + } + catch (Exception $e) + {} + } + + public function testDbCache() + { + $this->fixture->setDatabaseCache('template'); + + file_put_contents(TEST_TEMPLATE_PATH . '/db.cache.test.tpl', 'store in the database'); + $this->fixture->cache(array('db.cache')); + + $template = $this->db->queryFirst("SELECT * FROM template"); + $this->assertNotEquals(null, $template); + + $this->setUp(); + $this->fixture->setDatabaseCache('template'); + + $this->fixture->cache(array('db.cache')); + $this->assertEquals('store in the database', $this->fixture->fetch('db.cache')); + + file_put_contents(TEST_TEMPLATE_PATH . '/db.cache.test.tpl', 'store in the database, retouched'); + touch(TEST_TEMPLATE_PATH . '/db.cache.test.tpl'); + clearstatcache(); + $this->setUp(); + $this->fixture->setDatabaseCache('template'); + $this->fixture->cache(array('db.cache')); + $this->setUp(); + $this->fixture->setDatabaseCache('template'); + + $this->fixture->cache(array('db.cache')); + $this->assertEquals('store in the database, retouched', $this->fixture->fetch('db.cache')); + } } ?> \ No newline at end of file diff --git a/UnitTest/templates/hook.test.tpl b/UnitTest/templates/hook.test.tpl new file mode 100644 index 0000000..15f738b --- /dev/null +++ b/UnitTest/templates/hook.test.tpl @@ -0,0 +1 @@ +none of this text should ever be seen \ No newline at end of file -- 2.43.5