Skip to content

Commit 143879b

Browse files
authored
Update mongodb/laravel-mongodb package to version 5.0 in composer.json (#40)
* Refactor StoreTest to use attributes for test annotations, update PHPUnit configuration and improve code type hints. Adjust .gitignore for PHPUnit cache directory and add ext-mongodb requirement in composer.json. * Stop using deprecated RP_PRIMARY and replace it with PRIMARY in MongoDB read preferences. * Update mongodb/laravel-mongodb package to version 5.0 * Add test script to composer.json for PHPUnit execution * Update MongoDB package to ensure compatibility with v5 Refactor `first` method in Builder to handle object and array scenarios effectively. Adjust queries in Store and command files to use `getDatabase()` instead of `getMongoDB()`. Update property accessors for cache data to support object dereferencing. * Remove deprecated MongoDB builder and helper classes, and refactor tests to use direct database connections. Update GitHub Actions to ensure MongoDB service is available during testing. * Update MongoDB package and enhance test suite - Add PHP attribute to require the MongoDB extension in tests. - Refactor the `TestCase` class for improved environment setup. - Introduce new test classes: `AdvancedCacheFeaturesTest`, `TaggedCacheTest`, and `LaravelIntegrationTest` to cover various aspects of the MongoDB cache driver. - Implement tests for incrementing, decrementing, storing arrays/objects, and tagged cache functionality. - Document test structure in README for clarity on test organization and coverage.
1 parent 7ae697c commit 143879b

16 files changed

+730
-506
lines changed

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

Lines changed: 60 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,67 @@ name: "Run Tests - Laravel 10"
22

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

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

1010
jobs:
11-
tests:
12-
13-
runs-on: ubuntu-latest
14-
strategy:
15-
fail-fast: false
16-
matrix:
17-
php: [8.1]
18-
laravel: [10.*]
19-
include:
20-
- laravel: 10.*
21-
testbench: 8.*
22-
23-
name: P${{ matrix.php }} - L${{ matrix.laravel }}
24-
25-
steps:
26-
- name: Checkout code
27-
uses: actions/checkout@v2
28-
29-
- name: Setup PHP
30-
uses: shivammathur/setup-php@v2
31-
with:
32-
php-version: ${{ matrix.php }}
33-
extensions: pdo, sqlite, pdo_sqlite
34-
35-
- name: Install Dependencies
36-
run: composer install
37-
38-
- name: Execute tests
39-
run: vendor/bin/phpunit
11+
tests:
12+
13+
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+
26+
strategy:
27+
fail-fast: false
28+
matrix:
29+
php: [ 8.1 ]
30+
laravel: [ 10.* ]
31+
include:
32+
- laravel: 10.*
33+
testbench: 10.*
34+
35+
name: P${{ matrix.php }} - L${{ matrix.laravel }}
36+
37+
steps:
38+
- name: Checkout code
39+
uses: actions/checkout@v3
40+
41+
- name: Setup PHP
42+
uses: shivammathur/setup-php@v2
43+
with:
44+
php-version: ${{ matrix.php }}
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-
58+
59+
- name: Install Dependencies
60+
run: composer install --prefer-dist --no-interaction --no-progress
61+
62+
63+
- name: Execute tests
64+
run: vendor/bin/phpunit
65+
env:
66+
MONGODB_HOST: 127.0.0.1
67+
MONGODB_PORT: 27017
68+
MONGODB_DATABASE: laravel_mongodb_cache_test

README.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,59 @@ Advantages
6666

6767
php artisan mongodb:cache:dropindex
6868

69+
Testing
70+
-------
71+
72+
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.
73+
74+
To run the tests:
75+
76+
1. Make sure you have MongoDB installed and running on your local machine
77+
2. The test configuration is set in `phpunit.xml`:
78+
79+
```xml
80+
<php>
81+
<env name="MONGODB_HOST" value="127.0.0.1"/>
82+
<env name="MONGODB_PORT" value="27017"/>
83+
<env name="MONGODB_DATABASE" value="laravel_mongodb_cache_test"/>
84+
<env name="MONGODB_USERNAME" value=""/>
85+
<env name="MONGODB_PASSWORD" value=""/>
86+
</php>
87+
```
88+
89+
3. Run the tests with:
90+
91+
```
92+
composer test
93+
```
94+
95+
or
96+
97+
```
98+
vendor/bin/phpunit
99+
```
100+
101+
### Test Structure
102+
103+
The test suite is organized into multiple files to test various aspects of the MongoDB cache driver:
104+
105+
- `StoreTest.php`: Tests basic Store class functionality (get, put, forget, flush)
106+
- `AdvancedCacheFeaturesTest.php`: Tests advanced Store features like increment/decrement, forever storage, and handling arrays/objects
107+
- `TaggedCacheTest.php`: Tests tagged cache functionality
108+
- `LaravelIntegrationTest.php`: Tests integration with Laravel's Cache facade
109+
110+
Some functionality (like increment/decrement, forever storage) is intentionally tested in multiple contexts:
111+
1. At the low-level Store implementation
112+
2. Through Laravel's Cache facade
113+
3. With tagged cache operations
114+
115+
This multi-layered approach ensures that all feature functionality works correctly at all levels of integration.
116+
117+
GitHub Actions
118+
-------------
119+
120+
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.
121+
69122
Warning
70123
-------
71124

composer.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"require": {
66
"php": "^8.1",
77
"illuminate/cache": "^10.0",
8-
"mongodb/laravel-mongodb": "^4.1",
8+
"mongodb/laravel-mongodb": "^5.0",
99
"ext-mongodb": "*"
1010
},
1111
"require-dev": {
@@ -39,5 +39,8 @@
3939
"ForFit\\Mongodb\\Cache\\ServiceProvider"
4040
]
4141
}
42+
},
43+
"scripts": {
44+
"test": "vendor/bin/phpunit"
4245
}
4346
}

phpunit.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,10 @@
1010
</testsuites>
1111
<php>
1212
<env name="CACHE_DRIVER" value="array"/>
13+
<env name="MONGODB_HOST" value="127.0.0.1"/>
14+
<env name="MONGODB_PORT" value="27017"/>
15+
<env name="MONGODB_DATABASE" value="laravel_mongodb_cache_test"/>
16+
<env name="MONGODB_USERNAME" value=""/>
17+
<env name="MONGODB_PASSWORD" value=""/>
1318
</php>
1419
</phpunit>

src/Console/Commands/MongodbCacheDropIndex.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public function handle(): void
2323
{
2424
$cacheCollectionName = config('cache')['stores']['mongodb']['table'];
2525

26-
DB::connection('mongodb')->getMongoDB()->command([
26+
DB::connection('mongodb')->getDatabase()->command([
2727
'dropIndexes' => $cacheCollectionName,
2828
'index' => $this->argument('index'),
2929
], [

src/Console/Commands/MongodbCacheIndex.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public function handle(): void
2323
{
2424
$cacheCollectionName = config('cache')['stores']['mongodb']['table'];
2525

26-
DB::connection('mongodb')->getMongoDB()->command([
26+
DB::connection('mongodb')->getDatabase()->command([
2727
'createIndexes' => $cacheCollectionName,
2828
'indexes' => [
2929
[

src/Console/Commands/MongodbCacheIndexTags.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public function handle(): void
2323
{
2424
$cacheCollectionName = config('cache')['stores']['mongodb']['table'];
2525

26-
DB::connection('mongodb')->getMongoDB()->command([
26+
DB::connection('mongodb')->getDatabase()->command([
2727
'createIndexes' => $cacheCollectionName,
2828
'indexes' => [
2929
[

src/Store.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
use Illuminate\Contracts\Cache\Store as StoreInterface;
88
use Illuminate\Database\ConnectionInterface;
99
use Illuminate\Support\InteractsWithTime;
10-
use Jenssegers\Mongodb\Query\Builder;
1110
use MongoDB\BSON\UTCDateTime;
1211
use MongoDB\Driver\Exception\BulkWriteException;
12+
use MongoDB\Laravel\Query\Builder;
1313

1414
class Store implements StoreInterface
1515
{
@@ -59,7 +59,7 @@ public function get($key)
5959
{
6060
$cacheData = $this->table()->where('key', $this->getKeyWithPrefix($key))->first();
6161

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

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

168-
if (empty($cacheData['expiration'])) {
168+
if (empty($cacheData->expiration)) {
169169
return null;
170170
}
171171

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

174174
return round($expirationSeconds - $this->currentTime());
175175
}

0 commit comments

Comments
 (0)