forked from php-soap/encoding
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScopedCacheTest.php
More file actions
97 lines (75 loc) · 2.76 KB
/
ScopedCacheTest.php
File metadata and controls
97 lines (75 loc) · 2.76 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
<?php
declare(strict_types=1);
namespace Soap\Encoding\Test\Unit\Cache;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;
use Soap\Encoding\Cache\ScopedCache;
use stdClass;
#[CoversClass(ScopedCache::class)]
final class ScopedCacheTest extends TestCase
{
public function test_it_returns_cached_value_on_hit(): void
{
/** @var ScopedCache<stdClass, string> */
$cache = new ScopedCache();
$scope = new stdClass();
$result1 = $cache->lookup($scope, 'key', static fn () => 'built');
$result2 = $cache->lookup($scope, 'key', static fn () => 'should not be called');
static::assertSame('built', $result1);
static::assertSame('built', $result2);
}
public function test_it_calls_factory_on_miss(): void
{
/** @var ScopedCache<stdClass, string> */
$cache = new ScopedCache();
$scope = new stdClass();
$calls = 0;
$cache->lookup($scope, 'a', static function () use (&$calls) {
$calls++;
return 'value';
});
$cache->lookup($scope, 'b', static function () use (&$calls) {
$calls++;
return 'other';
});
static::assertSame(2, $calls);
}
public function test_it_separates_keys_within_same_scope(): void
{
/** @var ScopedCache<stdClass, string> */
$cache = new ScopedCache();
$scope = new stdClass();
$a = $cache->lookup($scope, 'a', static fn () => 'alpha');
$b = $cache->lookup($scope, 'b', static fn () => 'beta');
static::assertSame('alpha', $a);
static::assertSame('beta', $b);
}
public function test_it_separates_scopes(): void
{
/** @var ScopedCache<stdClass, string> */
$cache = new ScopedCache();
$scope1 = new stdClass();
$scope2 = new stdClass();
$a = $cache->lookup($scope1, 'key', static fn () => 'from scope 1');
$b = $cache->lookup($scope2, 'key', static fn () => 'from scope 2');
static::assertSame('from scope 1', $a);
static::assertSame('from scope 2', $b);
}
public function test_it_releases_entries_when_scope_is_garbage_collected(): void
{
/** @var ScopedCache<stdClass, string> */
$cache = new ScopedCache();
$scope = new stdClass();
$cache->lookup($scope, 'key', static fn () => str_repeat('x', 1024));
unset($scope);
gc_collect_cycles();
// Create a new scope with a new key; factory must be called (no stale entry)
$newScope = new stdClass();
$calls = 0;
$cache->lookup($newScope, 'key', static function () use (&$calls) {
$calls++;
return 'fresh';
});
static::assertSame(1, $calls);
}
}