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
38 changes: 35 additions & 3 deletions .github/workflows/run-tests-l10.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,57 @@ jobs:
tests:

runs-on: ubuntu-latest

services:
mongodb:
image: mongo:7
ports:
- 27017:27017
options: >-
--health-cmd="mongosh --quiet --eval 'db.runCommand({ping:1})'"
--health-interval=10s
--health-timeout=5s
--health-retries=3

strategy:
fail-fast: false
matrix:
php: [ 8.1 ]
laravel: [ 10.* ]
include:
- laravel: 10.*
testbench: 10.*

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

steps:
- name: Checkout code
uses: actions/checkout@v2
uses: actions/checkout@v3

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
extensions: pdo, sqlite, pdo_sqlite
extensions: pdo, sqlite, pdo_sqlite, mongodb
coverage: none

- name: Get composer cache directory
id: composer-cache
run: echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT

- name: Cache dependencies
uses: actions/cache@v3
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
restore-keys: ${{ runner.os }}-composer-

- name: Install Dependencies
run: composer install
run: composer install --prefer-dist --no-interaction --no-progress

- name: Execute tests
run: vendor/bin/phpunit
env:
MONGODB_HOST: 127.0.0.1
MONGODB_PORT: 27017
MONGODB_DATABASE: laravel_session_test
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/vendor
composer.lock
composer.lock
.phpunit.cache
68 changes: 68 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,71 @@ Advantages

Enjoy!
------

## Testing

This package includes a comprehensive test suite to ensure the MongoDB session handler works correctly. The tests cover:

1. Basic session operations (read, write, destroy)
2. Integration with Laravel's session system
3. HTTP session functionality
4. Laravel's testing helpers integration

### Running the Tests

To run the tests, follow these steps:

1. Make sure MongoDB is installed and running on your system
2. Install the package dependencies with Composer:

```bash
composer install
```

3. Run the tests with PHPUnit:

```bash
vendor/bin/phpunit
```

### Continuous Integration

The package includes a GitHub Actions workflow that automatically runs tests on PHP 8.1 with Laravel 10.x against MongoDB 7. The workflow:

1. Sets up a MongoDB service container
2. Installs PHP with MongoDB extension
3. Caches Composer dependencies for faster builds
4. Runs the test suite

This ensures all tests pass before merging new changes.

### Expected Test Results

When all tests are passing, you should see output similar to:

```
PHPUnit 10.x.x by Sebastian Bergmann and contributors.

............... 15 / 15 (100%)

Time: 00:00.444, Memory: 32.00 MB

OK (15 tests, 41 assertions)
```

### Testing Environments

The tests are compatible with:

- PHP 8.1+
- Laravel 10.x
- MongoDB 4.0+

### Test Coverage

- **Unit Tests**: These test the `MongoDbSessionHandler` methods directly (open, close, read, write, destroy, gc)
- **Feature Tests**: These test the integration with Laravel's session functionality
- **HTTP Tests**: These test session handling in HTTP requests and session persistence
- **Laravel Helper Tests**: These test integration with Laravel's testing helpers like `withSession` and `flushSession`

If you encounter any issues with the tests, please submit an issue on the GitHub repository.
13 changes: 12 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@
"name": "1ff/laravel-mongodb-session",
"description": "A mongodb session driver for laravel",
"type": "library",
"version": "5.0.4",
"require": {
"php": "^8.1",
"illuminate/session": "^10.0",
"mongodb/laravel-mongodb": "^4.0",
"ext-mongodb": "*"
},
"require-dev": {
"phpunit/phpunit": "^10.0",
"orchestra/testbench": "^8.0"
},
"license": "MIT",
"authors": [
{
Expand All @@ -26,11 +29,19 @@
"ForFit\\Session\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"ForFit\\Session\\Tests\\": "tests"
}
},
"extra": {
"laravel": {
"providers": [
"ForFit\\Session\\SessionServiceProvider"
]
}
},
"scripts": {
"test": "vendor/bin/phpunit"
}
}
27 changes: 27 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
bootstrap="vendor/autoload.php"
backupGlobals="false"
colors="true"
processIsolation="false"
stopOnFailure="false"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.0/phpunit.xsd"
cacheDirectory=".phpunit.cache"
backupStaticProperties="false"
>
<testsuites>
<testsuite name="Unit">
<directory suffix="Test.php">./tests/Unit</directory>
</testsuite>
<testsuite name="Feature">
<directory suffix="Test.php">./tests/Feature</directory>
</testsuite>
</testsuites>
<php>
<env name="APP_ENV" value="testing"/>
<env name="MONGODB_HOST" value="127.0.0.1"/>
<env name="MONGODB_PORT" value="27017"/>
<env name="MONGODB_DATABASE" value="laravel_session_test"/>
</php>
</phpunit>
24 changes: 6 additions & 18 deletions src/Console/Commands/MongodbSessionDropIndex.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,42 +4,30 @@

use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use \MongoDB\Driver\ReadPreference;
use MongoDB\Driver\ReadPreference;

/**
* Drop the indexes created by MongodbSessionIndex
*/
class MongodbSessionDropIndex extends Command
{

/**
* The name and signature of the console command.
*
* @var string
*/
/** The name and signature of the console command. */
protected $signature = 'mongodb:session:dropindex {index}';

/**
* The console command description.
*
* @var string
*/
/** The console command description. */
protected $description = 'Drops the passed index from the mongodb `sessions` collection';

/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
/** Execute the console command. */
public function handle(): void
{
$collection = config('session.table');

DB::connection('mongodb')->getMongoDB()->command([
'dropIndexes' => $collection,
'index' => $this->argument('index'),
], [
'readPreference' => new ReadPreference(ReadPreference::RP_PRIMARY)
'readPreference' => new ReadPreference(ReadPreference::PRIMARY)
]);
}
}
24 changes: 6 additions & 18 deletions src/Console/Commands/MongodbSessionIndex.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,22 @@

use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use \MongoDB\Driver\ReadPreference;
use MongoDB\Driver\ReadPreference;

/**
* Create indexes for the Session collection
*/
class MongodbSessionIndex extends Command
{

/**
* The name and signature of the console command.
*
* @var string
*/
/** The name and signature of the console command. */
protected $signature = 'mongodb:session:index';

/**
* The console command description.
*
* @var string
*/
/** The console command description. */
protected $description = 'Create indexes on the mongodb `sessions` collection';

/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
/** Execute the console command. */
public function handle(): void
{
$collection = config('session.table');

Expand All @@ -46,7 +34,7 @@ public function handle()
]
]
], [
'readPreference' => new ReadPreference(ReadPreference::RP_PRIMARY)
'readPreference' => new ReadPreference(ReadPreference::PRIMARY)
]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class IndexMongodbSessionsCollection extends Migration
*
* @return void
*/
public function up()
public function up(): void
{
Artisan::call('mongodb:session:index');
}
Expand All @@ -21,7 +21,7 @@ public function up()
*
* @return void
*/
public function down()
public function down(): void
{
Artisan::call('mongodb:session:dropindex', ['index' => 'expires_at_ttl']);
}
Expand Down
Loading