Added tests for template preparse hooks and the cache system
authorRobert Sesek <rsesek@bluestatic.org>
Sun, 19 Aug 2007 17:58:38 +0000 (17:58 +0000)
committerRobert Sesek <rsesek@bluestatic.org>
Sun, 19 Aug 2007 17:58:38 +0000 (17:58 +0000)
* UnitTest/TemplateTest.php

UnitTest/TemplateTest.php
UnitTest/templates/hook.test.tpl [new file with mode: 0644]

index 935c3ded7fd361345eb5400aec25264ef3567ff4..42a10398774f1a3b129197de7034d281b9b72450 100644 (file)
@@ -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", "<strong>[MISSING SUBSTITUTION INDEX: 2]</strong>", "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 (file)
index 0000000..15f738b
--- /dev/null
@@ -0,0 +1 @@
+none of this text should ever be seen
\ No newline at end of file