league/factory-muffin
Factory Muffin helps you rapidly create test objects in PHP with simple, reusable model factories (inspired by factory_girl). Define models and generate instances for fixtures and unit tests; optional Faker integration is available via factory-muffin-faker.
DatabaseFactory but offers declarative, reusable factories for complex object graphs.UserFactory, OrderFactory), reducing test pollution and improving maintainability.league/factory-muffin and a factory class. No database migrations or schema changes needed for basic usage.DatabaseFactory or ModelFactory by extending existing factory classes or using factory composition.mocksfactory/mocksfactory).DatabaseFactory or mocksfactory/mocksfactory meet our needs with lower risk?create()/factory() syntax sufficient?Model::create() calls in createUserTest().beforeEach/afterEach for factory reset.UserTest from manual User::create() to UserFactory::create().DatabaseFactory entirely (if justified).use League\FactoryMuffin\Factory;
use League\FactoryMuffin\FactoryInterface;
class UserFactory extends Factory implements FactoryInterface { ... }
Illuminate\Testing changes).Model::create() (e.g., some ORM tools).composer require league/factory-muffin.make:factory).tests/FactoryMuffin/ (e.g., UserFactory.php).TestCase setup:
protected function setUp(): void {
parent::setUp();
$this->loadMigrationsFrom(__DIR__.'/../database/migrations');
FactoryMuffin::loadFactoriesFrom(__DIR__.'/FactoryMuffin');
}
User::create() with UserFactory::create().UserFactory::new()->admin()->create();
create() calls.dd($factory->getState()) for diagnostics.OrderFactory with 10 relationships) may slow tests.create() calls.| Failure Scenario | Impact | Mitigation |
|---|---|---|
| Factory defines invalid DB state | Tests pass but reflect bad data | Add assertDatabaseHas() validations |
| Package incompatibility (PHP 8.x) | Tests fail silently | Fork or switch to mocksfactory/mocksfactory |
| Overly coupled factories | Refactoring breaks tests | Keep factories stateless or use traits |
| Missing factory for a model | Manual create() slips in |
Enforce factory-first in PR reviews |
How can I help you explore Laravel packages today?