PhptalCacheTest.php
5.18 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
<?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: PhptalCacheTest.php 579 2009-04-25 23:14:46Z kornel $
* @link http://phptal.org/
*/
require_once dirname(__FILE__)."/config.php";
class PhptalCacheTest extends PHPTAL_TestCase
{
function setUp()
{
parent::setUp();
$this->PhptalCacheTest_random = time().mt_rand();
}
private function PHPTALWithSource($source)
{
global $PhptalCacheTest_random;
$tpl = new PHPTAL();
$tpl->setForceReparse(false);
$tpl->setSource($source."<!-- {$this->PhptalCacheTest_random} -->"); // avoid cached templates from previous test runs
return $tpl;
}
function testBasicCache()
{
$tpl = $this->PHPTALWithSource('<div phptal:cache="1h" tal:content="var" />');
$tpl->var = 'SUCCESS';
$this->assertContains( "SUCCESS", $tpl->execute() );
$tpl->var = 'FAIL';
$res = $tpl->execute();
$this->assertNotContains( "FAIL", $res );
$this->assertContains( "SUCCESS", $res );
}
/**
* tal:define is also cached
*/
function testDefine()
{
$tpl = $this->PHPTALWithSource('<div tal:define="display var" phptal:cache="1h">${display}</div>');
$tpl->var = 'SUCCESS';
$this->assertContains( "SUCCESS", $tpl->execute() );
$tpl->var = 'FAIL';
$res = $tpl->execute();
$this->assertNotContains( "FAIL", $res );
$this->assertContains( "SUCCESS", $res );
}
function testTimedExpiry()
{
$this->markTestSkipped("slow tests are no fun");
$tpl = $this->PHPTALWithSource('<div phptal:cache="1s" tal:content="var" />');
$tpl->var = 'FIRST';
$this->assertContains( "FIRST", $tpl->execute() );
sleep(2); // wait for it to expire :)
$tpl->var = 'SECOND';
$res = $tpl->execute();
$this->assertContains( "SECOND", $res );
$this->assertNotContains( "FIRST", $res );
}
function testCacheInStringSource()
{
$source = '<div phptal:cache="1d" tal:content="var" />';
$tpl = $this->PHPTALWithSource($source);
$tpl->var = 'FIRST';
$this->assertContains( "FIRST", $tpl->execute() );
$tpl = $this->PHPTALWithSource($source);
$tpl->var = 'SECOND';
$this->assertContains( "FIRST", $tpl->execute() );
}
function testCleanUpCache()
{
$source = '<div phptal:cache="1d" tal:content="var" />';
$tpl = $this->PHPTALWithSource($source);
$tpl->cleanUpCache();
$tpl->var = 'FIRST';
$this->assertContains( "FIRST", $tpl->execute() );
$tpl = $this->PHPTALWithSource($source);
$tpl->var = 'SECOND';
$res = $tpl->execute();
$this->assertContains( "FIRST", $res );
$this->assertNotContains( "SECOND", $res );
$tpl->cleanUpCache();
$tpl->var = 'THIRD';
$res = $tpl->execute();
$this->assertContains( "THIRD", $res );
$this->assertNotContains( "SECOND", $res );
$this->assertNotContains( "FIRST", $res );
}
function testPerExpiry()
{
$tpl = $this->PHPTALWithSource('<div phptal:cache="1d per var" tal:content="var" />');
$tpl->var = 'FIRST';
$this->assertContains( "FIRST", $tpl->execute() );
$tpl->var = 'SECOND';
$res = $tpl->execute();
$this->assertContains( "SECOND", $res );
$this->assertNotContains( "FIRST", $res );
}
function testVersions()
{
$tpl = $this->PHPTALWithSource('<div phptal:cache="40s per version" tal:content="var" />');
$tpl->var = 'FIRST';
$tpl->version = '1';
$this->assertContains( "FIRST", $tpl->execute() );
$tpl->var = 'FAIL';
$tpl->version = '1';
$res = $tpl->execute();
$this->assertContains( "FIRST", $res );
$this->assertNotContains( "FAIL", $res );
$tpl->var = 'THRID';
$tpl->version = '3';
$res = $tpl->execute();
$this->assertContains( "THRID", $res );
$this->assertNotContains( "SECOND", $res );
$tpl->var = 'FAIL';
$tpl->version = '3';
$res = $tpl->execute();
$this->assertContains( "THRID", $res );
$this->assertNotContains( "FAIL", $res );
}
function testVariableExpiry()
{
$tpl = $this->PHPTALWithSource('<div phptal:cache="vartime s" tal:content="var" />');
$tpl->vartime = 0;
$tpl->var = 'FIRST';
$this->assertContains( "FIRST", $tpl->execute() );
$tpl->var = 'SECOND'; // time is 0 = no cache
$this->assertContains( "SECOND", $tpl->execute() );
$tpl->vartime = 60; // get it to cache it
$tpl->var = 'SECOND';
$this->assertContains( "SECOND", $tpl->execute() );
$tpl->var = 'THRID';
$res = $tpl->execute();
$this->assertContains( "SECOND", $res );
$this->assertNotContains( "THRID", $res ); // should be cached
}
}