Deprecated: Assigning the return value of new by reference is deprecated in /home/bluestat/public_html/source/index.php on line 477
ISSO - Blob - ViewGit - Blue Static
<?php

require_once 'PHPUnit/Framework.php';

class TemplateTest extends PHPUnit_Framework_TestCase
{
	private $db;

	public function setUp()
	{
		require_once ISSO . '/App.php';
		require_once ISSO . '/Template.php';

		BSTemplate::$templatePath = TEST_TEMPLATE_PATH . '/%s.test.tpl';

		if ($this->db == null)
		{
			require_once ISSO . '/DbMySql.php';
			$this->db = new BSDbMySql();
			$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::$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()
	{
		try
		{
			new BSTemplate('--invalid--');
			$this->fail('exception expected');
		}
		catch (Exception $e)
		{}
	}

	public function testFetch()
	{
		$result = BSTemplate::fetch('fetch.lang');
		$this->assertEquals('this is a test of <?php T(\'language\') ?> strings', $result->getTemplate());

		$result = BSTemplate::fetch('fetch.if');
		$this->assertEquals('this <?php  if ($a == $b):  ?>is cool<?php  else:  ?>sucks<?php  endif;  ?>!', $result->getTemplate());

		$result = BSTemplate::fetch('fetch.php');
		$this->assertEquals('this is <?php  date(\'Y\', time()  ?> today\'s year', $result->getTemplate());

		$result = BSTemplate::fetch('fetch.if.nested');
		$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());
	}

	public function testPreParseHook()
	{
		$func = create_function('$tpl', 'return "this template is now cleared";');
		BSTemplate::$preParseHook = $func;

		$result = new BSTemplate('hook');
		$this->assertEquals('this template is now cleared', $result->getTemplate());
		BSTemplate::$preParseHook = null;
	}

	public function testStandardCache()
	{
		file_put_contents(TEST_TEMPLATE_PATH . '/cache.test.tpl', 'this file is retreived from the cache');
		BSTemplate::fetch('cache');
		unlink(TEST_TEMPLATE_PATH . '/cache.test.tpl');

		try
		{
			$result = BSTemplate::fetch('cache');
		}
		catch (Exception $e)
		{
			$this->fail('unexpected exception');
		}
		$this->assertEquals('this file is retreived from the cache', $result->getTemplate());

		try
		{
			BSTemplate::cache(array('hook'));
			$this->fail('exception expected');
		}
		catch (Exception $e)
		{}
	}

	public function testDbCache()
	{
		BSTemplate::$dbCacheTable = 'template';

		file_put_contents(TEST_TEMPLATE_PATH . '/db.cache.test.tpl', 'store in the database');
		BSTemplate::fetch('db.cache');

		$template = $this->db->queryFirst("SELECT * FROM template");
		$this->assertNotEquals(null, $template);

		BSTemplate::cache(array('db.cache'));
		$this->assertEquals('store in the database', BSTemplate::fetch('db.cache')->getTemplate());

		file_put_contents(TEST_TEMPLATE_PATH . '/db.cache.test.tpl', 'store in the database, retouched');
		sleep(1); // give the file system some time
		touch(TEST_TEMPLATE_PATH . '/db.cache.test.tpl');
		clearstatcache();

		$this->assertEquals('store in the database, retouched', BSTemplate::fetch('db.cache')->getTemplate());

		BSTemplate::$dbCacheTable = null;
	}

	public function testFlush()
	{
		$a = 'foo';
		ob_start();
		BSTemplate::fetch('flush')->flush();
		$data = ob_get_contents();
		ob_end_clean();
		$this->assertEquals('this is a $a test', $data);
	}
}

?>