Skip to content

Commit c8909db

Browse files
committed
Add unit tests for ScopedCache
1 parent b37f201 commit c8909db

1 file changed

Lines changed: 97 additions & 0 deletions

File tree

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Soap\Encoding\Test\Unit\Cache;
5+
6+
use PHPUnit\Framework\Attributes\CoversClass;
7+
use PHPUnit\Framework\TestCase;
8+
use Soap\Encoding\Cache\ScopedCache;
9+
use stdClass;
10+
11+
#[CoversClass(ScopedCache::class)]
12+
final class ScopedCacheTest extends TestCase
13+
{
14+
public function test_it_returns_cached_value_on_hit(): void
15+
{
16+
/** @var ScopedCache<stdClass, string> */
17+
$cache = new ScopedCache();
18+
$scope = new stdClass();
19+
20+
$result1 = $cache->lookup($scope, 'key', static fn () => 'built');
21+
$result2 = $cache->lookup($scope, 'key', static fn () => 'should not be called');
22+
23+
static::assertSame('built', $result1);
24+
static::assertSame('built', $result2);
25+
}
26+
27+
public function test_it_calls_factory_on_miss(): void
28+
{
29+
/** @var ScopedCache<stdClass, string> */
30+
$cache = new ScopedCache();
31+
$scope = new stdClass();
32+
33+
$calls = 0;
34+
$cache->lookup($scope, 'a', static function () use (&$calls) {
35+
$calls++;
36+
37+
return 'value';
38+
});
39+
$cache->lookup($scope, 'b', static function () use (&$calls) {
40+
$calls++;
41+
42+
return 'other';
43+
});
44+
45+
static::assertSame(2, $calls);
46+
}
47+
48+
public function test_it_separates_keys_within_same_scope(): void
49+
{
50+
/** @var ScopedCache<stdClass, string> */
51+
$cache = new ScopedCache();
52+
$scope = new stdClass();
53+
54+
$a = $cache->lookup($scope, 'a', static fn () => 'alpha');
55+
$b = $cache->lookup($scope, 'b', static fn () => 'beta');
56+
57+
static::assertSame('alpha', $a);
58+
static::assertSame('beta', $b);
59+
}
60+
61+
public function test_it_separates_scopes(): void
62+
{
63+
/** @var ScopedCache<stdClass, string> */
64+
$cache = new ScopedCache();
65+
$scope1 = new stdClass();
66+
$scope2 = new stdClass();
67+
68+
$a = $cache->lookup($scope1, 'key', static fn () => 'from scope 1');
69+
$b = $cache->lookup($scope2, 'key', static fn () => 'from scope 2');
70+
71+
static::assertSame('from scope 1', $a);
72+
static::assertSame('from scope 2', $b);
73+
}
74+
75+
public function test_it_releases_entries_when_scope_is_garbage_collected(): void
76+
{
77+
/** @var ScopedCache<stdClass, string> */
78+
$cache = new ScopedCache();
79+
$scope = new stdClass();
80+
81+
$cache->lookup($scope, 'key', static fn () => str_repeat('x', 1024));
82+
83+
unset($scope);
84+
gc_collect_cycles();
85+
86+
// Create a new scope with a new key; factory must be called (no stale entry)
87+
$newScope = new stdClass();
88+
$calls = 0;
89+
$cache->lookup($newScope, 'key', static function () use (&$calls) {
90+
$calls++;
91+
92+
return 'fresh';
93+
});
94+
95+
static::assertSame(1, $calls);
96+
}
97+
}

0 commit comments

Comments
 (0)