* @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License * @version SVN: $Id: CodeCacheTest.php 888 2010-06-08 09:48:33Z kornel $ * @link http://phptal.org/ */ class PHPTAL_TestCodeCache extends PHPTAL { public $testHasParsed = false; function parse() { $this->testHasParsed = true; return parent::parse(); } } class CodeCacheTest extends PHPTAL_TestCase { private $phptal; private $codeDestination; private function resetPHPTAL() { $this->phptal = new PHPTAL_TestCodeCache(); $this->phptal->setForceReparse(false); $this->assertFalse($this->phptal->getForceReparse()); if (function_exists('sys_get_temp_dir')) { $tmpdirpath = sys_get_temp_dir().DIRECTORY_SEPARATOR.'temp_output'; if (!is_dir($tmpdirpath)) mkdir($tmpdirpath); } else $this->markTestSkipped("Newer PHP needed"); $this->assertTrue(is_dir($tmpdirpath)); $this->assertTrue(is_writable($tmpdirpath)); $this->phptal->setPhpCodeDestination($tmpdirpath); $this->codeDestination = $this->phptal->getPhpCodeDestination(); } private function clearCache() { $this->assertContains(DIRECTORY_SEPARATOR.'temp_output'.DIRECTORY_SEPARATOR, $this->codeDestination); foreach (glob($this->codeDestination.'tpl_*') as $tpl) { $this->assertTrue(unlink($tpl), "Delete $tpl"); } } function setUp() { parent::setUp(); $this->resetPHPTAL(); $this->clearCache(); } function tearDown() { $this->clearCache(); parent::tearDown(); } function testNoParseOnReexecution() { $this->phptal->setSource('
hello
'); $this->phptal->execute(); $this->assertTrue($this->phptal->testHasParsed, "Initial parse"); $this->phptal->testHasParsed = false; $this->phptal->execute(); $this->assertFalse($this->phptal->testHasParsed, "No reparse"); } function testNoParseOnReset() { $this->phptal->setSource('hello2
'); $this->phptal->execute(); $this->assertTrue($this->phptal->testHasParsed, "Initial parse"); $this->resetPHPTAL(); $this->phptal->setSource('hello2
'); $this->phptal->execute(); $this->assertFalse($this->phptal->testHasParsed, "No reparse"); } function testReparseAfterTouch() { if (!is_writable('input/code-cache-01.html')) $this->markTestSkipped(); $time1 = filemtime('input/code-cache-01.html'); touch('input/code-cache-01.html', time()); clearstatcache(); $time2 = filemtime('input/code-cache-01.html'); $this->assertNotEquals($time1, $time2, "touch() must work"); $this->phptal->setTemplate('input/code-cache-01.html'); $this->phptal->execute(); $this->assertTrue($this->phptal->testHasParsed, "Initial parse"); $this->resetPHPTAL(); touch('input/code-cache-01.html', $time1); clearstatcache(); $this->phptal->setTemplate('input/code-cache-01.html'); $this->phptal->execute(); $this->assertTrue($this->phptal->testHasParsed, "Reparse"); } function testGarbageRemoval() { $src = '