forked from mongodb/laravel-mongodb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSessionTest.php
94 lines (72 loc) · 2.88 KB
/
SessionTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<?php
namespace MongoDB\Laravel\Tests;
use Illuminate\Session\DatabaseSessionHandler;
use Illuminate\Session\SessionManager;
use Illuminate\Support\Facades\DB;
use MongoDB\Laravel\Session\MongoDbSessionHandler;
use PHPUnit\Framework\Attributes\TestWith;
use SessionHandlerInterface;
class SessionTest extends TestCase
{
protected function tearDown(): void
{
DB::connection('mongodb')->getCollection('sessions')->drop();
parent::tearDown();
}
/** @param class-string<SessionHandlerInterface> $class */
#[TestWith([DatabaseSessionHandler::class])]
#[TestWith([MongoDbSessionHandler::class])]
public function testSessionHandlerFunctionality(string $class)
{
$handler = new $class(
$this->app['db']->connection('mongodb'),
'sessions',
10,
);
$sessionId = '123';
$handler->write($sessionId, 'foo');
$this->assertEquals('foo', $handler->read($sessionId));
$handler->write($sessionId, 'bar');
$this->assertEquals('bar', $handler->read($sessionId));
$handler->destroy($sessionId);
$this->assertEmpty($handler->read($sessionId));
$handler->write($sessionId, 'bar');
$handler->gc(-1);
$this->assertEmpty($handler->read($sessionId));
}
public function testDatabaseSessionHandlerRegistration()
{
$this->app['config']->set('session.driver', 'database');
$this->app['config']->set('session.connection', 'mongodb');
$session = $this->app['session'];
$this->assertInstanceOf(SessionManager::class, $session);
$this->assertInstanceOf(DatabaseSessionHandler::class, $session->getHandler());
$this->assertSessionCanStoreInMongoDB($session);
}
public function testMongoDBSessionHandlerRegistration()
{
$this->app['config']->set('session.driver', 'mongodb');
$this->app['config']->set('session.connection', 'mongodb');
$session = $this->app['session'];
$this->assertInstanceOf(SessionManager::class, $session);
$this->assertInstanceOf(MongoDbSessionHandler::class, $session->getHandler());
$this->assertSessionCanStoreInMongoDB($session);
}
private function assertSessionCanStoreInMongoDB(SessionManager $session): void
{
$session->put('foo', 'bar');
$session->save();
$this->assertNotNull($session->getId());
$data = DB::connection('mongodb')
->getCollection('sessions')
->findOne(['_id' => $session->getId()]);
self::assertIsObject($data);
self::assertSame($session->getId(), $data->_id);
$session->remove('foo');
$data = DB::connection('mongodb')
->getCollection('sessions')
->findOne(['_id' => $session->getId()]);
self::assertIsObject($data);
self::assertSame($session->getId(), $data->_id);
}
}