33namespace MongoDB \Laravel \Tests ;
44
55use Illuminate \Session \DatabaseSessionHandler ;
6+ use Illuminate \Session \SessionManager ;
67use Illuminate \Support \Facades \DB ;
8+ use Symfony \Component \HttpFoundation \Session \Storage \Handler \MongoDbSessionHandler ;
79
810class SessionTest extends TestCase
911{
@@ -14,7 +16,7 @@ protected function tearDown(): void
1416 parent ::tearDown ();
1517 }
1618
17- public function testDatabaseSessionHandler ()
19+ public function testDatabaseSessionHandlerCompatibility ()
1820 {
1921 $ sessionId = '123 ' ;
2022
@@ -30,4 +32,43 @@ public function testDatabaseSessionHandler()
3032 $ handler ->write ($ sessionId , 'bar ' );
3133 $ this ->assertEquals ('bar ' , $ handler ->read ($ sessionId ));
3234 }
35+
36+ public function testDatabaseSessionHandlerRegistration ()
37+ {
38+ $ this ->app ['config ' ]->set ('session.driver ' , 'database ' );
39+ $ this ->app ['config ' ]->set ('session.connection ' , 'mongodb ' );
40+
41+ $ session = $ this ->app ['session ' ];
42+ $ this ->assertInstanceOf (SessionManager::class, $ session );
43+ $ this ->assertInstanceOf (DatabaseSessionHandler::class, $ session ->getHandler ());
44+
45+ $ this ->assertSessionCanStoreInMongoDB ($ session );
46+ }
47+
48+ public function testMongoDBSessionHandlerRegistration ()
49+ {
50+ $ this ->app ['config ' ]->set ('session.driver ' , 'mongodb ' );
51+ $ this ->app ['config ' ]->set ('session.connection ' , 'mongodb ' );
52+
53+ $ session = $ this ->app ['session ' ];
54+ $ this ->assertInstanceOf (SessionManager::class, $ session );
55+ $ this ->assertInstanceOf (MongoDbSessionHandler::class, $ session ->getHandler ());
56+
57+ $ this ->assertSessionCanStoreInMongoDB ($ session );
58+ }
59+
60+ private function assertSessionCanStoreInMongoDB (SessionManager $ session ): void
61+ {
62+ $ session ->put ('foo ' , 'bar ' );
63+ $ session ->save ();
64+
65+ $ this ->assertNotNull ($ session ->getId ());
66+
67+ $ data = DB ::connection ('mongodb ' )
68+ ->getCollection ('sessions ' )
69+ ->findOne (['_id ' => $ session ->getId ()]);
70+
71+ self ::assertIsObject ($ data );
72+ self ::assertSame ($ session ->getId (), $ data ->_id );
73+ }
3374}
0 commit comments