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

Commit f584218

Browse files
committed
Update MongoDB commands and fix minor code style issues
- Replace `getMongoDB()` with `getDatabase()` in MongodbSessionDropIndex.php. - Adjust code formatting in MongoDbSessionHandler.php for consistency. - Refactor session access in MongoDbSessionHandlerTest.php from array syntax to object properties for improved clarity and readability.
1 parent 67db865 commit f584218

File tree

3 files changed

+27
-27
lines changed

3 files changed

+27
-27
lines changed

src/Console/Commands/MongodbSessionDropIndex.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public function handle(): void
2323
{
2424
$collection = config('session.table');
2525

26-
DB::connection('mongodb')->getMongoDB()->command([
26+
DB::connection('mongodb')->getDatabase()->command([
2727
'dropIndexes' => $collection,
2828
'index' => $this->argument('index'),
2929
], [

src/MongoDbSessionHandler.php

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

33
namespace ForFit\Session;
44

5-
use MongoDB\BSON\UTCDateTime;
65
use MongoDB\BSON\Binary;
6+
use MongoDB\BSON\UTCDateTime;
77
use MongoDB\Driver\Exception\BulkWriteException;
88
use SessionHandlerInterface;
99

@@ -14,14 +14,14 @@ class MongoDbSessionHandler implements SessionHandlerInterface
1414
protected $table;
1515

1616
/**
17-
* @param \Illuminate\Database\ConnectionInterface $connection
17+
* @param \Illuminate\Database\ConnectionInterface $connection
1818
* @param string $table
1919
* @param integer $minutes
2020
*/
2121
public function __construct($connection, $table = 'sessions', $minutes = 60)
2222
{
2323
$this->connection = $connection;
24-
$this->minutes = (int) $minutes;
24+
$this->minutes = (int)$minutes;
2525
$this->table = $table;
2626
}
2727

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

51-
return $session ? $session['payload'] : '';
51+
return $session ? $session->payload : '';
5252
}
5353

5454
/**
@@ -57,7 +57,7 @@ public function read($id): false|string
5757
public function write($id, $data): bool
5858
{
5959
try {
60-
return (bool) $this->query()
60+
return (bool)$this->query()
6161
->where('_id', $id)
6262
->update($this->buildPayload($data), ['upsert' => true]);
6363
} catch (BulkWriteException $exception) {

tests/Unit/MongoDbSessionHandlerTest.php

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,97 +13,97 @@ class MongoDbSessionHandlerTest extends TestCase
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(UTCDateTime::class, $session->expires_at);
80+
$this->assertInstanceOf(UTCDateTime::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)