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

Commit b15a77e

Browse files
authored
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
1 parent ec7be50 commit b15a77e

13 files changed

+601
-43
lines changed

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

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,57 @@ 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: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +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",
98
"mongodb/laravel-mongodb": "^4.0",
109
"ext-mongodb": "*"
1110
},
11+
"require-dev": {
12+
"phpunit/phpunit": "^10.0",
13+
"orchestra/testbench": "^8.0"
14+
},
1215
"license": "MIT",
1316
"authors": [
1417
{
@@ -26,11 +29,19 @@
2629
"ForFit\\Session\\": "src"
2730
}
2831
},
32+
"autoload-dev": {
33+
"psr-4": {
34+
"ForFit\\Session\\Tests\\": "tests"
35+
}
36+
},
2937
"extra": {
3038
"laravel": {
3139
"providers": [
3240
"ForFit\\Session\\SessionServiceProvider"
3341
]
3442
}
43+
},
44+
"scripts": {
45+
"test": "vendor/bin/phpunit"
3546
}
3647
}

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: 6 additions & 18 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

3826
DB::connection('mongodb')->getMongoDB()->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: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,34 +4,22 @@
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

@@ -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
}

0 commit comments

Comments
 (0)