, (*1)
Fabricate
PHP data generator for Testing, (*2)
It's inspired on Fabrication and factory-girl from the Ruby world., (*3)
Fabricate is a simple fake object generation core library for PHP.
Quickly Fabricate objects as needed anywhere in your app or test case., (*4)
If you use CakePHP2, please see cakephp2 branch., (*5)
Install
Add require-dev in your composer.json, (*6)
composer require --dev sizuhiko/fabricate
, (*7)
Usage
Adaptor
At first, Fabricate require to config for using.
For example, to override these settings, put a bootstrap.php in your app folder and append the path to phpunit.xml, (*8)
use Fabricate\Fabricate;
use CakeFabricate\Adaptor\CakeFabricateAdaptor;
Fabricate::config(function($config) {
$config->adaptor = new CakeFabricateAdaptor();
});
Fabricate doesn't provide adaptors.
If you will make adaptor of any frameworks, send us your pull request.
The pull request will include suggestion into composer.json and link of repository on README(Comunity Adaptors)., (*9)
Comunity Adaptors
The Basics
The simplest way to generate objects, (*10)
Fabricate::create('Post')
That will generate and save to database an instance of Post using the schema information., (*11)
To set additional attributes or override what is in the Fabricator, you can pass a array to Fabricate with the fields you want to set., (*12)
Fabricate::create('Post', ["created" => "2013-10-09 12:40:28", "updated" => "2013-10-09 12:40:28"])
Fabricating With Blocks
In addition to the array, you can pass a callback function to Fabricate and all the features of a Fabricator definition are available to you at object generation time., (*13)
Fabricate::create('Post', 10, function($data){
return ["created" => "2013-10-09 12:40:28", "updated" => "2013-10-09 12:40:28"];
});
The hash will overwrite any fields defined in the callback function., (*14)
APIs
Configuration
To override these settings, put a bootstrap.php in your app folder and append the path to phpunit.xml, (*15)
Fabricate::config(function($config) {
$config->sequence_start = 1;
$config->adaptor = new Fabricate\Adaptor\CakePHPAdaptor();
$config->faker = \Faker\Factory::create('ja_JP');
});
Supported Options
sequence_start
Allows you to specify the default starting number for all sequences.
This can still be overridden for specific sequences., (*16)
Default: 1
, (*17)
adaptor
Adapters ease the population of databases through the Database accessor provided by an ORM library(or framework)., (*18)
Default: null
, (*19)
faker
Allow you to specify the default Faker instance to return localized data., (*20)
Default: default locale(en_EN) instance
, (*21)
text_size_limit
Allow you to specify the limit size for generation of text field., (*22)
Default: 200
, (*23)
from v1.2.3, (*24)
Generate model attributes as array (not saved)
Fabricate::attributes_for(:model_name, :number_of_generation, :array_or_callback)
generate only attributes., (*25)
- model_name: Model class name.
- number_of_generation: Generated number of records
- array_or_callback: it can override each generated attributes
Example
$results = Fabricate::attributes_for('Post', 10, function($data){
return ["created" => "2013-10-09 12:40:28", "updated" => "2013-10-09 12:40:28"];
});
// $results is followings :
array (
0 =>
array (
'id' => 1,
'title' => 'Lorem ipsum dolor sit amet',
'body' => 'Lorem ipsum dolor sit amet, aliquet feugiat. Convallis morbi fringilla gravida, phasellus feugiat dapibus velit nunc, pulvinar eget sollicitudin venenatis cum nullam, vivamus ut a sed, mollitia lectus. Nulla vestibulum massa neque ut et, id hendrerit sit, feugiat in taciti enim proin nibh, tempor dignissim, rhoncus duis vestibulum nunc mattis convallis.',
'created' => '2013-10-09 12:40:28',
'updated' => '2013-10-09 12:40:28',
),
1 =>
array (
....
Generate a model instance (not saved)
Fabricate::build(:model_name, :array_or_callback)
generate a model instance (using ClassRegistry::init)., (*26)
- model_name: Model class name.
- array_or_callback: it can override each generated attributes
Example
$result = Fabricate::build('Post', function($data){
return ["created" => "2013-10-09 12:40:28", "updated" => "2013-10-09 12:40:28"];
});
// $results are depends adaptor result.
......
Generate records to database
Fabricate::create(:model_name, :number_of_generation, :array_or_callback)
generate and save records to database., (*27)
- model_name: Model class name.
- number_of_generation: Generated number of records
- array_or_callback: it can override each generated attributes
Example
Fabricate::create('Post', 10, function($data){
return ["created" => "2013-10-09 12:40:28", "updated" => "2013-10-09 12:40:28"];
});
Defining
Fabricate has a name and a set of attributes when fabricating objects.
The name is used to guess the class of the object by default,, (*28)
Fabricate::define('Post', ['published'=>'1']);
// or using callback block
Fabricate::define('Post', function($data, $world) {
return ['published'=>'1']
});
To use a different name from the class, you must specify 'class'=>:class_name into first argument as array., (*29)
Fabricate::define(['PublishedPost', 'class'=>'Post'], ['published'=>'1']);
Fabricate::create('PublishedPost');
You can inherit attributes from other defined set of attributes by using the 'parent' key., (*30)
Fabricate::define(['PublishedPost', 'class'=>'Post'], ['published'=>'1']);
Fabricate::define(['Author5PublishedPost', 'parent'=>'PublishedPost'], ['author_id'=>'5']);
Fabricate::create('Author5PublishedPost');
Associations
It's possible to set up associations(hasOne/hasMany/belongsTo) within Fabricate::create().
You can also specify a FabricateContext::association().
It will generate the attributes, and set(merge) it in the current array., (*31)
Usage
Fabricate::create('User', function($data, $world) {
return [
'user' => 'taro',
'Post' => $world->association('Post', 3),
];
});
// or can overwritten by array or callback block.
Fabricate::create('User', function($data, $world) {
return [
'user' => 'taro',
'Post' => $world->association('Post', 3, function($data, $world) {
return ['title'=>$world->sequence('Post.title',function($i){ return "Title-${i}"; })];
}),
];
});
// can use defined onbject.
Fabricate::define(['PublishedPost', 'class'=>'Post'], ['published'=>'1']);
Fabricate::create('User', function($data, $world) {
return [
'user' => 'taro',
'Post' => $world->association(['PublishedPost', 'association'=>'Post'], 3),
];
});
// can use association alias (Post belongs to Author of User class)
Fabricate::define(['PublishedPost', 'class'=>'Post'], ['published'=>'1']);
Fabricate::create('PublishedPost', 3, function($data, $world) {
return [
'Author' => $world->association(['User', 'association'=>'Author'], ['id'=>1,'user'=>'taro']),
];
});
Sequences
A sequence allows you to get a series of numbers unique within the each generation function. Fabrication provides you with an easy and flexible means for keeping track of sequences., (*32)
Config
Allows you to specify the default starting number for all sequences.
This can still be overridden for specific sequences., (*33)
Fabricate::config(function($config) {
$config->sequence_start = 100;
});
Usage
Fabricate::config(function($config) {
$config->sequence_start = 100;
});
$results = Fabricate::attributes_for('Post', 10, function($data, $world){
return [
'id'=> $world->sequence('id'),
'title'=> $world->sequence('title', 1, function($i){ return "Title {$i}"; })
];
});
// $results is followings :
array (
0 =>
array (
'id' => 100, // starting configure sequence
'title' => 'Title 1', // closure function returned
...
),
1 =>
array (
'id' => 101, // starting configure sequence
'title' => 'Title 2', // closure function returned
...
If you want use sequence within generation function, callback has second attribute.
$world
is FabricateContext instance. It have sequence method., (*34)
FabricateContext#sequence API
You should set name argument to sequence., (*35)
$world->sequence('id')
If you want to specify the starting number, you can do it with a second parameter.
It will always return the seed number on the first call and it will be ignored with subsequent calls., (*36)
$world->sequence('id', 10)
If you are generating something like an unique string, you can pass it a callback function and the callback function response will be returned., (*37)
$world->sequence('title', function($i){ return "Title {$i}"; }
// or with start number
$world->sequence('title', 1, function($i){ return "Title {$i}"; }
Traits
Traits allow you to group attributes together and then apply them to any fabricating objects., (*38)
Fabricate::define(['trait'=>'published'], ['published'=>'1']);
Fabricate::create('Post', function($data, $world) {
$world->traits('published');
return ['author_id'=>5];
});
traits
can specify defined names as array, (*39)
Fabricate::define(['trait'=>'published'], ['published'=>'1']);
Fabricate::define(['trait'=>'author5'], function($data, $world) { return ['author_id'=>5]; });
Fabricate::create('Post', function($data, $world) {
$world->traits(['published','author5']);
return [];
});
Faker
Faker is a PHP library that generates fake data for you.
Fabrication provides you with generation custom value for own rule., (*40)
Config
Faker supports a localization.
The default locale is en_EN.
Allows you to specify the locale.
This can still be overridden for specific Faker Factory., (*41)
Fabricate::config(function($config) {
$config->faker = Faker\Factory::create('ja_JP');
});
Usage
Fabricate::config(function($config) {
$config->faker = Faker\Factory::create('ja_JP'); // this is optional
});
$results = Fabricate::attributes_for('User', function($data, $world){
return [
'user'=> $world->faker()->name
];
});
Reloading
If you need to reset fabricate back to its original state after it has been loaded., (*42)
Fabricate::clear();
Contributing to this Library
Please feel free to contribute to the library with new issues, requests, unit tests and code fixes or new features.
If you want to contribute some code, create a feature branch from develop, and send us your pull request., (*43)