Skip to content
This repository was archived by the owner on Jul 31, 2025. It is now read-only.

Commit 0ec6933

Browse files
committed
Update mongodb/laravel-mongodb package to version 5.0 in composer.json (#27)
* Update mongodb/laravel-mongodb package to version 5.0 in composer.json * Update MongoDB commands and improve session handling Refactor the command to use `getDatabase()` instead of `getMongoDB()`. Update payload access in `MongoDbSessionHandler` from array syntax to object syntax for better readability. Adjust test assertions to match the new access methodology and ensure compatibility with Carbon instances for expiration and last activity timestamps. Clean up formatting inconsistencies throughout the code. * Update MongoDB connection method to use getDatabase() instead of getMongoDB() in MongodbSessionIndex command
1 parent 49965c9 commit 0ec6933

File tree

3 files changed

+34
-31
lines changed

3 files changed

+34
-31
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"require": {
66
"php": "^8.1",
77
"illuminate/session": "^10.0",
8-
"mongodb/laravel-mongodb": "^4.0",
8+
"mongodb/laravel-mongodb": "^5.0",
99
"ext-mongodb": "*"
1010
},
1111
"require-dev": {

src/MongoDbSessionHandler.php

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,23 @@
22

33
namespace ForFit\Session;
44

5-
use Illuminate\Database\ConnectionInterface;
6-
use Illuminate\Database\Query\Builder;
75
use MongoDB\BSON\Binary;
86
use MongoDB\BSON\UTCDateTime;
97
use MongoDB\Driver\Exception\BulkWriteException;
108
use SessionHandlerInterface;
119

1210
class MongoDbSessionHandler implements SessionHandlerInterface
1311
{
14-
protected ConnectionInterface $connection;
15-
protected int $minutes;
16-
protected string $table;
12+
protected $connection;
13+
protected $minutes;
14+
protected $table;
1715

18-
public function __construct(ConnectionInterface $connection, string $table = 'sessions', int $minutes = 60)
16+
/**
17+
* @param \Illuminate\Database\ConnectionInterface $connection
18+
* @param string $table
19+
* @param integer $minutes
20+
*/
21+
public function __construct($connection, $table = 'sessions', $minutes = 60)
1922
{
2023
$this->connection = $connection;
2124
$this->minutes = (int)$minutes;
@@ -86,7 +89,7 @@ public function gc($max_lifetime): false|int
8689
* Returns the query builder
8790
*
8891
*/
89-
protected function query(): Builder
92+
protected function query()
9093
{
9194
return $this->connection->table($this->table);
9295
}
@@ -97,7 +100,7 @@ protected function query(): Builder
97100
* @param string|null $data
98101
* @return array
99102
*/
100-
protected function buildPayload($data): array
103+
protected function buildPayload($data)
101104
{
102105
return [
103106
'payload' => new Binary($data, Binary::TYPE_OLD_BINARY),

tests/Unit/MongoDbSessionHandlerTest.php

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,106 +4,106 @@
44

55
use ForFit\Session\MongoDbSessionHandler;
66
use ForFit\Session\Tests\TestCase;
7+
use Illuminate\Support\Carbon;
78
use MongoDB\BSON\Binary;
8-
use MongoDB\BSON\UTCDateTime;
99

1010
class MongoDbSessionHandlerTest extends TestCase
1111
{
1212
/**
1313
* @var MongoDbSessionHandler
1414
*/
1515
protected $handler;
16-
16+
1717
/**
1818
* @var string
1919
*/
2020
protected $sessionId;
21-
21+
2222
/**
2323
* Set up the test environment.
2424
*/
2525
protected function setUp(): void
2626
{
2727
parent::setUp();
28-
28+
2929
$this->handler = $this->app['session.store']->getHandler();
3030
$this->sessionId = md5(uniqid('test_session'));
3131
}
32-
32+
3333
/**
3434
* Test the open method
3535
*/
3636
public function test_open_method(): void
3737
{
3838
$this->assertTrue($this->handler->open('path', 'name'));
3939
}
40-
40+
4141
/**
4242
* Test the close method
4343
*/
4444
public function test_close_method(): void
4545
{
4646
$this->assertTrue($this->handler->close());
4747
}
48-
48+
4949
/**
5050
* Test reading non-existent session
5151
*/
5252
public function test_read_non_existent_session(): void
5353
{
5454
$this->assertEquals('', $this->handler->read('non_existent_id'));
5555
}
56-
56+
5757
/**
5858
* Test write and read session
5959
*/
6060
public function test_write_and_read_session(): void
6161
{
6262
$data = 'test_data_' . time();
63-
63+
6464
// Write session data
6565
$this->assertTrue($this->handler->write($this->sessionId, $data));
66-
66+
6767
// Read it back
6868
$readData = $this->handler->read($this->sessionId);
69-
69+
7070
$this->assertEquals($data, $readData);
71-
71+
7272
// Check database directly
7373
$session = $this->app['db']->table(config('session.table'))
7474
->where('_id', $this->sessionId)
7575
->first();
76-
76+
7777
$this->assertNotNull($session);
78-
$this->assertInstanceOf(Binary::class, $session['payload']);
79-
$this->assertInstanceOf(UTCDateTime::class, $session['expires_at']);
80-
$this->assertInstanceOf(UTCDateTime::class, $session['last_activity']);
78+
$this->assertInstanceOf(Binary::class, $session->payload);
79+
$this->assertInstanceOf(Carbon::class, $session->expires_at);
80+
$this->assertInstanceOf(Carbon::class, $session->last_activity);
8181
}
82-
82+
8383
/**
8484
* Test destroy session
8585
*/
8686
public function test_destroy_session(): void
8787
{
8888
// First write a session
8989
$this->handler->write($this->sessionId, 'test_data');
90-
90+
9191
// Verify it exists
9292
$exists = $this->app['db']->table(config('session.table'))
9393
->where('_id', $this->sessionId)
9494
->exists();
9595
$this->assertTrue($exists);
96-
96+
9797
// Now destroy it
9898
$this->assertTrue($this->handler->destroy($this->sessionId));
99-
99+
100100
// Verify it's gone
101101
$exists = $this->app['db']->table(config('session.table'))
102102
->where('_id', $this->sessionId)
103103
->exists();
104104
$this->assertFalse($exists);
105105
}
106-
106+
107107
/**
108108
* Test garbage collection
109109
*/
@@ -112,4 +112,4 @@ public function test_garbage_collection(): void
112112
// gc should return a truthy value as it's handled by MongoDB TTL index
113113
$this->assertNotFalse($this->handler->gc(100));
114114
}
115-
}
115+
}

0 commit comments

Comments
 (0)