Factory Drone is a comprehensive solution for using the factory pattern to generate short-lived test data. This approach provides richer context for individual tests and discourages the creation of unwieldy fixture data.
Travis-CI Build Status
Defining Factories
Creating factory definitions is fairly straightforward - simply provide the type, a unique name for the factory, and any default attributes. The following example illustrates the use of sequences, randomly generated values, and associations.
FactoryDrone::define('node', 'simple page', array(
'type' => FactoryDrone::association('a content type factory'),
'title' => FactoryDrone::sequence(function ($n) {
return "node_title{$n}";
}),
'body' => FactoryDrone::random(function ($n) {
return "node_body{$n}";
}),
));
Loading Factories
It will be necessary to manually load factories in your test suite. This can be accomplished by adding any necessary paths and loading the definitions.
FactoryDrone::addFactoryPath(join(DIRECTORY_SEPARATOR, array(__DIR__, 'factories')));
FactoryDrone::loadDefinitions();
Using Factories
When using a factory, it is possible to override any of the options provided by the definition. Consequently, you always have control over the data at the time you create it. Using factories is as simple as:
FactoryDrone::create('simple page');
or, to override options:
FactoryDrone::create('simple page', array('title' => 'Overridden Title'));
Drush Integration
Factory Drone fully exposes its data creation interface through Drush. See drush help fd-create for more information.
Supported Types
At present, Factory Drone has support for the following types of data in Drupal:
- Vocabularies
- Terms
- Roles
- Users
- Content Types
- Nodes
- Menus
- Menu Links
Roadmap
Above all, the vision for Factory Drone is to fix this testing pain point and will benefit greatly from the input of the Drupal community. Upcoming improvements under consideration, however, include:
- Improved fields support
- Method to return objects without persisting them
- Inheritance of options from another factory
- Callbacks to provide ways to hook into the creation process
Project information
- Created by sonnym on , updated
Stable releases for this project are covered by the security advisory policy.
There are currently no supported stable releases.
