From 67db865a60e843bb467e494a0d15a25f32cd984c Mon Sep 17 00:00:00 2001 From: Ivelin Ivanov <9060426+ivosgem@users.noreply.github.com> Date: Wed, 23 Apr 2025 14:44:59 +0300 Subject: [PATCH 1/3] Update mongodb/laravel-mongodb package to version 5.0 in composer.json --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 459bdda..6ef049a 100644 --- a/composer.json +++ b/composer.json @@ -5,7 +5,7 @@ "require": { "php": "^8.1", "illuminate/session": "^10.0", - "mongodb/laravel-mongodb": "^4.0", + "mongodb/laravel-mongodb": "^5.0", "ext-mongodb": "*" }, "require-dev": { From 16eedf24e07f00a9b51ead85ebd2bf1bea9c8861 Mon Sep 17 00:00:00 2001 From: Ivelin Ivanov <9060426+ivosgem@users.noreply.github.com> Date: Fri, 25 Apr 2025 19:02:16 +0300 Subject: [PATCH 2/3] 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. --- .../Commands/MongodbSessionDropIndex.php | 2 +- src/MongoDbSessionHandler.php | 10 ++--- tests/Unit/MongoDbSessionHandlerTest.php | 44 +++++++++---------- 3 files changed, 28 insertions(+), 28 deletions(-) diff --git a/src/Console/Commands/MongodbSessionDropIndex.php b/src/Console/Commands/MongodbSessionDropIndex.php index d506777..17eaf94 100644 --- a/src/Console/Commands/MongodbSessionDropIndex.php +++ b/src/Console/Commands/MongodbSessionDropIndex.php @@ -23,7 +23,7 @@ public function handle(): void { $collection = config('session.table'); - DB::connection('mongodb')->getMongoDB()->command([ + DB::connection('mongodb')->getDatabase()->command([ 'dropIndexes' => $collection, 'index' => $this->argument('index'), ], [ diff --git a/src/MongoDbSessionHandler.php b/src/MongoDbSessionHandler.php index ae34554..507fc46 100644 --- a/src/MongoDbSessionHandler.php +++ b/src/MongoDbSessionHandler.php @@ -2,8 +2,8 @@ namespace ForFit\Session; -use MongoDB\BSON\UTCDateTime; use MongoDB\BSON\Binary; +use MongoDB\BSON\UTCDateTime; use MongoDB\Driver\Exception\BulkWriteException; use SessionHandlerInterface; @@ -14,14 +14,14 @@ class MongoDbSessionHandler implements SessionHandlerInterface protected $table; /** - * @param \Illuminate\Database\ConnectionInterface $connection + * @param \Illuminate\Database\ConnectionInterface $connection * @param string $table * @param integer $minutes */ public function __construct($connection, $table = 'sessions', $minutes = 60) { $this->connection = $connection; - $this->minutes = (int) $minutes; + $this->minutes = (int)$minutes; $this->table = $table; } @@ -48,7 +48,7 @@ public function read($id): false|string { $session = $this->query()->find($id); - return $session ? $session['payload'] : ''; + return $session ? $session->payload : ''; } /** @@ -57,7 +57,7 @@ public function read($id): false|string public function write($id, $data): bool { try { - return (bool) $this->query() + return (bool)$this->query() ->where('_id', $id) ->update($this->buildPayload($data), ['upsert' => true]); } catch (BulkWriteException $exception) { diff --git a/tests/Unit/MongoDbSessionHandlerTest.php b/tests/Unit/MongoDbSessionHandlerTest.php index 2c2c784..a008442 100644 --- a/tests/Unit/MongoDbSessionHandlerTest.php +++ b/tests/Unit/MongoDbSessionHandlerTest.php @@ -4,8 +4,8 @@ use ForFit\Session\MongoDbSessionHandler; use ForFit\Session\Tests\TestCase; +use Illuminate\Support\Carbon; use MongoDB\BSON\Binary; -use MongoDB\BSON\UTCDateTime; class MongoDbSessionHandlerTest extends TestCase { @@ -13,23 +13,23 @@ class MongoDbSessionHandlerTest extends TestCase * @var MongoDbSessionHandler */ protected $handler; - + /** * @var string */ protected $sessionId; - + /** * Set up the test environment. */ protected function setUp(): void { parent::setUp(); - + $this->handler = $this->app['session.store']->getHandler(); $this->sessionId = md5(uniqid('test_session')); } - + /** * Test the open method */ @@ -37,7 +37,7 @@ public function test_open_method(): void { $this->assertTrue($this->handler->open('path', 'name')); } - + /** * Test the close method */ @@ -45,7 +45,7 @@ public function test_close_method(): void { $this->assertTrue($this->handler->close()); } - + /** * Test reading non-existent session */ @@ -53,33 +53,33 @@ public function test_read_non_existent_session(): void { $this->assertEquals('', $this->handler->read('non_existent_id')); } - + /** * Test write and read session */ public function test_write_and_read_session(): void { $data = 'test_data_' . time(); - + // Write session data $this->assertTrue($this->handler->write($this->sessionId, $data)); - + // Read it back $readData = $this->handler->read($this->sessionId); - + $this->assertEquals($data, $readData); - + // Check database directly $session = $this->app['db']->table(config('session.table')) ->where('_id', $this->sessionId) ->first(); - + $this->assertNotNull($session); - $this->assertInstanceOf(Binary::class, $session['payload']); - $this->assertInstanceOf(UTCDateTime::class, $session['expires_at']); - $this->assertInstanceOf(UTCDateTime::class, $session['last_activity']); + $this->assertInstanceOf(Binary::class, $session->payload); + $this->assertInstanceOf(Carbon::class, $session->expires_at); + $this->assertInstanceOf(Carbon::class, $session->last_activity); } - + /** * Test destroy session */ @@ -87,23 +87,23 @@ public function test_destroy_session(): void { // First write a session $this->handler->write($this->sessionId, 'test_data'); - + // Verify it exists $exists = $this->app['db']->table(config('session.table')) ->where('_id', $this->sessionId) ->exists(); $this->assertTrue($exists); - + // Now destroy it $this->assertTrue($this->handler->destroy($this->sessionId)); - + // Verify it's gone $exists = $this->app['db']->table(config('session.table')) ->where('_id', $this->sessionId) ->exists(); $this->assertFalse($exists); } - + /** * Test garbage collection */ @@ -112,4 +112,4 @@ public function test_garbage_collection(): void // gc should return a truthy value as it's handled by MongoDB TTL index $this->assertNotFalse($this->handler->gc(100)); } -} \ No newline at end of file +} From d5c09681adfc1da710b14737c3bca5a6275ae12d Mon Sep 17 00:00:00 2001 From: Ivelin Ivanov <9060426+ivosgem@users.noreply.github.com> Date: Mon, 28 Apr 2025 10:47:24 +0300 Subject: [PATCH 3/3] Update MongoDB connection method to use getDatabase() instead of getMongoDB() in MongodbSessionIndex command --- src/Console/Commands/MongodbSessionIndex.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Console/Commands/MongodbSessionIndex.php b/src/Console/Commands/MongodbSessionIndex.php index 4ee93be..8c29904 100644 --- a/src/Console/Commands/MongodbSessionIndex.php +++ b/src/Console/Commands/MongodbSessionIndex.php @@ -23,7 +23,7 @@ public function handle(): void { $collection = config('session.table'); - DB::connection('mongodb')->getMongoDB()->command([ + DB::connection('mongodb')->getDatabase()->command([ 'createIndexes' => $collection, 'indexes' => [ [