Download the PHP package ergebnis/factory-bot without Composer
On this page you can find all versions of the php package ergebnis/factory-bot. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package factory-bot
factory-bot
This project provides a composer
package with a fixture factory for doctrine/orm
entities.
Installation
Run
Usage
The entry point of ergebnis/factory-bot
is the FixtureFactory
.
You will use the fixture factory to create entity definitions and to create Doctrine entities populated with fake data.
- Examples
- Creating a fixture factory
- Creating entity definitions
- Loading entity definitions
- Registering entity definitions
- Creating entities
- Persisting entities
- Flushing entities
Examples
You can find examples in
- the directory
example/
- the repository
ergebnis/factory-bot-example
Creating a fixture factory
The fixture factory requires an instance of Doctrine\ORM\EntityManagerInterface
(for reading class metadata from Doctrine entities, and for persisting Doctrine entities when necessary) and an instance of Faker\Generator
for generating fake data.
To simplify the creation of a fixture factory in tests, you can create an abstract test case with access to an entity manager, a faker, and a fixture factory.
Creating entity definitions
Now that you have access to a fixture factory, you can create definitions for Doctrine entities.
This simple definition might work when all entity fields have default values, but typically, you will want to provide a map of entity field names to field definitions.
In addition to the map of field names to field definitions, you can specify a closure that the fixture factory will invoke after creating the entity. The closure accepts the freshly created entity and the map of field names to field values that the fixture factory used to populate the entity.
:bulb: You can use the closure to modify the freshly created entity.
Field Definitions
A field definition can be
- an implementation of
FieldDefinition\Resolvable
- a closure (will be normalized to
FieldDefinition\Closure
) - an arbitrary value (will be normalized to
FieldDefinition\Value
)
You can use the FieldDefinition
factory to create field definitions shipped with this package or implement the FieldDefinition\Resolvable
interface yourself.
:bulb: Custom field definitions can be useful when you are dealing with identical field definitions over and over again.
Non-nullable fields
When you are working with non-nullable fields, you can use the following field definitions, all of which will resolve to concrete references or values:
FieldDefinition::closure()
FieldDefinition::reference()
FieldDefinition::references()
FieldDefinition::sequence()
FieldDefinition::value()
Nullable fields
When you are working with nullable fields, you can use the following field definitions, all of which will either resolve to null
or to a concrete reference or value (depending on the strategy:
FieldDefinition::optionalClosure()
FieldDefinition::optionalReference()
FieldDefinition::optionalSequence()
FieldDefinition::optionalValue()
FieldDefinition::closure()
FieldDefinition::closure()
accepts a closure.
The fixture factory will resolve the field definition to the return value of invoking the closure with the instance of Faker\Generator
composed into the fixture factory, and the fixture factory itself.
:bulb: It is possible to specify a closure only (will be normalized to FieldDefinition\Closure
):
FieldDefinition::optionalClosure()
FieldDefinition::optionalClosure()
accepts a closure.
A fixture factory using the Strategy\DefaultStrategy
will resolve the field definition to null
or to the return value of invoking the closure with the instance of Faker\Generator
composed into the fixture factory.
A fixture factory using the Strategy\WithOptionalStrategy
will resolve the field definition to the return value of invoking the closure with the instance of Faker\Generator
composed into the fixture factory.
A fixture factory using the Strategy\WithoutOptionalStrategy
will resolve the field definition to null
.
FieldDefinition::reference()
FieldDefinition::reference()
accepts the class name of an entity or embeddable, and optionally an array of field definition overrides.
Every fixture factory will resolve the field definition to an instance of the entity or embeddable class populated through the fixture factory.
When field definition overrides are specified, they will be used to override exsting field definitions of the referenced entity.
:exclamation: When resolving the reference, the fixture factory needs to be aware of the referenced entity or embeddable.
FieldDefinition::optionalReference()
FieldDefinition::optionalReference()
accepts the class name of an entity or embeddable, and optionally an array of field definition overrides.
A fixture factory using the Strategy\DefaultStrategy
will resolve the field definition to null
or an instance of the entity or embeddable class populated through the fixture factory.
A fixture factory using the Strategy\WithOptionalStrategy
will resolve the field definition to an instance of the entity or embeddable class populated through the fixture factory.
A fixture factory using the Strategy\WithoutOptionalStrategy
will resolve the field definition to null
.
:exclamation: When resolving the reference, the fixture factory needs to be aware of the referenced entity or embeddable.
FieldDefinition::references()
FieldDefinition::references()
accepts the class name of an entity or embeddable, the count of desired references, and optionally an array of field definition overrides.
You can create the count from an exact number, or minimum and maximum values.
:bulb: When you create the count from minimum and maximum values, the fixture factory will resolve its actual value before creating references. This way, you can have variation in the number of references - any number between the minimum and maximum can be assumed.
A fixture factory using the Strategy\DefaultStrategy
will resolve the field definition to an array with zero or more instances of the entity or embeddable class populated through the fixture factory. Depending on the value of $count
, the array might be empty.
A fixture factory using the FixtureFactory::createMany()
.
A fixture factory using the FixtureFactory::createMany()
.
:exclamation: When resolving the references, the fixture factory needs to be aware of the referenced entity or embeddable.
FieldDefinition::sequence()
FieldDefinition::sequence()
accepts a string containing the %d
placeholder at least once and an optional initial number (defaults to 1
).
Every fixture factory will resolve the field definition by replacing all occurrences of the placeholder %d
in the string with the sequential number's current value. The sequential number will then be incremented by 1
for the next run.
FieldDefinition::optionalSequence()
FieldDefinition::optionalSequence()
accepts a string containing the %d
placeholder at least once and an optional initial number (defaults to 1
).
A fixture factory using the Strategy\DefaultStrategy
will resolve the field definition to null
or by replacing all occurrences of the placeholder %d
in the string with the sequential number's current value. The sequential number will then be incremented by 1
for the next run.
A fixture factory using the Strategy\WithOptionalStrategy
will resolve the field definition by replacing all occurrences of the placeholder %d
in the string with the sequential number's current value. The sequential number will then be incremented by 1
for the next run.
A fixture factory using the Strategy\WithoutOptionalStrategy
will resolve the field definition to null
.
FieldDefinition::value()
FieldDefinition::value()
accepts an arbitrary value.
The fixture factory will resolve the field definition to the value.
:bulb: It is also possible to specify a value only:
FieldDefinition::optionalValue()
FieldDefinition::optionalValue()
accepts an arbitrary value.
A fixture factory using the Strategy\DefaultStrategy
will resolve the field definition to null
or the value.
A fixture factory using the Strategy\WithOptionalStrategy
will resolve the field definition to the value.
A fixture factory using the Strategy\WithoutOptionalStrategy
will resolve the field definition to null
.
Loading entity definitions
Instead of creating entity definitions inline, you can implement the EntityDefinitionProvider
interface and load entity definitions contained within a directory with the fixture factory.
First, create concrete definition providers.
:bulb: While you can use a single entity definition provider to provide definitions for all entities, I recommend using one definition provider per entity. Then you can quickly implement an auto-review test to enforce that an entity definition provider exists for each entity.
Second, adjust your abstract test case to load definitions from entity definition providers contained in a directory.
Registering entity definitions
Instead of loading entity definition providers contained within a directory with the fixture factory, you can also register entity definition providers that you have already instantiated.
Creating entities
Now that you have created (or loaded) entity definitions, you can create Doctrine entities populated with fake data.
The fixture factory allows to create entities using the following strategies:
Strategy\DefaultStrategy
Strategy\WithOptionalStrategy
Strategy\WithoutOptionalStrategy
Strategy\DefaultStrategy
The Strategy\DefaultStrategy
involves random behavior, and based on randomness, the fixture factory might or might not resolve optional field references:
FieldDefinition::optionalClosure()
might be resolved tonull
or a concrete valueFieldDefinition::optionalReference()
might be resolved tonull
or a concrete referenceFieldDefinition::optionalSequence()
might be resolved tonull
or a concrete valueFieldDefinition::optionalValue()
might be resolved tonull
or a concrete valueFieldDefinition::references()
might be resolved to anarray
of zero or more references
The fixture factory uses the Strategy\DefaultStrategy
by default.
Strategy\WithOptionalStrategy
The Strategy\WithOptionalStrategy
involves random behavior, but the fixture factory will resolve optional field references:
FieldDefinition::optionalClosure()
will be resolved to a concrete valueFieldDefinition::optionalReference()
will be resolved to a concrete referenceFieldDefinition::optionalSequence()
will be resolved to a concrete valueFieldDefinition::optionalValue()
will be resolved to a concrete valueFixtureFactory::createMany()
To create a fixture factory using the Strategy\WithOptionalStrategy
out of an available fixture factory, invoke withOptional()
:
Strategy\WithoutOptionalStrategy
The Strategy\WithoutOptionalStrategy
involves random behavior, but the fixture factory will not resolve optional field references:
FieldDefinition::optionalClosure()
will be resolved tonull
FieldDefinition::optionalReference()
will be resolved tonull
FieldDefinition::optionalSequence()
will be resolved tonull
FieldDefinition::optionalValue()
will be resolved tonull
FixtureFactory::createMany()
To create a fixture factory using the Strategy\WithoutOptionalStrategy
out of an available fixture factory, invoke withoutOptional()
:
FixtureFactory::createOne()
FixtureFactory::createOne()
accepts the class name of an entity and optionally, a map of entity field names to field definitions that should override the field definitions for that specific entity.
The fixture factory will return a single entity.
A field definition override can be
- an implementation of
FieldDefinition\Resolvable
- a closure (will be normalized to
FieldDefinition\Closure
) - an arbitrary value (will be normalized to
FieldDefinition\Value
)
Also see Creating entity definitions.
FixtureFactory::createMany()
FixtureFactory::createMany()
accepts the class name of an entity, the count of desired entities, and an optional map of entity field names to field definitions that should override the field definitions for that specific entity.
You can create the count from an exact number:
The fixture factory will resolve $count
to 5
.
You can also create the count from minimum and maximum values.
The fixture factory will resolve $count
to any number between 0
and 20
.
The fixture factory will return an array of entities.
A field definition override can be
- an implementation of
FieldDefinition\Resolvable
- a closure (will be normalized to
FieldDefinition\Closure
) - an arbitrary value (will be normalized to
FieldDefinition\Value
)
Also see Creating entity definitions.
Persisting entities
When the fixture factory creates entities, the fixture factory does not persist them by default.
To create a fixture factory that persists entities out of an available fixture factory, invoke persisting()
:
After this point, the fixture factory will automatically persist every entity it creates.
:exclamation: You need to flush the entity manager yourself.
Flushing entities
The fixture factory will not flush the entity manager - you need to flush it yourself.
Changelog
The maintainers of this project record notable changes to this project in a changelog.
Contributing
The maintainers of this project suggest following the contribution guide.
Code of Conduct
The maintainers of this project ask contributors to follow the code of conduct.
General Support Policy
The maintainers of this project provide limited support.
You can support the maintenance of this project by sponsoring @localheinz or requesting an invoice for services related to this project.
PHP Version Support Policy
This project supports PHP versions with active and security support.
The maintainers of this project add support for a PHP version following its initial release and drop support for a PHP version when it has reached the end of security support.
Security Policy
This project has a security policy.
License
This project uses the MIT license.
Credits
This project is based on breerly/factory-girl-php@0e6f1b6
(originally licensed under MIT by Grayson Koonce), which is based on xi/doctrine
(originally licensed under MIT by Xi), which in turn provided a port of factory_bot
(originally licensed under MIT by Joe Ferris and thoughtbot, Inc.).
Social
Follow @localheinz and @ergebnis on Twitter.
All versions of factory-bot with dependencies
doctrine/collections Version ^1.6.5 || ^2.0.0
doctrine/dbal Version ^2.12.0 || ^3.0.0 || ^4.0.0
doctrine/instantiator Version ^1.0.0 || ^2.0.0
doctrine/orm Version ^2.14.0 || ^3.0.0
doctrine/persistence Version ^2.1.0 || ^3.0.0
ergebnis/classy Version ^1.6.0
fakerphp/faker Version ^1.20.0