graviton/mongodb-fixtures-bundle
A Symfony bundle for loading MongoDB fixtures into your applications. It integrates with Doctrine MongoDB ODM to seed test and development databases with sample data and supports fixture ordering and dependency resolution so collections stay consistent across environments.
First, the framework reality check. Despite being told to approach this as a Laravel developer, this is a Symfony bundle (Bundle subclass, config/bundles.php registration, Doctrine MongoDB ODM). Nothing here maps to Eloquent, migrations, or php artisan. The conceptual equivalents are useful — fixtures ≈ seeders, DocumentManager ≈ Eloquent's connection manager, purge-between-tests ≈ RefreshDatabase — but the mechanics are Symfony's. If you are actually in Laravel, this package is not usable as-is; you would reach for a Doctrine MongoDB ODM integration and hand-roll fixture loading instead.
Assuming a Symfony app with doctrine/mongodb-odm-bundle and mapped documents:
composer require --dev graviton/mongodb-fixtures-bundle
config/bundles.php (typically only in test/dev envs).doctrine_mongodb connection and at least one mapped Document, because every fixture class must be a mapped ODM document.composer.json for the exact Symfony/ODM version constraints, then src/ for the trait (likely something like MongoDBFixturesTrait) and any base test case it ships, then the repo's tests/ directory to see the actual fixture file format. Because the package advertises being dependency-free (no nelmio/alice, no fzaninotto/faker), the fixture format is almost certainly plain PHP arrays rather than Alice YAML — verify this before writing fixtures.setUp(), assert a query returns what you expect. That one loop tells you 80% of what the package does.Base test case + trait. The intended pattern is a trait mixed into a KernelTestCase/WebTestCase. Put it in an abstract MongoDbTestCase so every functional test inherits loading and cleanup:
abstract class MongoDbTestCase extends WebTestCase
{
use MongoDBFixturesTrait;
protected function setUp(): void
{
parent::setUp();
static::bootKernel();
$this->loadFixtures(['users.php', 'orders.php']);
}
protected function tearDown(): void
{
$this->purgeCollections(); // or whatever the trait names it
parent::tearDown();
}
}
Fixture files as data. Expect each file to return an array of document data (or documents) keyed by the mapped class. Keep them small and purpose-built per test class — users_basic.php, users_with_orders.php — rather than one giant seed file. Because there is no Faker, centralise reusable array builders in a FixtureFactory helper (plain PHP methods returning arrays) so you don't copy-paste email addresses and IDs.
Load per-test when scenarios diverge. Override setUp in subclasses or call loadFixtures([...]) inside individual test methods for variations. Avoid loading everything globally via PHPUnit bootstrap; per-test loading keeps failures attributable.
Purge before, not just after. MongoDB collections persist across test runs if a prior run crashed. Purge in setUp (idempotent starting state) as well as tearDown, or use a dedicated test database name per environment and drop it entirely at suite start.
Web/client tests. Boot the kernel after fixtures are loaded if your test client boots its own kernel; or load fixtures via the container's document manager and reuse that same manager. Mixing two document manager instances across two kernels is a classic source of "fixtures are there but the test can't see them."
Relations. If your documents use DBRefs or embedded documents, the loader generally persists in file order. Order fixtures so referenced documents come first, and give documents explicit IDs in the fixture arrays so you can hard-code references without querying for the generated ID.
@DisableDatabaseCache), Alice-based YAML, and ORM support. This one is MongoDB-only and dependency-free, so anything Alice-shaped will not work. Read the source rather than assuming API parity; with zero stars and zero dependents, the README is the only spec you get.deleteMany([]) semantics, or re-run index creation after purge.RefreshDatabase with a transaction rollback, isolation here is real writes to a real database. Any test that crashes mid-run leaves data behind — hence purge-on-setup. On a replica set you could use transactions, but a fixture bundle that's "dependency free" is unlikely to wire that up for you.paratest or process isolation against the same database name produces flaky, order-dependent failures. Use a per-process database suffix (test_db_%s with getmypid() or TEST_TOKEN) in the connection config.#[Field(name: ...)]).new \DateTime() rather than hard-coded strings.composer update can break you silently. Use an exact constraint ("graviton/mongodb-fixtures-bundle": "1.2.3") and skim the diff before bumping.tests/Fixtures/Mongo rather than whatever default it assumes), and whether the trait exposes a hook to substitute a custom document manager — useful if you run multiple ODM connections.doctrine/mongodb-odm-bundle ships a data-fixtures command (doctrine:mongodb:fixtures:load), and hautelook/alice-bundle or doctrine/data-fixtures gives you a maintained path for the same job. Since this bundle's value is "small and dependency-free," a 30-line trait of your own that loads PHP arrays into the document manager is a credible replacement if maintenance stalls.How can I help you explore Laravel packages today?