3
3
namespace MongoDB \Laravel \Tests ;
4
4
5
5
use Illuminate \Session \DatabaseSessionHandler ;
6
+ use Illuminate \Session \SessionManager ;
6
7
use Illuminate \Support \Facades \DB ;
8
+ use Symfony \Component \HttpFoundation \Session \Storage \Handler \MongoDbSessionHandler ;
7
9
8
10
class SessionTest extends TestCase
9
11
{
@@ -14,7 +16,7 @@ protected function tearDown(): void
14
16
parent ::tearDown ();
15
17
}
16
18
17
- public function testDatabaseSessionHandler ()
19
+ public function testDatabaseSessionHandlerCompatibility ()
18
20
{
19
21
$ sessionId = '123 ' ;
20
22
@@ -30,4 +32,43 @@ public function testDatabaseSessionHandler()
30
32
$ handler ->write ($ sessionId , 'bar ' );
31
33
$ this ->assertEquals ('bar ' , $ handler ->read ($ sessionId ));
32
34
}
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
+ }
33
74
}
0 commit comments