CodeCacheTest.php
4.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<?php
/**
* PHPTAL templating engine
*
* PHP Version 5
*
* @category HTML
* @package PHPTAL
* @author Laurent Bedubourg <lbedubourg@motion-twin.com>
* @author Kornel Lesiński <kornel@aardvarkmedia.co.uk>
* @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
* @version SVN: $Id: CodeCacheTest.php 605 2009-05-03 02:50:26Z kornel $
* @link http://phptal.org/
*/
require_once dirname(__FILE__)."/config.php";
class PHPTAL_CodeCacheTest 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_CodeCacheTest();
$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();
}
function testNoParseOnReexecution()
{
$this->phptal->setSource('<p>hello</p>');
$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('<p>hello2</p>');
$this->phptal->execute();
$this->assertTrue($this->phptal->testHasParsed, "Initial parse");
$this->resetPHPTAL();
$this->phptal->setSource('<p>hello2</p>');
$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 = '<test uniq="'.time().mt_rand().'" phptal:cache="1d" />';
$this->phptal->setSource($src);
$this->phptal->execute();
$this->assertTrue($this->phptal->testHasParsed, "Parse");
$this->phptal->testHasParsed = false;
$this->phptal->setSource($src);
$this->phptal->execute();
$this->assertFalse($this->phptal->testHasParsed, "Reparse!?");
$files = glob($this->codeDestination.'*');
$this->assertEquals(2,count($files)); // one for template, one for cache
foreach($files as $file) {
$this->assertFileExists($file);
touch($file, time() - 3600*24*100);
}
clearstatcache();
$this->phptal->cleanUpGarbage(); // should delete all files
clearstatcache();
// can't check for reparse, because PHPTAL uses function_exists() as a shortcut!
foreach($files as $file) {
$this->assertFileNotExists($file);
}
}
}