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

Commit 1f354ed

Browse files
DevquasarX9spont4e
andauthored
V5.x to master (#29)
* Added support for Laravel 10.x (#15) * Updated mongodb/laravel-mongodb package (#16) * Refactor method signatures in MongoDbSessionHandler for clarity and type safety. Add .editorconfig for consistent coding style across files. Update composer.json to include ext-mongodb requirement. (#23) * V5.x rebase (#25) * v5.x to master (#17) * Added support for Laravel 10.x (#15) * Updated mongodb/laravel-mongodb package (#16) # Conflicts: # composer.json * Update laravel mongodb to rc1 (#18) * Added support for Laravel 10.x (#15) * Updated mongodb/laravel-mongodb package (#16) * Updated mongodb/laravel-mongodb to rc1. * Updated mongodb/laravel-mongodb to stable version 4. (#19) * Changed package version * Update deprecated MongoDB read preference constants to use current values (#28) * Update deprecated MongoDB read preference constants to use current values * Bump version to 5.0.5 in composer.json for updated dependencies * Update session testing suite, add MongoDB test cases, and configure PHPUnit - Enhance `.gitignore` to exclude `.phpunit.cache`. - Update GitHub workflow to include Testbench version for Laravel 10.x. - Create new feature tests: `TestHelperIntegrationTest`, `HttpSessionTest`, and `MongoDbSessionHandlerTest` to validate session handling with MongoDB. - Refactor MongoDB session command files to remove unnecessary docblocks. - Document testing process in `README.md`, including setup and expected results. - Add basic PHPUnit configuration file. - Define session environment in the base `TestCase`. - Ensure session operations like reading, writing, and destruction are tested against MongoDB. * Update GitHub Actions workflow to use MongoDB 7 and improve caching - Add MongoDB 7 service to CI workflow - Upgrade checkout action to v3 - Include MongoDB extension in PHP setup - Implement Composer dependency caching for faster builds - Modify composer install command for better performance - Set environment variables for MongoDB connection during test execution - Update README.md to reflect changes in the CI process * Remove deprecated MongoDB version constraint from composer.json * 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 * Update GitHub Actions workflow to trigger on the master branch --------- Co-authored-by: Ivaylo Mutafov <[email protected]>
1 parent f484ac3 commit 1f354ed

14 files changed

+611
-54
lines changed

.github/workflows/run-tests-l10.yml

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,66 @@ name: "Run Tests - Laravel 10"
22

33
on:
44
push:
5-
branches: [ v5.x ]
5+
branches: [ v5.x, master ]
66

77
pull_request:
8-
branches: [ v5.x ]
8+
branches: [ v5.x, master ]
99

1010
jobs:
1111
tests:
1212

1313
runs-on: ubuntu-latest
14+
15+
services:
16+
mongodb:
17+
image: mongo:7
18+
ports:
19+
- 27017:27017
20+
options: >-
21+
--health-cmd="mongosh --quiet --eval 'db.runCommand({ping:1})'"
22+
--health-interval=10s
23+
--health-timeout=5s
24+
--health-retries=3
25+
1426
strategy:
1527
fail-fast: false
1628
matrix:
1729
php: [ 8.1 ]
1830
laravel: [ 10.* ]
1931
include:
2032
- laravel: 10.*
33+
testbench: 10.*
2134

2235
name: P${{ matrix.php }} - L${{ matrix.laravel }}
2336

2437
steps:
2538
- name: Checkout code
26-
uses: actions/checkout@v2
39+
uses: actions/checkout@v3
2740

2841
- name: Setup PHP
2942
uses: shivammathur/setup-php@v2
3043
with:
3144
php-version: ${{ matrix.php }}
32-
extensions: pdo, sqlite, pdo_sqlite
45+
extensions: pdo, sqlite, pdo_sqlite, mongodb
46+
coverage: none
47+
48+
- name: Get composer cache directory
49+
id: composer-cache
50+
run: echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT
51+
52+
- name: Cache dependencies
53+
uses: actions/cache@v3
54+
with:
55+
path: ${{ steps.composer-cache.outputs.dir }}
56+
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
57+
restore-keys: ${{ runner.os }}-composer-
3358

3459
- name: Install Dependencies
35-
run: composer install
60+
run: composer install --prefer-dist --no-interaction --no-progress
61+
62+
- name: Execute tests
63+
run: vendor/bin/phpunit
64+
env:
65+
MONGODB_HOST: 127.0.0.1
66+
MONGODB_PORT: 27017
67+
MONGODB_DATABASE: laravel_session_test

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
/vendor
2-
composer.lock
2+
composer.lock
3+
.phpunit.cache

README.md

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,71 @@ Advantages
3434

3535
Enjoy!
3636
------
37+
38+
## Testing
39+
40+
This package includes a comprehensive test suite to ensure the MongoDB session handler works correctly. The tests cover:
41+
42+
1. Basic session operations (read, write, destroy)
43+
2. Integration with Laravel's session system
44+
3. HTTP session functionality
45+
4. Laravel's testing helpers integration
46+
47+
### Running the Tests
48+
49+
To run the tests, follow these steps:
50+
51+
1. Make sure MongoDB is installed and running on your system
52+
2. Install the package dependencies with Composer:
53+
54+
```bash
55+
composer install
56+
```
57+
58+
3. Run the tests with PHPUnit:
59+
60+
```bash
61+
vendor/bin/phpunit
62+
```
63+
64+
### Continuous Integration
65+
66+
The package includes a GitHub Actions workflow that automatically runs tests on PHP 8.1 with Laravel 10.x against MongoDB 7. The workflow:
67+
68+
1. Sets up a MongoDB service container
69+
2. Installs PHP with MongoDB extension
70+
3. Caches Composer dependencies for faster builds
71+
4. Runs the test suite
72+
73+
This ensures all tests pass before merging new changes.
74+
75+
### Expected Test Results
76+
77+
When all tests are passing, you should see output similar to:
78+
79+
```
80+
PHPUnit 10.x.x by Sebastian Bergmann and contributors.
81+
82+
............... 15 / 15 (100%)
83+
84+
Time: 00:00.444, Memory: 32.00 MB
85+
86+
OK (15 tests, 41 assertions)
87+
```
88+
89+
### Testing Environments
90+
91+
The tests are compatible with:
92+
93+
- PHP 8.1+
94+
- Laravel 10.x
95+
- MongoDB 4.0+
96+
97+
### Test Coverage
98+
99+
- **Unit Tests**: These test the `MongoDbSessionHandler` methods directly (open, close, read, write, destroy, gc)
100+
- **Feature Tests**: These test the integration with Laravel's session functionality
101+
- **HTTP Tests**: These test session handling in HTTP requests and session persistence
102+
- **Laravel Helper Tests**: These test integration with Laravel's testing helpers like `withSession` and `flushSession`
103+
104+
If you encounter any issues with the tests, please submit an issue on the GitHub repository.

composer.json

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@
22
"name": "1ff/laravel-mongodb-session",
33
"description": "A mongodb session driver for laravel",
44
"type": "library",
5-
"version": "5.0.4",
65
"require": {
76
"php": "^8.1",
87
"illuminate/session": "^10.0",
9-
"mongodb/laravel-mongodb": "^4.0",
8+
"mongodb/laravel-mongodb": "^5.0",
109
"ext-mongodb": "*"
1110
},
12-
11+
"require-dev": {
12+
"phpunit/phpunit": "^10.0",
13+
"orchestra/testbench": "^8.0"
14+
},
1315
"license": "MIT",
1416
"authors": [
1517
{
@@ -27,11 +29,19 @@
2729
"ForFit\\Session\\": "src"
2830
}
2931
},
32+
"autoload-dev": {
33+
"psr-4": {
34+
"ForFit\\Session\\Tests\\": "tests"
35+
}
36+
},
3037
"extra": {
3138
"laravel": {
3239
"providers": [
3340
"ForFit\\Session\\SessionServiceProvider"
3441
]
3542
}
43+
},
44+
"scripts": {
45+
"test": "vendor/bin/phpunit"
3646
}
3747
}

phpunit.xml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
bootstrap="vendor/autoload.php"
5+
backupGlobals="false"
6+
colors="true"
7+
processIsolation="false"
8+
stopOnFailure="false"
9+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.0/phpunit.xsd"
10+
cacheDirectory=".phpunit.cache"
11+
backupStaticProperties="false"
12+
>
13+
<testsuites>
14+
<testsuite name="Unit">
15+
<directory suffix="Test.php">./tests/Unit</directory>
16+
</testsuite>
17+
<testsuite name="Feature">
18+
<directory suffix="Test.php">./tests/Feature</directory>
19+
</testsuite>
20+
</testsuites>
21+
<php>
22+
<env name="APP_ENV" value="testing"/>
23+
<env name="MONGODB_HOST" value="127.0.0.1"/>
24+
<env name="MONGODB_PORT" value="27017"/>
25+
<env name="MONGODB_DATABASE" value="laravel_session_test"/>
26+
</php>
27+
</phpunit>

src/Console/Commands/MongodbSessionDropIndex.php

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,42 +4,30 @@
44

55
use Illuminate\Console\Command;
66
use Illuminate\Support\Facades\DB;
7-
use \MongoDB\Driver\ReadPreference;
7+
use MongoDB\Driver\ReadPreference;
88

99
/**
1010
* Drop the indexes created by MongodbSessionIndex
1111
*/
1212
class MongodbSessionDropIndex extends Command
1313
{
1414

15-
/**
16-
* The name and signature of the console command.
17-
*
18-
* @var string
19-
*/
15+
/** The name and signature of the console command. */
2016
protected $signature = 'mongodb:session:dropindex {index}';
2117

22-
/**
23-
* The console command description.
24-
*
25-
* @var string
26-
*/
18+
/** The console command description. */
2719
protected $description = 'Drops the passed index from the mongodb `sessions` collection';
2820

29-
/**
30-
* Execute the console command.
31-
*
32-
* @return mixed
33-
*/
34-
public function handle()
21+
/** Execute the console command. */
22+
public function handle(): void
3523
{
3624
$collection = config('session.table');
3725

38-
DB::connection('mongodb')->getMongoDB()->command([
26+
DB::connection('mongodb')->getDatabase()->command([
3927
'dropIndexes' => $collection,
4028
'index' => $this->argument('index'),
4129
], [
42-
'readPreference' => new ReadPreference(ReadPreference::RP_PRIMARY)
30+
'readPreference' => new ReadPreference(ReadPreference::PRIMARY)
4331
]);
4432
}
4533
}

src/Console/Commands/MongodbSessionIndex.php

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,38 +4,26 @@
44

55
use Illuminate\Console\Command;
66
use Illuminate\Support\Facades\DB;
7-
use \MongoDB\Driver\ReadPreference;
7+
use MongoDB\Driver\ReadPreference;
88

99
/**
1010
* Create indexes for the Session collection
1111
*/
1212
class MongodbSessionIndex extends Command
1313
{
1414

15-
/**
16-
* The name and signature of the console command.
17-
*
18-
* @var string
19-
*/
15+
/** The name and signature of the console command. */
2016
protected $signature = 'mongodb:session:index';
2117

22-
/**
23-
* The console command description.
24-
*
25-
* @var string
26-
*/
18+
/** The console command description. */
2719
protected $description = 'Create indexes on the mongodb `sessions` collection';
2820

29-
/**
30-
* Execute the console command.
31-
*
32-
* @return mixed
33-
*/
34-
public function handle()
21+
/** Execute the console command. */
22+
public function handle(): void
3523
{
3624
$collection = config('session.table');
3725

38-
DB::connection('mongodb')->getMongoDB()->command([
26+
DB::connection('mongodb')->getDatabase()->command([
3927
'createIndexes' => $collection,
4028
'indexes' => [
4129
[
@@ -46,7 +34,7 @@ public function handle()
4634
]
4735
]
4836
], [
49-
'readPreference' => new ReadPreference(ReadPreference::RP_PRIMARY)
37+
'readPreference' => new ReadPreference(ReadPreference::PRIMARY)
5038
]);
5139
}
5240
}

src/Database/Migrations/2019_08_01_000000_index_mongodb_sessions_collection.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class IndexMongodbSessionsCollection extends Migration
1111
*
1212
* @return void
1313
*/
14-
public function up()
14+
public function up(): void
1515
{
1616
Artisan::call('mongodb:session:index');
1717
}
@@ -21,7 +21,7 @@ public function up()
2121
*
2222
* @return void
2323
*/
24-
public function down()
24+
public function down(): void
2525
{
2626
Artisan::call('mongodb:session:dropindex', ['index' => 'expires_at_ttl']);
2727
}

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) {

0 commit comments

Comments
 (0)