- Can I use Zenstruck/Foundry with Laravel’s Eloquent models, or is it strictly for Doctrine?
- Foundry is primarily designed for Doctrine ORM/ODM, but you can wrap Eloquent models using custom factories. For full feature parity (like auto-refresh or relationship consistency), stick to Doctrine ORM. Hybrid projects may require additional factory wrappers or fall back to Laravel’s native factories for Eloquent-only models.
- How do I migrate from Laravel’s built-in `make:factory` to Foundry?
- Replace `Post::factory()` with `PostFactory::new()` and chain state methods (e.g., `->published()`). Use `php artisan foundry:upgrade` if available, or write a custom script to convert existing factories. Foundry’s fluent syntax mirrors Laravel’s factory pattern but adds auto-completion and state objects for reusability.
- Will Foundry work with Pest PHP for testing, or is it PHPUnit-only?
- Foundry works with PHPUnit via the `ZenstruckFoundryBundle` and has community support for Pest (e.g., `pestphp/foundry`). The core library is framework-agnostic, so you can use it in Pest tests by manually loading factories or integrating the bundle. Check the Pest package docs for setup details.
- How does Foundry handle database resets in Laravel’s testing environment?
- Foundry v2.9+ replaces the `ResetDatabase` trait with built-in reset mechanisms. Use `Foundry::reset()` or configure auto-reset in your test suite. For Laravel’s `DatabaseTransactions`, ensure Foundry’s auto-refresh is disabled (`auto_refresh: false`) to avoid conflicts with transaction rollbacks.
- What’s the performance impact of Foundry’s auto-refresh feature in large test suites?
- Auto-refresh (enabled by default) ensures relationship consistency but can slow down tests with large datasets. Disable it via `auto_refresh: false` in your Foundry config. For CI/CD, consider resetting the database between test suites instead of relying on auto-refresh.
- Can Foundry generate fixtures for MongoDB ODM in a Laravel app?
- Yes, Foundry supports `doctrine/mongodb-odm` via the `DoctrineMongoDBBundle`. Install the bundle and configure Foundry to work with MongoDB entities. This is useful for hybrid Laravel apps using both SQL and NoSQL databases, though Eloquent models won’t integrate directly.
- How do I handle Faker seed consistency across test runs in Foundry?
- Use `FOUNDRY_FAKER_SEED` to scope seeds per test case or environment. Avoid global seeds in CI/CD to prevent flaky tests. Foundry’s state objects (e.g., `PublishedState`) can also enforce deterministic data without relying on Faker seeds for critical fields.
- Are there alternatives to Foundry for Laravel that don’t require Doctrine?
- For Eloquent-only projects, consider `laravel/factories` (native) or `mockery/mockery` for mock objects. If you need advanced features like state management or auto-completion, Foundry’s wrapper approach (via custom factories) may still be worth the Doctrine dependency for core entities.
- How do I integrate Foundry with Laravel’s `DatabaseMigrations` for test isolation?
- Foundry’s reset mechanisms work alongside Laravel’s migrations. Use `Foundry::reset()` before each test or configure `database: { reset: true }` in your test suite. For complex setups, combine Foundry’s fixtures with `DatabaseMigrations` to ensure a clean slate before each test.
- Does Foundry support event-driven fixture lifecycle hooks (e.g., pre/post-persistence) in Laravel?
- Yes, Foundry uses Symfony’s event system (e.g., `#[AsFoundryHook]`) for lifecycle hooks. In Laravel, dispatch custom events or use Foundry’s `onCreate()`/`onUpdate()` callbacks. This is useful for side effects like notifications or caching, though it requires familiarity with Symfony’s event system.