mongodb/mongodb driver. It replaces real MongoDB collections with in-memory mocks, enabling isolated, deterministic testing without external dependencies.insertOne, updateOne, deleteOne, find, findOneAndUpdate, etc.) and query operators ($set, $inc, $push, $all, $exists, regex, etc.). This aligns well with 80% of use cases for Laravel applications interacting with MongoDB via repositories or services.MongoDB facade, Illuminate\Database\Eloquent\Model for MongoDB) could be partially mocked by replacing MongoDB\Collection instances. However, full ORM integration (e.g., MongoDB\Eloquent) would need additional effort.public function __construct(private MockCollection $mockCollection) {}
phpunit.xml configuration. No conflicts with Laravel’s testing utilities (e.g., RefreshDatabase traits).mongodb/mongodb driver’s BSON types, ensuring type safety in tests. However, custom BSON extensions (e.g., MongoDB\BSON\UTCDateTime) may not be fully supported.MongoDB\Database and MongoDB\Collection, but higher-level abstractions (e.g., Laravel’s MongoDB facade) would need manual overrides in tests._id, default indexing) may not match production. Mitigation: Explicitly configure mocks to replicate production logic.beforeEach/afterEach hooks.mongodb/mongodb queries? Eloquent may require custom test doubles for relationships.Tests\TestCase, DatabaseMigrations).MongoDB\Collection.Model layer instead (e.g., using Laravel’s createMock).phpunit.xml and Pest PHP (if using PHPUnit).RefreshDatabase for hybrid tests (real DB for some tests, mocks for others).MockCollection.// Before: Real DB
public function testUserCreation() {
$user = User::create([...]);
$this->assertDatabaseHas('users', [...]);
}
// After: Mocked
public function testUserCreation() {
$mockCollection = new MockCollection();
$mockCollection->insertOne([...]);
$this->assertEquals([...], $mockCollection->findOne([...]));
}
MongoDB\Collection in service classes (e.g., UserService).$service = new UserService(new MockCollection());
$this->app->instance(MockCollection::class, new MockCollection());
phpunit.xml configuration:
<env name="MOCK_MONGODB" value="true" />
MongoDB\Collection.MongoDB facade (requires overriding the facade in tests).ObjectId, Date, Binary). Custom types may need manual handling.assertContains, assertInstanceOf) via the package’s extended filter support.$lookup), fork the package or create a wrapper.$group").$match operator).How can I help you explore Laravel packages today?