A Laravel Bundle to enable the rapid creation of objects for the purpose of testing.
Switch branches/tags
Nothing to show
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.


FactoryMuff (Laravel Bundle)

factory muff poster


This package is outdated. Even thought if you are using Laravel 3, I strongly recommend you to use the new FactoryMuff with composer.

The goal of this Laravel Bundle is to enable the rapid creation of objects for the purpose of testing. Basically a "factory_girl_rails" simplified for use with Laravel.


Do What The Fuck You Want To Public License, Version 2, as published by Sam Hocevar. http://sam.zoy.org/wtfpl/COPYING



php artisan bundle:install factorymuff

Bundle Registration

add the following to application/bundles.php

'factorymuff' => array('auto' => true),

How it works

FactoryMuff (which stands for Factory Muffin) uses a list of 2300+ words with at least 4 characters. These words are scrambled at every execution and will not repeat unless you use all the words. In this case the list is re-started and scrambled again.

Theoretically you will not need to worry about repeating values​​, unless your application has ALOT of tests to run which may cause the wordlist to restart. If this is your case, you can simply increase wordlist in wordlist.php


Declare a public static array called $factory in your model. This array should contain the kind of values you want for the attributes.


class Message extends Eloquent
    // Array that determines the kind of attributes
    // you would like to have
    public static $factory = array(
        'user_id' => 'factory|User',
        'subject' => 'string',
        'address' => 'email',
        'message' => 'text',

    // Relashionship with user
    public function user()
        return $this->belongs_to('User');

To create model instances do the following:


class TestUserModel extends PHPUnit_Framework_TestCase {

    public function __construct()
        // Load FactoryMuff bundle
        Bundle::start( 'factorymuff' );

    public function testSampleFactory()
        // Creates a new instance
        $message = FactoryMuff::create( 'Message' );

        // Access the relationship, because attributes
        // with kind "factory|<ModelName> creates and
        // saves the <ModelName> object and return the
        // id. And now, because of eloquent we can do
        // this:

        // And you can also get attributes for a new
        // instance
        $new_message = new Message( FactoryMuff::attributes_for( 'Message' ) )

        // For both methods (create and attributes_for
        // you can pass fixed attributes. Those will be
        // merged into the object before save.
        $muffin_message = FactoryMuff::create(
            'Message', array(
                'subject' => 'About Muffin',
                'message' => 'Its tasty!',

Kinds of attribute suported

  • string
  • Grab a random word from the wordlist. Ex: "bucket","mouse","laptop","America"
  • email
  • An word from the wordlist + domain. Ex: "smart@example.com", "Brasil@nonexist.org"
  • text
  • A text of about 7 words from the list. Ex: "something table underrated blackboard"
  • factory|ModelName
  • Will trigger the FactoryMuff::create for the given model and return it's id.
  • Any thing else
  • Will be returned. Ex: kind "tuckemuffin" will become the value of the attribute in the instantiated object.

More help

Read the source code. There is alot of comments there. ;)

or contact me.