55namespace Mcp \Server \Tests \Unit \Session ;
66
77use Mcp \Server \Session \CacheSessionHandler ;
8- use Mockery ;
98use PHPUnit \Framework \TestCase ;
109use Psr \Clock \ClockInterface ;
1110use Psr \SimpleCache \CacheInterface ;
@@ -16,25 +15,6 @@ final class CacheSessionHandlerTest extends TestCase
1615 private ClockInterface $ clock ;
1716 private CacheSessionHandler $ handler ;
1817
19- protected function setUp (): void
20- {
21- $ this ->cache = Mockery::mock (CacheInterface::class);
22- $ this ->clock = Mockery::mock (ClockInterface::class);
23-
24- // Mock the initial session index retrieval in constructor
25- $ this ->cache ->shouldReceive ('get ' )
26- ->with ('mcp_session_index ' , [])
27- ->andReturn ([])
28- ->once ();
29-
30- $ this ->handler = new CacheSessionHandler ($ this ->cache , ttl: 3600 , clock: $ this ->clock );
31- }
32-
33- protected function tearDown (): void
34- {
35- Mockery::close ();
36- }
37-
3818 public function test_read_returns_false_when_session_not_exists (): void
3919 {
4020 $ this ->cache ->shouldReceive ('get ' )->with ('non-existent-id ' , false )->andReturn (false );
@@ -47,7 +27,7 @@ public function test_read_returns_false_when_session_not_exists(): void
4727 public function test_read_removes_expired_session_from_index (): void
4828 {
4929 $ sessionId = 'expired-session ' ;
50-
30+
5131 // Session exists in cache but not in index
5232 $ this ->cache ->shouldReceive ('get ' )->with ($ sessionId , false )->andReturn (false );
5333 $ this ->cache ->shouldReceive ('set ' )->with ('mcp_session_index ' , [])->andReturn (true );
@@ -209,7 +189,7 @@ public function test_gc_returns_empty_array_when_no_expired_sessions(): void
209189
210190 public function test_constructor_with_default_ttl (): void
211191 {
212- $ cache = Mockery::mock (CacheInterface::class);
192+ $ cache = \ Mockery::mock (CacheInterface::class);
213193 $ cache ->shouldReceive ('get ' )->with ('mcp_session_index ' , [])->andReturn ([]);
214194
215195 $ handler = new CacheSessionHandler ($ cache );
@@ -219,7 +199,7 @@ public function test_constructor_with_default_ttl(): void
219199
220200 public function test_constructor_with_custom_ttl (): void
221201 {
222- $ cache = Mockery::mock (CacheInterface::class);
202+ $ cache = \ Mockery::mock (CacheInterface::class);
223203 $ cache ->shouldReceive ('get ' )->with ('mcp_session_index ' , [])->andReturn ([]);
224204
225205 $ handler = new CacheSessionHandler ($ cache , ttl: 7200 );
@@ -248,7 +228,7 @@ public function test_multiple_operations_maintain_index_consistency(): void
248228 $ this ->cache ->shouldReceive ('set ' )
249229 ->with ('mcp_session_index ' , [
250230 $ sessionId1 => $ currentTime ->getTimestamp (),
251- $ sessionId2 => $ currentTime ->getTimestamp ()
231+ $ sessionId2 => $ currentTime ->getTimestamp (),
252232 ])
253233 ->andReturn (true );
254234 $ this ->cache ->shouldReceive ('set ' )->with ($ sessionId2 , $ sessionData2 )->andReturn (true );
@@ -263,4 +243,23 @@ public function test_multiple_operations_maintain_index_consistency(): void
263243
264244 $ this ->assertTrue ($ result );
265245 }
246+
247+ protected function setUp (): void
248+ {
249+ $ this ->cache = \Mockery::mock (CacheInterface::class);
250+ $ this ->clock = \Mockery::mock (ClockInterface::class);
251+
252+ // Mock the initial session index retrieval in constructor
253+ $ this ->cache ->shouldReceive ('get ' )
254+ ->with ('mcp_session_index ' , [])
255+ ->andReturn ([])
256+ ->once ();
257+
258+ $ this ->handler = new CacheSessionHandler ($ this ->cache , ttl: 3600 , clock: $ this ->clock );
259+ }
260+
261+ protected function tearDown (): void
262+ {
263+ \Mockery::close ();
264+ }
266265}
0 commit comments