@@ -96,17 +96,10 @@ public function getCompletions(string $currentValue, SessionInterface $session):
9696 expect (getPrivateProperty ($ this ->builder , 'logger ' ))->toBe ($ logger );
9797});
9898
99- it ('sets cache and TTL correctly ' , function () {
100- $ cache = Mockery::mock (CacheInterface::class);
101- $ this ->builder ->withCache ($ cache , 1800 );
102- expect (getPrivateProperty ($ this ->builder , 'cache ' ))->toBe ($ cache );
103- expect (getPrivateProperty ($ this ->builder , 'definitionCacheTtl ' ))->toBe (1800 );
104- });
105-
106- it ('sets cache with default TTL if TTL not provided ' , function () {
99+ it ('sets cache correctly ' , function () {
107100 $ cache = Mockery::mock (CacheInterface::class);
108101 $ this ->builder ->withCache ($ cache );
109- expect (getPrivateProperty ($ this ->builder , 'definitionCacheTtl ' ))->toBe (3600 );
102+ expect (getPrivateProperty ($ this ->builder , 'cache ' ))->toBe ($ cache );
110103});
111104
112105it ('sets session handler correctly ' , function () {
@@ -116,22 +109,86 @@ public function getCompletions(string $currentValue, SessionInterface $session):
116109 expect (getPrivateProperty ($ this ->builder , 'sessionTtl ' ))->toBe (7200 );
117110});
118111
119- it ('sets ArraySessionHandler correctly ' , function () {
120- $ this ->builder ->withArraySessionHandler (1800 );
121- expect (getPrivateProperty ($ this ->builder , 'sessionHandler ' ))->toBeInstanceOf (ArraySessionHandler::class);
122- expect (getPrivateProperty ($ this ->builder , 'sessionHandler ' )->ttl )->toBe (1800 );
112+ it ('sets session driver to array correctly ' , function () {
113+ $ this ->builder ->withSession ('array ' , 1800 );
114+ expect (getPrivateProperty ($ this ->builder , 'sessionDriver ' ))->toBe ('array ' );
123115 expect (getPrivateProperty ($ this ->builder , 'sessionTtl ' ))->toBe (1800 );
124116});
125117
126- it ('sets CacheSessionHandler correctly ' , function () {
118+ it ('sets session driver to cache correctly ' , function () {
119+ $ this ->builder ->withSession ('cache ' , 900 );
120+ expect (getPrivateProperty ($ this ->builder , 'sessionDriver ' ))->toBe ('cache ' );
121+ expect (getPrivateProperty ($ this ->builder , 'sessionTtl ' ))->toBe (900 );
122+ });
123+
124+ it ('uses default TTL when not specified for session ' , function () {
125+ $ this ->builder ->withSession ('array ' );
126+ expect (getPrivateProperty ($ this ->builder , 'sessionTtl ' ))->toBe (3600 );
127+ });
128+
129+ it ('throws exception for invalid session driver ' , function () {
130+ $ this ->builder ->withSession ('redis ' );
131+ })->throws (\InvalidArgumentException::class, "Unsupported session driver 'redis'. Only 'array' and 'cache' drivers are supported. " );
132+
133+ it ('throws exception for cache session driver without cache during build ' , function () {
134+ $ this ->builder
135+ ->withServerInfo ('Test ' , '1.0 ' )
136+ ->withSession ('cache ' )
137+ ->build ();
138+ })->throws (ConfigurationException::class, 'Cache session driver requires a cache instance ' );
139+
140+ it ('creates ArraySessionHandler when array driver is specified ' , function () {
141+ $ server = $ this ->builder
142+ ->withServerInfo ('Test ' , '1.0 ' )
143+ ->withSession ('array ' , 1800 )
144+ ->build ();
145+
146+ $ sessionManager = $ server ->getSessionManager ();
147+ $ smReflection = new ReflectionClass (SessionManager::class);
148+ $ handlerProp = $ smReflection ->getProperty ('handler ' );
149+ $ handlerProp ->setAccessible (true );
150+ $ handler = $ handlerProp ->getValue ($ sessionManager );
151+
152+ expect ($ handler )->toBeInstanceOf (ArraySessionHandler::class);
153+ expect ($ handler ->ttl )->toBe (1800 );
154+ });
155+
156+ it ('creates CacheSessionHandler when cache driver is specified ' , function () {
127157 $ cache = Mockery::mock (CacheInterface::class);
128158 $ cache ->shouldReceive ('get ' )->with ('mcp_session_index ' , [])->andReturn ([]);
129- $ this ->builder ->withCacheSessionHandler ($ cache , 900 );
130- $ sessionHandler = getPrivateProperty ($ this ->builder , 'sessionHandler ' );
131- expect ($ sessionHandler )->toBeInstanceOf (CacheSessionHandler::class);
132- expect ($ sessionHandler ->cache )->toBe ($ cache );
133- expect ($ sessionHandler ->ttl )->toBe (900 );
134- expect (getPrivateProperty ($ this ->builder , 'sessionTtl ' ))->toBe (900 );
159+
160+ $ server = $ this ->builder
161+ ->withServerInfo ('Test ' , '1.0 ' )
162+ ->withCache ($ cache )
163+ ->withSession ('cache ' , 900 )
164+ ->build ();
165+
166+ $ sessionManager = $ server ->getSessionManager ();
167+ $ smReflection = new ReflectionClass (SessionManager::class);
168+ $ handlerProp = $ smReflection ->getProperty ('handler ' );
169+ $ handlerProp ->setAccessible (true );
170+ $ handler = $ handlerProp ->getValue ($ sessionManager );
171+
172+ expect ($ handler )->toBeInstanceOf (CacheSessionHandler::class);
173+ expect ($ handler ->cache )->toBe ($ cache );
174+ expect ($ handler ->ttl )->toBe (900 );
175+ });
176+
177+ it ('prefers custom session handler over session driver ' , function () {
178+ $ customHandler = Mockery::mock (SessionHandlerInterface::class);
179+
180+ $ server = $ this ->builder
181+ ->withServerInfo ('Test ' , '1.0 ' )
182+ ->withSession ('array ' )
183+ ->withSessionHandler ($ customHandler , 1200 )
184+ ->build ();
185+
186+ $ sessionManager = $ server ->getSessionManager ();
187+ $ smReflection = new ReflectionClass (SessionManager::class);
188+ $ handlerProp = $ smReflection ->getProperty ('handler ' );
189+ $ handlerProp ->setAccessible (true );
190+
191+ expect ($ handlerProp ->getValue ($ sessionManager ))->toBe ($ customHandler );
135192});
136193
137194
0 commit comments