Skip to content
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
91 changes: 60 additions & 31 deletions .github/workflows/run-tests-l10.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,67 @@ name: "Run Tests - Laravel 10"

on:
push:
branches: [ v6.x, master ]
branches: [ v6.x ]

pull_request:
branches: [ v6.x, master ]
branches: [ v6.x ]

jobs:
tests:

runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
php: [8.1]
laravel: [10.*]
include:
- laravel: 10.*
testbench: 8.*

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

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

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

- name: Install Dependencies
run: composer install

- name: Execute tests
run: vendor/bin/phpunit
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@v3

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
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 --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
53 changes: 53 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,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
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"require": {
"php": "^8.1",
"illuminate/cache": "^10.0",
"mongodb/laravel-mongodb": "^4.1",
"mongodb/laravel-mongodb": "^5.0",
"ext-mongodb": "*"
},
"require-dev": {
Expand Down Expand Up @@ -39,5 +39,8 @@
"ForFit\\Mongodb\\Cache\\ServiceProvider"
]
}
},
"scripts": {
"test": "vendor/bin/phpunit"
}
}
5 changes: 5 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,10 @@
</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>
2 changes: 1 addition & 1 deletion src/Console/Commands/MongodbCacheDropIndex.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ 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'),
], [
Expand Down
2 changes: 1 addition & 1 deletion src/Console/Commands/MongodbCacheIndex.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function handle(): void
{
$cacheCollectionName = config('cache')['stores']['mongodb']['table'];

DB::connection('mongodb')->getMongoDB()->command([
DB::connection('mongodb')->getDatabase()->command([
'createIndexes' => $cacheCollectionName,
'indexes' => [
[
Expand Down
2 changes: 1 addition & 1 deletion src/Console/Commands/MongodbCacheIndexTags.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function handle(): void
{
$cacheCollectionName = config('cache')['stores']['mongodb']['table'];

DB::connection('mongodb')->getMongoDB()->command([
DB::connection('mongodb')->getDatabase()->command([
'createIndexes' => $cacheCollectionName,
'indexes' => [
[
Expand Down
8 changes: 4 additions & 4 deletions src/Store.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
use Illuminate\Contracts\Cache\Store as StoreInterface;
use Illuminate\Database\ConnectionInterface;
use Illuminate\Support\InteractsWithTime;
use Jenssegers\Mongodb\Query\Builder;
use MongoDB\BSON\UTCDateTime;
use MongoDB\Driver\Exception\BulkWriteException;
use MongoDB\Laravel\Query\Builder;

class Store implements StoreInterface
{
Expand Down Expand Up @@ -59,7 +59,7 @@ public function get($key)
{
$cacheData = $this->table()->where('key', $this->getKeyWithPrefix($key))->first();

return $cacheData ? unserialize($cacheData['value']) : null;
return $cacheData ? unserialize($cacheData->value) : null;
}

/**
Expand Down Expand Up @@ -165,11 +165,11 @@ public function getExpiration($key): ?float
{
$cacheData = $this->table()->where('key', $this->getKeyWithPrefix($key))->first();

if (empty($cacheData['expiration'])) {
if (empty($cacheData->expiration)) {
return null;
}

$expirationSeconds = $cacheData['expiration']->toDateTime()->getTimestamp();
$expirationSeconds = $cacheData->expiration->toDateTime()->getTimestamp();

return round($expirationSeconds - $this->currentTime());
}
Expand Down
Loading