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": { 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/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' => [ [ 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 +}