Skip to content

v7.x-update-mongodb-package #42

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: v7.x
Choose a base branch
from
Open
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
37 changes: 33 additions & 4 deletions .github/workflows/run-tests-l11.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,58 @@ 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.2, 8.3 ]
laravel: [ 11.* ]
include:
- laravel: 11.*
testbench: 8.*
testbench: 9.*

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_mongodb_cache_test
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
/vendor/
composer.lock
.phpunit.result.cache
.phpunit.cache
2 changes: 1 addition & 1 deletion .phprc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
php8.1
php8.2
53 changes: 53 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,59 @@ Advantages

php artisan mongodb:cache:dropindex

Testing
-------

This package includes tests that interact with a real MongoDB database to verify the functionality of the cache driver. The tests require a MongoDB instance to run successfully.

To run the tests:

1. Make sure you have MongoDB installed and running on your local machine
2. The test configuration is set in `phpunit.xml`:

```xml
<php>
<env name="MONGODB_HOST" value="127.0.0.1"/>
<env name="MONGODB_PORT" value="27017"/>
<env name="MONGODB_DATABASE" value="laravel_mongodb_cache_test"/>
<env name="MONGODB_USERNAME" value=""/>
<env name="MONGODB_PASSWORD" value=""/>
</php>
```

3. Run the tests with:

```
composer test
```

or

```
vendor/bin/phpunit
```

### Test Structure

The test suite is organized into multiple files to test various aspects of the MongoDB cache driver:

- `StoreTest.php`: Tests basic Store class functionality (get, put, forget, flush)
- `AdvancedCacheFeaturesTest.php`: Tests advanced Store features like increment/decrement, forever storage, and handling arrays/objects
- `TaggedCacheTest.php`: Tests tagged cache functionality
- `LaravelIntegrationTest.php`: Tests integration with Laravel's Cache facade

Some functionality (like increment/decrement, forever storage) is intentionally tested in multiple contexts:
1. At the low-level Store implementation
2. Through Laravel's Cache facade
3. With tagged cache operations

This multi-layered approach ensures that all feature functionality works correctly at all levels of integration.

GitHub Actions
-------------

The package includes GitHub Actions workflows that automatically run tests against a MongoDB service. The MongoDB service is started as part of the CI workflow, ensuring tests are executed in an environment with a real MongoDB database.

Warning
-------

Expand Down
7 changes: 5 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
"name": "1ff/laravel-mongodb-cache",
"description": "A mongodb cache driver for laravel",
"type": "library",
"version": "7.0.0",
"require": {
"php": "^8.2|^8.3",
"illuminate/cache": "^11.0",
"mongodb/laravel-mongodb": "^4.1"
"mongodb/laravel-mongodb": "^5.0",
"ext-mongodb": "*"
},
"require-dev": {
"orchestra/testbench": "^9.0"
Expand Down Expand Up @@ -39,5 +39,8 @@
"ForFit\\Mongodb\\Cache\\ServiceProvider"
]
}
},
"scripts": {
"test": "vendor/bin/phpunit"
}
}
22 changes: 9 additions & 13 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
bootstrap="vendor/autoload.php"
backupGlobals="false"
backupStaticAttributes="false"
colors="true"
verbose="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"
>
<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.5/phpunit.xsd" cacheDirectory=".phpunit.cache"
backupStaticProperties="false">
<testsuites>
<testsuite name="MongoDB Cache Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>
<php>
<env name="CACHE_DRIVER" value="array"/>
<env name="MONGODB_HOST" value="127.0.0.1"/>
<env name="MONGODB_PORT" value="27017"/>
<env name="MONGODB_DATABASE" value="laravel_mongodb_cache_test"/>
<env name="MONGODB_USERNAME" value=""/>
<env name="MONGODB_PASSWORD" value=""/>
</php>
</phpunit>
26 changes: 7 additions & 19 deletions src/Console/Commands/MongodbCacheDropIndex.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 MongodbCacheIndex
*/
class MongodbCacheDropIndex extends Command
{

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

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

/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
/** Execute the console command. */
public function handle(): void
{
$cacheCollectionName = config('cache')['stores']['mongodb']['table'];

DB::connection('mongodb')->getMongoDB()->command([
DB::connection('mongodb')->getDatabase()->command([
'dropIndexes' => $cacheCollectionName,
'index' => $this->argument('index'),
], [
'readPreference' => new ReadPreference(ReadPreference::RP_PRIMARY)
'readPreference' => new ReadPreference(ReadPreference::PRIMARY)
]);
}
}
26 changes: 7 additions & 19 deletions src/Console/Commands/MongodbCacheIndex.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,26 @@

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

/**
* Create indexes for the cache collection
*/
class MongodbCacheIndex extends Command
{

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

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

/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
/** Execute the console command. */
public function handle(): void
{
$cacheCollectionName = config('cache')['stores']['mongodb']['table'];

DB::connection('mongodb')->getMongoDB()->command([
DB::connection('mongodb')->getDatabase()->command([
'createIndexes' => $cacheCollectionName,
'indexes' => [
[
Expand All @@ -52,7 +40,7 @@ public function handle()
]
]
], [
'readPreference' => new ReadPreference(ReadPreference::RP_PRIMARY)
'readPreference' => new ReadPreference(ReadPreference::PRIMARY)
]);
}
}
26 changes: 7 additions & 19 deletions src/Console/Commands/MongodbCacheIndexTags.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,26 @@

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

/**
* Create indexes for the cache collection
*/
class MongodbCacheIndexTags extends Command
{

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

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

/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
/** Execute the console command.*/
public function handle(): void
{
$cacheCollectionName = config('cache')['stores']['mongodb']['table'];

DB::connection('mongodb')->getMongoDB()->command([
DB::connection('mongodb')->getDatabase()->command([
'createIndexes' => $cacheCollectionName,
'indexes' => [
[
Expand All @@ -45,7 +33,7 @@ public function handle()
],
]
], [
'readPreference' => new ReadPreference(ReadPreference::RP_PRIMARY)
'readPreference' => new ReadPreference(ReadPreference::PRIMARY)
]);
}
}
Loading