Download the PHP package silvertipsoftware/factorygirl without Composer
On this page you can find all versions of the php package silvertipsoftware/factorygirl. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download silvertipsoftware/factorygirl
More information about silvertipsoftware/factorygirl
Files in silvertipsoftware/factorygirl
Package factorygirl
Short Description A port of FactoryGirl to Laravel4/Eloquent
License MIT
Informations about the package factorygirl
SilvertipSoftware/FactoryGirl
FactoryGirl is a (or hopes to be) a relatively faithful port of thoughtbot/factory_girl, a Ruby-land object factory library for testing.
Installation
Composer
Add silvertipsoftware/factorygirl to the require-dev section of your composer.json:
You can obviously choose any available version(s). Run composer update to get it.
Laravel
Add the FactoryGirl ServiceProvider to your Laravel application:
(optional) Add the following snippets to your app/config/app.php file:
Supported PHP Versions
Currently, PHP 5.3+ is supported. PHP 5.4 would make some things nicer, so that may change for future releases.
Documentation
See FactoryGirl documentation to get a feel for what it does and is used for. The syntax is hopefully a straightforward port to PHP.
Factory Definitions
The app/tests/factories.php file is used to store the factory definitions, and is automatically loaded on the first build/create on an object.
Basic Factory
defines a factory for a Room Eloquent model, and sets the name,capacity, and notes attributes to the given values. The model class name is inferred from the factory name. Where that's not intended, the model class name can also be specified:
Building/Creating Objects
With the factory defined above, your test code can do:
to get new room instances, which are not saved to the database. If you want them persisted, use create:
In either case, attributes may be overridden by passing an array as a second parameter:
Sequences
Sequences return a value based on a increasing index passed to the closure. Useful for creating uni que attributes in a standard way.
Then, in a factory, you can use the sequence by:
Associations
Given a factory for an account model, you can associate a user model to it with:
When a user is built, an account instance will be created and the account_id of user will be set to reference the new account. Currently, only belongsTo relations are supported, and the only "build strategy" is to save the associated object in the database.
Attribute values in the associated object can be overridden by passing an array:
The factory to use is inferred from the attribute name. If a different factory is desired, pass it to the associate call:
Or, specify both the factory and overrides:
Closures as Attributes
A closure can also be passed as an attribute value, and it is evaluated during model build. This lets you do more complex logic at build-time. This is particularly useful for 3-object associations. For example:
Attribute values are evaluated in the order they are given in the factory definition, so swapping account and location above would not have worked.
Inheritance
Factories can be chained to reuse common attribute definitions as follows: