|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Yii\Widgets\Tests\FragmentCache; |
|
6
|
|
|
|
|
7
|
|
|
use PHPUnit\Framework\TestCase; |
|
8
|
|
|
use Yiisoft\Cache\CacheKeyNormalizer; |
|
9
|
|
|
use Yiisoft\Cache\Dependency\TagDependency; |
|
10
|
|
|
use Yiisoft\Definitions\Exception\CircularReferenceException; |
|
11
|
|
|
use Yiisoft\Definitions\Exception\InvalidConfigException; |
|
12
|
|
|
use Yiisoft\Definitions\Exception\NotInstantiableException; |
|
13
|
|
|
use Yiisoft\Factory\NotFoundException; |
|
14
|
|
|
use Yiisoft\View\Cache\CachedContent; |
|
15
|
|
|
use Yiisoft\View\Cache\DynamicContent; |
|
16
|
|
|
use Yiisoft\Yii\Widgets\FragmentCache; |
|
17
|
|
|
use Yiisoft\Yii\Widgets\Tests\Support\TestTrait; |
|
18
|
|
|
|
|
19
|
|
|
use function array_merge; |
|
20
|
|
|
use function md5; |
|
21
|
|
|
use function sha1; |
|
22
|
|
|
use function sprintf; |
|
23
|
|
|
|
|
24
|
|
|
final class FragmentCacheTest extends TestCase |
|
25
|
|
|
{ |
|
26
|
|
|
use TestTrait; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @throws CircularReferenceException |
|
30
|
|
|
* @throws InvalidConfigException |
|
31
|
|
|
* @throws NotFoundException |
|
32
|
|
|
* @throws NotInstantiableException |
|
33
|
|
|
*/ |
|
34
|
|
|
public function testCacheFragment(): void |
|
35
|
|
|
{ |
|
36
|
|
|
FragmentCache::widget()->dependency(new TagDependency('test'))->id('test')->ttl(30)->begin(); |
|
|
|
|
|
|
37
|
|
|
echo 'cached fragment'; |
|
38
|
|
|
$content = FragmentCache::end(); |
|
39
|
|
|
|
|
40
|
|
|
$this->assertSame('cached fragment', $content); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @throws CircularReferenceException |
|
45
|
|
|
* @throws InvalidConfigException |
|
46
|
|
|
* @throws NotFoundException |
|
47
|
|
|
* @throws NotInstantiableException |
|
48
|
|
|
*/ |
|
49
|
|
|
public function testCacheFragmentWithEmptyContent(): void |
|
50
|
|
|
{ |
|
51
|
|
|
FragmentCache::widget()->id('test')->ttl(30)->begin(); |
|
|
|
|
|
|
52
|
|
|
$content = FragmentCache::end(); |
|
53
|
|
|
|
|
54
|
|
|
$this->assertEmpty($content); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @throws CircularReferenceException |
|
59
|
|
|
* @throws InvalidConfigException |
|
60
|
|
|
* @throws NotFoundException |
|
61
|
|
|
* @throws NotInstantiableException |
|
62
|
|
|
*/ |
|
63
|
|
|
public function testSingleDynamicFragment(): void |
|
64
|
|
|
{ |
|
65
|
|
|
for ($counter = 0; $counter < 42; $counter++) { |
|
66
|
|
|
$dynamicContent = new DynamicContent( |
|
67
|
|
|
'dynamic-test', |
|
68
|
|
|
static fn ($params) => (string) $params['counter'], |
|
69
|
|
|
['counter' => $counter], |
|
70
|
|
|
); |
|
71
|
|
|
|
|
72
|
|
|
FragmentCache::widget()->dynamicContents($dynamicContent)->id('test')->begin(); |
|
|
|
|
|
|
73
|
|
|
echo 'single dynamic cached fragment: '; |
|
74
|
|
|
echo $dynamicContent->placeholder(); |
|
75
|
|
|
$content = FragmentCache::end(); |
|
76
|
|
|
|
|
77
|
|
|
$expectedContent = sprintf('single dynamic cached fragment: %d', $counter); |
|
78
|
|
|
|
|
79
|
|
|
$this->assertSame($expectedContent, $content); |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @throws CircularReferenceException |
|
85
|
|
|
* @throws InvalidConfigException |
|
86
|
|
|
* @throws NotFoundException |
|
87
|
|
|
* @throws NotInstantiableException |
|
88
|
|
|
*/ |
|
89
|
|
|
public function testMultipleDynamicFragments(): void |
|
90
|
|
|
{ |
|
91
|
|
|
for ($counter = 0; $counter < 42; $counter++) { |
|
92
|
|
|
$dynamicContent1 = new DynamicContent( |
|
93
|
|
|
'dynamic-test-1', |
|
94
|
|
|
static fn ($params) => md5((string) $params['counter']), |
|
95
|
|
|
['counter' => $counter], |
|
96
|
|
|
); |
|
97
|
|
|
$dynamicContent2 = new DynamicContent( |
|
98
|
|
|
'dynamic-test-2', |
|
99
|
|
|
static fn ($params) => (string) $params['counter'], |
|
100
|
|
|
['counter' => $counter], |
|
101
|
|
|
); |
|
102
|
|
|
|
|
103
|
|
|
FragmentCache::widget() |
|
104
|
|
|
->dynamicContents($dynamicContent1) |
|
105
|
|
|
->dynamicContents($dynamicContent2) |
|
106
|
|
|
->id('test') |
|
107
|
|
|
->begin() |
|
108
|
|
|
; |
|
109
|
|
|
echo 'multiple dynamic cached fragments: '; |
|
110
|
|
|
echo $dynamicContent1->placeholder(); |
|
111
|
|
|
echo $dynamicContent2->placeholder(); |
|
112
|
|
|
$content = FragmentCache::end(); |
|
113
|
|
|
|
|
114
|
|
|
$expectedContent = sprintf( |
|
115
|
|
|
'multiple dynamic cached fragments: %s%d', |
|
116
|
|
|
md5((string) $counter), |
|
117
|
|
|
$counter, |
|
118
|
|
|
); |
|
119
|
|
|
|
|
120
|
|
|
$this->assertSame($expectedContent, $content); |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* @throws CircularReferenceException |
|
126
|
|
|
* @throws InvalidConfigException |
|
127
|
|
|
* @throws NotFoundException |
|
128
|
|
|
* @throws NotInstantiableException |
|
129
|
|
|
*/ |
|
130
|
|
|
public function testNestedDynamicFragments(): void |
|
131
|
|
|
{ |
|
132
|
|
|
for ($counter = 0; $counter < 42; $counter++) { |
|
133
|
|
|
$dynamicContent1 = new DynamicContent( |
|
134
|
|
|
'dynamic-test-1', |
|
135
|
|
|
static fn ($params) => md5((string) $params['counter']), |
|
136
|
|
|
['counter' => $counter], |
|
137
|
|
|
); |
|
138
|
|
|
$dynamicContent2 = new DynamicContent( |
|
139
|
|
|
'dynamic-test-2', |
|
140
|
|
|
static fn ($params) => sha1((string) $params['counter']), |
|
141
|
|
|
['counter' => $counter], |
|
142
|
|
|
); |
|
143
|
|
|
$dynamicContent3 = new DynamicContent( |
|
144
|
|
|
'dynamic-test-3', |
|
145
|
|
|
static fn ($params) => (string) ($params['counter'] + 1), |
|
146
|
|
|
['counter' => $counter], |
|
147
|
|
|
); |
|
148
|
|
|
|
|
149
|
|
|
FragmentCache::widget()->dynamicContents($dynamicContent1)->id('test')->begin(); |
|
150
|
|
|
echo 'nested dynamic cached fragments: '; |
|
151
|
|
|
echo $dynamicContent1->placeholder(); |
|
152
|
|
|
$content = FragmentCache::end(); |
|
153
|
|
|
|
|
154
|
|
|
FragmentCache::widget() |
|
155
|
|
|
->dynamicContents($dynamicContent2) |
|
156
|
|
|
->dynamicContents($dynamicContent3) |
|
157
|
|
|
->id('test-nested') |
|
158
|
|
|
->begin() |
|
159
|
|
|
; |
|
160
|
|
|
echo $dynamicContent2->placeholder(); |
|
161
|
|
|
echo $dynamicContent3->placeholder(); |
|
162
|
|
|
$content .= FragmentCache::end(); |
|
163
|
|
|
|
|
164
|
|
|
$expectedContent = sprintf( |
|
165
|
|
|
'nested dynamic cached fragments: %s%s%d', |
|
166
|
|
|
md5((string) $counter), |
|
167
|
|
|
sha1((string) $counter), |
|
168
|
|
|
$counter + 1, |
|
169
|
|
|
); |
|
170
|
|
|
|
|
171
|
|
|
|
|
172
|
|
|
$this->assertSame($expectedContent, $content); |
|
173
|
|
|
} |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* @throws CircularReferenceException |
|
178
|
|
|
* @throws InvalidConfigException |
|
179
|
|
|
* @throws NotFoundException |
|
180
|
|
|
* @throws NotInstantiableException |
|
181
|
|
|
*/ |
|
182
|
|
|
public function testVariations(): void |
|
183
|
|
|
{ |
|
184
|
|
|
$this->setOutputCallback(static fn () => null); |
|
185
|
|
|
|
|
186
|
|
|
FragmentCache::widget()->id($id = 'test')->variations($variation = 'ru')->begin(); |
|
187
|
|
|
echo 'cached fragment'; |
|
188
|
|
|
|
|
189
|
|
|
$this->assertFalse($this->hasCache($id, $variation), 'Cached fragment should not be exist'); |
|
190
|
|
|
|
|
191
|
|
|
$content1 = FragmentCache::end(); |
|
192
|
|
|
|
|
193
|
|
|
$this->assertSame('cached fragment', $content1); |
|
194
|
|
|
|
|
195
|
|
|
FragmentCache::widget()->id($id = 'test')->variations($variation = 'ru')->begin(); |
|
196
|
|
|
|
|
197
|
|
|
$this->assertTrue($this->hasCache($id, $variation), 'Cached fragment should be exist'); |
|
198
|
|
|
|
|
199
|
|
|
$content2 = FragmentCache::end(); |
|
200
|
|
|
|
|
201
|
|
|
$this->assertSame($content1, $content2); |
|
202
|
|
|
|
|
203
|
|
|
FragmentCache::widget()->id($id = 'test')->variations($variation = 'en')->begin(); |
|
204
|
|
|
echo 'cached fragment'; |
|
205
|
|
|
|
|
206
|
|
|
$this->assertFalse($this->hasCache($id, $variation), 'Cached fragment should not be exist'); |
|
207
|
|
|
|
|
208
|
|
|
FragmentCache::end(); |
|
209
|
|
|
|
|
210
|
|
|
$this->assertTrue($this->hasCache($id, $variation), 'Cached fragment should not be exist'); |
|
211
|
|
|
|
|
212
|
|
|
/** without variations */ |
|
213
|
|
|
FragmentCache::widget()->id($id = 'test')->begin(); |
|
214
|
|
|
echo 'cached fragment'; |
|
215
|
|
|
|
|
216
|
|
|
$this->assertFalse($this->hasCache($id), 'Cached fragment should not be exist'); |
|
217
|
|
|
|
|
218
|
|
|
$content4 = FragmentCache::end(); |
|
219
|
|
|
|
|
220
|
|
|
$this->assertSame('cached fragment', $content4); |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
private function hasCache(string $id, string $variation = null): bool |
|
224
|
|
|
{ |
|
225
|
|
|
$key = (new CacheKeyNormalizer())->normalize( |
|
226
|
|
|
array_merge([CachedContent::class, $id], (array) ($variation ?? [])) |
|
227
|
|
|
); |
|
228
|
|
|
|
|
229
|
|
|
return $this->cache->psr()->has($key); |
|
230
|
|
|
} |
|
231
|
|
|
} |
|
232
|
|
|
|