This is a Laravel 4 package that makes the process of generating fake data models a breeze. It will enable you to quickly generate data for your tests or just to seed your application's database for UI review.
You'll need to add the package to you composer.json file.
The Fakefactory is the main class that you'll be using. It's responsible for create the fake models for your application. When it's instructed to create a model it will query the database for any information about that model's attributes and relations to other models. Based on that and any information provided by Faker objects it will create a model instance.
Here are several examples as how you can use the Fakefactory to create model instances.
The above example creates a User model instance, but without saving it to the database. To create a model and save it one go use:
Extracting model data from the database
The Fakefactory will try to extract any data it can about your model from the database. It will extract column names and types and based on that select appriate value generation methods. It will also be able to determine any related models based on foreign keys present on the model's table.
You can edit the default value generation rules by editing the modules configuration. You'll need run the following code to publish the configration into your config folder:
php artisan config:publish skovachev/fakefactory
By default the Fakefactory will not generate ID attributes for your models. This however can be changed either in the package config.php file or by using the following build option:
This build option can also be used as a one time override of the configuration. If you had it enabled by default you could then disable it for a single model instance:
In some cases you may want to generate a model along with a related model and insert both of them. This can be done with the with build option.
In the above example the factory will generate a Post model and attach it to the User model based on their relation type.
You can also override attributes of the related model by nesting the like in the following example:
The above code will generate a User model instance, but without providing a value for the username attribute.
In some cases the Fakefactory will not able to extract all the information it needs to generate a good fake instance from the database. In other cases you'll want more control over how fake values are generated for a specific model attribute. Then you'll need to create a Faker object for your class.
Faker objects provide additional information on how to generate fake models that match your business domain more closely.
A simple Faker object for the User model may look like so:
The above code will tell the Fakefactory class that when generating a User model it needs to set the type attribute to either admin or client. Additionally it tells it that there is relation present that does not include a foreign key on the model's table. In this case the foreign key would be on the posts table - for instance user_id.
Faking related classes by default
You can indicate that some relations in your model should be faked by default, without you having to specify the with rule when creating a faked object:
Simply creating the class, however, will not be enough. You'll need to tell the Fakefactory to use this Faker class when it needs to generate User models. The following code registers the Faker object with the Fakefactory:
The Fakefactory package uses the awesome Faker package to generate fake data.
It will extract column names and types from the database and use the appropriate generator rules for them.
For instance if we had a column of type Integer the factory would use the following faker method:
If we had a column named phone the phoneNumber method would be used.
All this can be changed in the package's config.php file.
If you wanted to generate a custom rule for a specific attribute you'll need to have a Faker class with a custom method. Let's say we wanted all usernames to be in the form <random_number>_<random_string>. Our Faker class could look like so:
The $faker argument is an instance of the Faker\Generator object providing all the generation of abilities of the Faker package.
You can also use any of the rules provided by the generator directly from the attributes array. If the Faker\Generator method you want to use expects arguments just pass them after the method name, separated with '|'. For example to generate 3 paragraphs of text you'll need to use the paragraphs method. Your attributes array may look like this:
Fakefactory is released under the MIT Licence. See the bundled LICENSE file for details.
Please do not hesitate to send suggestions and feature requests. Let's make this package awesome!