- How does zenstruck/foundry compare to Laravel’s built-in Factory classes?
- Foundry offers more expressive syntax and stateful fixtures, reducing boilerplate in complex test scenarios. While Laravel’s factories work well for simple cases, Foundry’s fluent API and auto-completion for relationships (e.g., User → Post → Comment) make it ideal for large-scale testing. You can coexist with Laravel’s factories or replace them entirely, depending on your needs.
- Can I use zenstruck/foundry with Laravel’s Eloquent models?
- Yes, Foundry works seamlessly with Eloquent since it’s built on Doctrine ORM principles. You’ll need to wrap Foundry’s Factory in a Laravel-friendly facade or trait (e.g., `HasFoundry`) to integrate with Eloquent’s `create()` or `factory()` methods. The stateful approach also aligns well with Laravel’s testing conventions.
- Does zenstruck/foundry support Laravel’s RefreshDatabase or MigrateFresh?
- Foundry’s stateful fixtures integrate well with Laravel’s database testing traits. You can use Foundry’s `Loader` to register fixtures before tests run, ensuring clean state management. For `RefreshDatabase`, Foundry’s fixtures will regenerate data on each test cycle, while `MigrateFresh` works similarly but with fresh migrations.
- How do I install and set up zenstruck/foundry in a Laravel project?
- Install via Composer: `composer require zenstruck/foundry`. Register the `FoundryServiceProvider` in `config/app.php` or use the `Loader` in your test `setUp()` method. Define factories in PHP classes (e.g., `UserFactory`) and use Foundry’s fluent API to generate test data, like `$user = UserFactory::createOne().`
- What Laravel versions does zenstruck/foundry support?
- Foundry is compatible with Laravel 10.x+ (PHP 8.1+) and follows Laravel’s LTS releases. It leverages Doctrine ORM, which Eloquent relies on, so there are no major version conflicts. Check the Foundry docs for exact PHP/Doctrine version requirements, as they may evolve with Symfony updates.
- How do I handle relationships between models in zenstruck/foundry?
- Foundry simplifies relationships with auto-completion and stateful helpers. For example, `UserFactory::createOne()->withPosts(3)` generates a user with 3 related posts. You can also define custom states (e.g., `hasPublishedPost()`) to model complex object graphs. This reduces manual fixture setup compared to Laravel’s native factories.
- Can zenstruck/foundry generate realistic test data without Faker?
- Foundry is Faker-powered by default, but you can customize or replace the data generation logic. For non-Faker setups, override the factory’s `define()` method or use Foundry’s `State` system to inject hardcoded or deterministic values. This flexibility works well for testing edge cases or controlled environments.
- Is zenstruck/foundry suitable for large-scale projects with 1000+ fixtures?
- Foundry is optimized for on-demand fixture generation, avoiding global state pollution. However, deep nested fixtures (e.g., 1000+ related records) may introduce overhead. Benchmark performance in your CI pipeline and consider lazy-loading fixtures or splitting them into modular factories to maintain speed.
- How do I test database migrations with zenstruck/foundry?
- Foundry doesn’t natively support migration testing, but you can integrate it with tools like `laravel-shift/database` or custom test helpers. Use Foundry to generate test data *after* migrations run, then assert schema changes separately. For example, run migrations with `Artisan::call('migrate')`, then use Foundry to populate tables for validation.
- Are there alternatives to zenstruck/foundry for Laravel test data?
- Laravel’s built-in Factory classes and packages like `laravel-shift/database` or `mockery` are alternatives. Foundry stands out for its stateful, fluent API and auto-completion, which reduce boilerplate in complex tests. If you need simpler setups, Laravel’s factories may suffice, but Foundry excels for large projects or teams requiring maintainable, expressive fixtures.