|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Yiisoft\Yii\Widgets\Tests; |
|
4
|
|
|
|
|
5
|
|
|
use Yiisoft\Yii\Widgets\FragmentCache; |
|
6
|
|
|
|
|
7
|
|
|
final class FragmentCacheTest extends TestCase |
|
8
|
|
|
{ |
|
9
|
|
|
public function testCacheEnabled(): void |
|
10
|
|
|
{ |
|
11
|
|
|
ob_start(); |
|
12
|
|
|
ob_implicit_flush(false); |
|
13
|
|
|
|
|
14
|
|
|
FragmentCache::begin() |
|
15
|
|
|
->id('test') |
|
16
|
|
|
->start(); |
|
17
|
|
|
|
|
18
|
|
|
echo 'cached fragment'; |
|
19
|
|
|
|
|
20
|
|
|
echo FragmentCache::end(); |
|
21
|
|
|
|
|
22
|
|
|
$this->assertEquals('cached fragment', ob_get_clean()); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
public function testCacheDisabled1() |
|
26
|
|
|
{ |
|
27
|
|
|
$expectedLevel = ob_get_level(); |
|
28
|
|
|
|
|
29
|
|
|
ob_start(); |
|
30
|
|
|
ob_implicit_flush(0); |
|
31
|
|
|
|
|
32
|
|
|
$key = array_merge([FragmentCache::class, 'test']); |
|
33
|
|
|
|
|
34
|
|
|
FragmentCache::begin() |
|
35
|
|
|
->id('test') |
|
36
|
|
|
->cache(null) |
|
37
|
|
|
->start(); |
|
38
|
|
|
|
|
39
|
|
|
echo 'cached fragment'; |
|
40
|
|
|
|
|
41
|
|
|
echo FragmentCache::end(); |
|
42
|
|
|
|
|
43
|
|
|
$this->assertFalse($this->cache->has($key)); |
|
44
|
|
|
$this->assertEquals('cached fragment', ob_get_clean()); |
|
45
|
|
|
|
|
46
|
|
|
ob_end_clean(); |
|
47
|
|
|
|
|
48
|
|
|
$this->assertEquals($expectedLevel, ob_get_level(), 'Output buffer not closed correctly.'); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
public function testCacheDisabled2() |
|
52
|
|
|
{ |
|
53
|
|
|
$expectedLevel = ob_get_level(); |
|
54
|
|
|
|
|
55
|
|
|
ob_start(); |
|
56
|
|
|
ob_implicit_flush(0); |
|
57
|
|
|
|
|
58
|
|
|
$key = array_merge([FragmentCache::class, 'test']); |
|
59
|
|
|
|
|
60
|
|
|
FragmentCache::begin() |
|
61
|
|
|
->id('test') |
|
62
|
|
|
->start(); |
|
63
|
|
|
|
|
64
|
|
|
echo 'cached fragment'; |
|
65
|
|
|
|
|
66
|
|
|
echo FragmentCache::end(); |
|
67
|
|
|
|
|
68
|
|
|
$this->assertTrue($this->cache->has($key)); |
|
69
|
|
|
$this->assertEquals('cached fragment', ob_get_clean()); |
|
70
|
|
|
|
|
71
|
|
|
ob_start(); |
|
72
|
|
|
ob_implicit_flush(0); |
|
73
|
|
|
|
|
74
|
|
|
FragmentCache::begin() |
|
75
|
|
|
->id('test') |
|
76
|
|
|
->cache(null) |
|
77
|
|
|
->start(); |
|
78
|
|
|
|
|
79
|
|
|
echo 'cached fragment other'; |
|
80
|
|
|
|
|
81
|
|
|
echo FragmentCache::end(); |
|
82
|
|
|
|
|
83
|
|
|
$this->assertEquals('cached fragment other', ob_get_clean()); |
|
84
|
|
|
|
|
85
|
|
|
ob_end_clean(); |
|
86
|
|
|
|
|
87
|
|
|
$this->assertEquals($expectedLevel, ob_get_level(), 'Output buffer not closed correctly.'); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
public function testSingleDynamicFragment() |
|
91
|
|
|
{ |
|
92
|
|
|
$params = 0; |
|
93
|
|
|
|
|
94
|
|
|
for ($counter = 0; $counter < 42; $counter++) { |
|
95
|
|
|
ob_start(); |
|
96
|
|
|
ob_implicit_flush(0); |
|
97
|
|
|
|
|
98
|
|
|
FragmentCache::begin()->id('test')->start(); |
|
99
|
|
|
|
|
100
|
|
|
echo 'single dynamic cached fragment: '; |
|
101
|
|
|
echo $this->webView->renderDynamic('return $counter++;', ['counter' => $params]); |
|
102
|
|
|
|
|
103
|
|
|
echo FragmentCache::end(); |
|
104
|
|
|
|
|
105
|
|
|
|
|
106
|
|
|
$expectedContent = vsprintf('single dynamic cached fragment: %d', [ |
|
107
|
|
|
$params, |
|
108
|
|
|
]); |
|
109
|
|
|
|
|
110
|
|
|
$this->assertEquals($expectedContent, ob_get_clean()); |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
public function testMultipleDynamicFragments() |
|
115
|
|
|
{ |
|
116
|
|
|
$params = 0; |
|
117
|
|
|
|
|
118
|
|
|
for ($counter = 0; $counter < 42; $counter++) { |
|
119
|
|
|
ob_start(); |
|
120
|
|
|
ob_implicit_flush(0); |
|
121
|
|
|
|
|
122
|
|
|
FragmentCache::begin()->id('test')->start(); |
|
123
|
|
|
|
|
124
|
|
|
echo 'multiple dynamic cached fragments: '; |
|
125
|
|
|
echo $this->webView->renderDynamic('return md5($counter);', ['counter' => $params]); |
|
126
|
|
|
echo $this->webView->renderDynamic('return $counter++;', ['counter' => $params]); |
|
127
|
|
|
|
|
128
|
|
|
echo FragmentCache::end(); |
|
129
|
|
|
|
|
130
|
|
|
$expectedContent = vsprintf('multiple dynamic cached fragments: %s%d', [ |
|
131
|
|
|
md5($params), |
|
132
|
|
|
$params, |
|
133
|
|
|
]); |
|
134
|
|
|
|
|
135
|
|
|
$this->assertEquals($expectedContent, ob_get_clean()); |
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
public function testNestedDynamicFragments() |
|
140
|
|
|
{ |
|
141
|
|
|
$params = 0; |
|
142
|
|
|
|
|
143
|
|
|
for ($counter = 0; $counter < 42; $counter++) { |
|
144
|
|
|
ob_start(); |
|
145
|
|
|
ob_implicit_flush(false); |
|
146
|
|
|
|
|
147
|
|
|
FragmentCache::begin()->id('test')->start(); |
|
148
|
|
|
|
|
149
|
|
|
echo 'nested dynamic cached fragments: '; |
|
150
|
|
|
echo $this->webView->renderDynamic('return md5($counter);', ['counter' => $params]); |
|
151
|
|
|
|
|
152
|
|
|
FragmentCache::begin()->id('test-nested')->start(); |
|
153
|
|
|
echo $this->webView->renderDynamic('return sha1($counter);', ['counter' => $params]); |
|
154
|
|
|
echo FragmentCache::end(); |
|
155
|
|
|
|
|
156
|
|
|
echo $this->webView->renderDynamic('return $counter++;', ['counter' => $params]); |
|
157
|
|
|
|
|
158
|
|
|
echo FragmentCache::end(); |
|
159
|
|
|
|
|
160
|
|
|
$expectedContent = vsprintf('nested dynamic cached fragments: %s%s%d', [ |
|
161
|
|
|
md5($params), |
|
162
|
|
|
sha1($params), |
|
163
|
|
|
$params, |
|
164
|
|
|
]); |
|
165
|
|
|
|
|
166
|
|
|
$this->assertEquals($expectedContent, ob_get_clean()); |
|
167
|
|
|
} |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
public function testVariations() |
|
171
|
|
|
{ |
|
172
|
|
|
$this->setOutputCallback(static function ($output) { |
|
173
|
|
|
return null; |
|
174
|
|
|
}); |
|
175
|
|
|
|
|
176
|
|
|
ob_start(); |
|
177
|
|
|
ob_implicit_flush(0); |
|
178
|
|
|
|
|
179
|
|
|
$key = array_merge([FragmentCache::class, 'test'], ['variations' => ['ru']]); |
|
180
|
|
|
|
|
181
|
|
|
FragmentCache::begin() |
|
182
|
|
|
->id('test') |
|
183
|
|
|
->variations(['variations' => ['ru']]) |
|
184
|
|
|
->start(); |
|
185
|
|
|
|
|
186
|
|
|
echo 'cached fragment'; |
|
187
|
|
|
|
|
188
|
|
|
$this->assertFalse($this->cache->has($key), 'Cached fragment should not be exist'); |
|
189
|
|
|
|
|
190
|
|
|
echo FragmentCache::end(); |
|
191
|
|
|
|
|
192
|
|
|
$cached = ob_get_clean(); |
|
193
|
|
|
$this->assertEquals('cached fragment', $cached); |
|
194
|
|
|
|
|
195
|
|
|
ob_start(); |
|
196
|
|
|
ob_implicit_flush(0); |
|
197
|
|
|
|
|
198
|
|
|
FragmentCache::begin() |
|
199
|
|
|
->id('test') |
|
200
|
|
|
->variations(['variations' => ['ru']]) |
|
201
|
|
|
->content(null) |
|
202
|
|
|
->start(); |
|
203
|
|
|
|
|
204
|
|
|
$this->assertTrue($this->cache->has($key), 'Cached fragment should be exist'); |
|
205
|
|
|
|
|
206
|
|
|
echo FragmentCache::end(); |
|
207
|
|
|
|
|
208
|
|
|
$this->assertEquals($cached, ob_get_clean()); |
|
209
|
|
|
|
|
210
|
|
|
$key = array_merge([FragmentCache::class, 'test'], ['variations' => ['en']]); |
|
211
|
|
|
|
|
212
|
|
|
FragmentCache::begin() |
|
213
|
|
|
->id('test') |
|
214
|
|
|
->variations(['variations' => ['en']]) |
|
215
|
|
|
->start(); |
|
216
|
|
|
|
|
217
|
|
|
echo 'cached fragment'; |
|
218
|
|
|
|
|
219
|
|
|
$this->assertFalse($this->cache->has($key), 'Cached fragment should not be exist'); |
|
220
|
|
|
|
|
221
|
|
|
echo FragmentCache::end(); |
|
222
|
|
|
|
|
223
|
|
|
FragmentCache::begin() |
|
224
|
|
|
->id('test') |
|
225
|
|
|
->variations(['variations' => ['en']]) |
|
226
|
|
|
->content(null) |
|
227
|
|
|
->start(); |
|
228
|
|
|
|
|
229
|
|
|
echo FragmentCache::end(); |
|
230
|
|
|
|
|
231
|
|
|
$this->assertTrue($this->cache->has($key), 'Cached fragment should be exist'); |
|
232
|
|
|
|
|
233
|
|
|
//without variations |
|
234
|
|
|
ob_start(); |
|
235
|
|
|
ob_implicit_flush(false); |
|
236
|
|
|
|
|
237
|
|
|
$key = [FragmentCache::class, 'test']; |
|
238
|
|
|
|
|
239
|
|
|
FragmentCache::begin() |
|
240
|
|
|
->id('test') |
|
241
|
|
|
->start(); |
|
242
|
|
|
|
|
243
|
|
|
echo 'cached fragment'; |
|
244
|
|
|
|
|
245
|
|
|
$this->assertFalse($this->cache->has($key), 'Cached fragment should not be exist'); |
|
246
|
|
|
|
|
247
|
|
|
echo FragmentCache::end(); |
|
248
|
|
|
|
|
249
|
|
|
$this->assertEquals('cached fragment', ob_get_clean()); |
|
250
|
|
|
|
|
251
|
|
|
//with variations as a string |
|
252
|
|
|
ob_start(); |
|
253
|
|
|
ob_implicit_flush(0); |
|
254
|
|
|
|
|
255
|
|
|
$key = array_merge([FragmentCache::class, 'test'], ['variations' => 'uz']); |
|
256
|
|
|
|
|
257
|
|
|
FragmentCache::begin() |
|
258
|
|
|
->id('test') |
|
259
|
|
|
->variations(['variations' => 'uz']) |
|
260
|
|
|
->start(); |
|
261
|
|
|
|
|
262
|
|
|
echo 'cached fragment'; |
|
263
|
|
|
|
|
264
|
|
|
$this->assertFalse($this->cache->has($key), 'Cached fragment should not be exist'); |
|
265
|
|
|
|
|
266
|
|
|
echo FragmentCache::end(); |
|
267
|
|
|
|
|
268
|
|
|
$cached = ob_get_clean(); |
|
269
|
|
|
$this->assertEquals('cached fragment', $cached); |
|
270
|
|
|
|
|
271
|
|
|
ob_start(); |
|
272
|
|
|
ob_implicit_flush(0); |
|
273
|
|
|
|
|
274
|
|
|
FragmentCache::begin() |
|
275
|
|
|
->id('test') |
|
276
|
|
|
->variations(['variations' => 'uz']) |
|
277
|
|
|
->content(null) |
|
278
|
|
|
->start(); |
|
279
|
|
|
echo FragmentCache::end(); |
|
280
|
|
|
|
|
281
|
|
|
$this->assertTrue($this->cache->has($key), 'Cached fragment should be exist'); |
|
282
|
|
|
$this->assertEquals($cached, ob_get_clean()); |
|
283
|
|
|
} |
|
284
|
|
|
} |
|
285
|
|
|
|