- Can I use zenstruck/foundry to replace Laravel’s built-in Factory classes (e.g., User::factory())?
- Yes, Foundry is designed to replace or extend Laravel’s Factory classes. It offers a more expressive API with stateful fixtures and auto-completion for related models. You’ll need to wrap Foundry’s factories in a Laravel-friendly facade or trait (e.g., HasFoundry) to maintain compatibility with existing test code.
- How does Foundry handle stateful fixtures (e.g., users with published vs. draft posts) in Laravel tests?
- Foundry’s StatefulInterface lets you define reusable states (e.g., `hasPublishedPost()` or `isAdmin()`) for fixtures. In Laravel, you can integrate this with PHPUnit/Pest by creating custom test traits or helper methods. This improves test isolation and readability compared to Laravel’s default factory states.
- Will zenstruck/foundry work with Laravel’s RefreshDatabase or MigrateFresh traits?
- Foundry’s fixtures are designed for on-demand generation, so they work well with Laravel’s database refresh traits. However, you may need to customize how states are managed during test execution. For example, you could reset stateful fixtures between tests using Foundry’s Loader or a custom test helper.
- Does Foundry support non-Doctrine models (e.g., custom collections, non-ORM entities) in Laravel?
- Foundry is optimized for Doctrine/Eloquent models, so its auto-completion features won’t apply to non-ORM entities. For custom collections or non-Doctrine models, you’ll need to manually define factories or use Foundry’s base Factory class without auto-completion.
- How do I install and register zenstruck/foundry in a Laravel project?
- Install via Composer: `composer require zenstruck/foundry`. Register fixtures in your `TestCase` or a dedicated `FixtureServiceProvider` using Foundry’s Loader. For example, add `$this->loadFixtures()` to your `setUp()` method to auto-load fixtures before each test.
- Is zenstruck/foundry compatible with Laravel 10.x and PHP 8.1+?
- Yes, Foundry supports PHP 8.1+ and integrates seamlessly with Laravel 10.x. It’s designed for modern PHP and Laravel’s testing ecosystem, including PHPUnit and Pest. Check the package’s changelog for version-specific updates.
- Can Foundry auto-generate related models (e.g., User → Post → Comment chains) without manual setup?
- Yes, Foundry’s auto-completion feature reduces boilerplate by generating related models dynamically. For example, defining a `User` factory with `hasMany(Post::class)` will auto-create posts when a user is instantiated. This works out-of-the-box for Eloquent relationships.
- How does Foundry’s performance compare to Laravel’s native factories for large test suites?
- Foundry is optimized for on-demand fixture generation, avoiding global state pollution. While auto-completion adds convenience, it may introduce slight overhead for deeply nested fixtures. Benchmark against your existing factories, especially for test suites with 1000+ fixture generations.
- Are there alternatives to zenstruck/foundry for Laravel fixture generation?
- Laravel’s built-in Factory classes and packages like `laravel-shift/database` or `mockery` are alternatives. Foundry stands out for its stateful, auto-completing approach and Symfony/Doctrine compatibility. If you need migration testing, consider combining Foundry with `laravel-shift/database` for schema validation.
- How can I integrate Faker with zenstruck/foundry in Laravel?
- Foundry has built-in Faker integration for generating realistic test data. Use Faker’s rules (e.g., `name()`, `email()`) directly in your factory definitions. For Laravel-specific Faker seeds, you may need to bridge Foundry’s Faker instance with Laravel’s `Faker` facade or extend Foundry’s factory classes.