Download the PHP package sizuhiko/fabricate without Composer
On this page you can find all versions of the php package sizuhiko/fabricate. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package fabricate
Fabricate
PHP data generator for Testing
It's inspired on Fabrication and factory-girl from the Ruby world.
Fabricate is a simple fake object generation core library for PHP. Quickly Fabricate objects as needed anywhere in your app or test case.
If you use CakePHP2, please see cakephp2 branch.
Install
Add require-dev in your composer.json
composer require --dev sizuhiko/fabricate
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
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).
Comunity Adaptors
- CakeFabricate for CakePHP3
- Please contribute
The Basics
The simplest way to generate objects
That will generate and save to database an instance of Post using the schema information.
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.
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.
The hash will overwrite any fields defined in the callback function.
APIs
Configuration
To override these settings, put a bootstrap.php in your app folder and append the path to phpunit.xml
Supported Options
sequence_start
Allows you to specify the default starting number for all sequences. This can still be overridden for specific sequences.
Default: 1
adaptor
Adapters ease the population of databases through the Database accessor provided by an ORM library(or framework).
Default: null
faker
Allow you to specify the default Faker instance to return localized data.
Default: default locale(en_EN) instance
text_size_limit
Allow you to specify the limit size for generation of text field.
Default: 200
from v1.2.3
Generate model attributes as array (not saved)
Fabricate::attributes_for(:model_name, :number_of_generation, :array_or_callback)
generate only attributes.
- model_name: Model class name.
- number_of_generation: Generated number of records
- array_or_callback: it can override each generated attributes
Example
Generate a model instance (not saved)
Fabricate::build(:model_name, :array_or_callback)
generate a model instance (using ClassRegistry::init).
- model_name: Model class name.
- array_or_callback: it can override each generated attributes
Example
Generate records to database
Fabricate::create(:model_name, :number_of_generation, :array_or_callback)
generate and save records to database.
- model_name: Model class name.
- number_of_generation: Generated number of records
- array_or_callback: it can override each generated attributes
Example
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,
To use a different name from the class, you must specify 'class'=>:class_name into first argument as array.
You can inherit attributes from other defined set of attributes by using the 'parent' key.
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.
Usage
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.
Config
Allows you to specify the default starting number for all sequences. This can still be overridden for specific sequences.
Usage
If you want use sequence within generation function, callback has second attribute.
$world
is FabricateContext instance. It have sequence method.
FabricateContext#sequence API
You should set name argument to sequence.
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.
If you are generating something like an unique string, you can pass it a callback function and the callback function response will be returned.
Traits
Traits allow you to group attributes together and then apply them to any fabricating objects.
traits
can specify defined names as array
Faker
Faker is a PHP library that generates fake data for you. Fabrication provides you with generation custom value for own rule.
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.
Usage
Reloading
If you need to reset fabricate back to its original state after it has been loaded.
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.