Skip to content
This repository was archived by the owner on Jul 31, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
2 changes: 1 addition & 1 deletion src/Console/Commands/MongodbSessionDropIndex.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
], [
Expand Down
2 changes: 1 addition & 1 deletion src/Console/Commands/MongodbSessionIndex.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => [
[
Expand Down
10 changes: 5 additions & 5 deletions src/MongoDbSessionHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
}

Expand All @@ -48,7 +48,7 @@ public function read($id): false|string
{
$session = $this->query()->find($id);

return $session ? $session['payload'] : '';
return $session ? $session->payload : '';
}

/**
Expand All @@ -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) {
Expand Down
44 changes: 22 additions & 22 deletions tests/Unit/MongoDbSessionHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,106 +4,106 @@

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
{
/**
* @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
*/
public function test_open_method(): void
{
$this->assertTrue($this->handler->open('path', 'name'));
}

/**
* Test the close method
*/
public function test_close_method(): void
{
$this->assertTrue($this->handler->close());
}

/**
* Test reading non-existent session
*/
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
*/
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
*/
Expand All @@ -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));
}
}
}