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.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package factory-bot

factory-bot

Integrate Merge Release Renew

Code Coverage Type Coverage

Latest Stable Version Total Downloads Monthly Downloads

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

You can find examples in

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

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:

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::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:

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:

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:

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

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

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

PHP Build Version
Package Version
Requires php Version ~8.1.0 || ~8.2.0 || ~8.3.0
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
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package ergebnis/factory-bot contains the following files

Loading the files please wait ....