Skip to content

Commit ff11c63

Browse files
committed
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 799e257 commit ff11c63

15 files changed

+669
-474
lines changed

README.md

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

6868
php artisan mongodb:cache:dropindex
6969

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

composer.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}
Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
<?php
2+
3+
namespace Tests;
4+
5+
use ForFit\Mongodb\Cache\Store;
6+
use Illuminate\Support\Carbon;
7+
use Illuminate\Support\Facades\DB;
8+
use PHPUnit\Framework\Attributes\RequiresPhpExtension;
9+
use PHPUnit\Framework\Attributes\Test;
10+
11+
#[RequiresPhpExtension('mongodb')]
12+
class AdvancedCacheFeaturesTest extends TestCase
13+
{
14+
protected Store $store;
15+
16+
protected function setUp(): void
17+
{
18+
parent::setUp();
19+
20+
// Setup the store with a real connection
21+
$this->store = new Store(
22+
DB::connection('mongodb'),
23+
$this->table()
24+
);
25+
26+
// Freeze time for consistent testing
27+
Carbon::setTestNow(now());
28+
}
29+
30+
protected function tearDown(): void
31+
{
32+
parent::tearDown();
33+
Carbon::setTestNow(); // Clear test now
34+
}
35+
36+
#[Test]
37+
public function it_can_increment_numeric_values(): void
38+
{
39+
// Arrange
40+
$this->store->put('counter', 5, 60);
41+
42+
// Act
43+
$result = $this->store->increment('counter', 3);
44+
45+
// Assert
46+
$this->assertEquals(8, $result);
47+
$this->assertEquals(8, $this->store->get('counter'));
48+
}
49+
50+
#[Test]
51+
public function it_can_decrement_numeric_values(): void
52+
{
53+
// Arrange
54+
$this->store->put('counter', 10, 60);
55+
56+
// Act
57+
$result = $this->store->decrement('counter', 3);
58+
59+
// Assert
60+
$this->assertEquals(7, $result);
61+
$this->assertEquals(7, $this->store->get('counter'));
62+
}
63+
64+
#[Test]
65+
public function it_returns_false_when_incrementing_non_existent_key(): void
66+
{
67+
// Act
68+
$result = $this->store->increment('non-existent', 1);
69+
70+
// Assert
71+
$this->assertFalse($result);
72+
}
73+
74+
#[Test]
75+
public function it_can_store_items_forever(): void
76+
{
77+
// Act
78+
$result = $this->store->forever('permanent-key', 'permanent-value');
79+
80+
// Assert
81+
$this->assertTrue($result);
82+
$this->assertEquals('permanent-value', $this->store->get('permanent-key'));
83+
84+
// Verify long expiration time (10 years minimum)
85+
$expiration = $this->store->getExpiration('permanent-key');
86+
$this->assertNotNull($expiration);
87+
$this->assertGreaterThanOrEqual(315360000, $expiration); // >= 10 years
88+
}
89+
90+
#[Test]
91+
public function it_can_store_and_retrieve_arrays(): void
92+
{
93+
// Arrange
94+
$data = ['name' => 'Test', 'values' => [1, 2, 3]];
95+
96+
// Act
97+
$this->store->put('array-data', $data, 60);
98+
99+
// Assert
100+
$result = $this->store->get('array-data');
101+
$this->assertEquals($data, $result);
102+
$this->assertIsArray($result);
103+
$this->assertEquals([1, 2, 3], $result['values']);
104+
}
105+
106+
#[Test]
107+
public function it_can_store_and_retrieve_objects(): void
108+
{
109+
// Arrange
110+
$data = new \stdClass();
111+
$data->name = 'Test Object';
112+
$data->value = 123;
113+
114+
// Act
115+
$this->store->put('object-data', $data, 60);
116+
117+
// Assert
118+
$result = $this->store->get('object-data');
119+
$this->assertEquals($data, $result);
120+
$this->assertInstanceOf(\stdClass::class, $result);
121+
$this->assertEquals('Test Object', $result->name);
122+
$this->assertEquals(123, $result->value);
123+
}
124+
125+
#[Test]
126+
public function it_can_forget_specific_keys(): void
127+
{
128+
// Arrange
129+
$this->store->put('forget-me', 'value', 60);
130+
$this->store->put('keep-me', 'value', 60);
131+
132+
// Assert item exists before forgetting
133+
$this->assertEquals('value', $this->store->get('forget-me'));
134+
135+
// Act
136+
$result = $this->store->forget('forget-me');
137+
138+
// Assert
139+
$this->assertTrue($result);
140+
$this->assertNull($this->store->get('forget-me'));
141+
$this->assertEquals('value', $this->store->get('keep-me'));
142+
}
143+
144+
#[Test]
145+
public function it_can_flush_entire_cache(): void
146+
{
147+
// Arrange
148+
$this->store->put('key1', 'value1', 60);
149+
$this->store->put('key2', 'value2', 60);
150+
$this->store->tags(['tag1'])->put('key3', 'value3', 60);
151+
152+
// Act
153+
$result = $this->store->flush();
154+
155+
// Assert
156+
$this->assertTrue($result);
157+
$this->assertNull($this->store->get('key1'));
158+
$this->assertNull($this->store->get('key2'));
159+
$this->assertNull($this->store->get('key3'));
160+
161+
// Verify MongoDB collection is empty
162+
$count = DB::connection('mongodb')
163+
->table($this->table())
164+
->count();
165+
166+
$this->assertEquals(0, $count);
167+
}
168+
}

0 commit comments

Comments
 (0)