The goal of this Package is to enable the rapid creation of objects for the purpose of testing. Basically a "factory_girl_rails" simplified for use with PHP.
MIT
In the require
key of composer.json
file add the following
"zizaco/factory-muff": "dev-master"
Run the Composer update command
$ composer update
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.
Example:
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',
'slug' => 'call|makeSlug|string',
);
// Relashionship with user
public function user()
{
return $this->belongs_to('User');
}
// this static method generates the 'slug'
public static function makeSlug($factory_muff_generated_string)
{
$base = strtolower($factory_muff_generated_string);
return preg_replace('|[^a-z0-9]+|', '-', $base);
}
To create model instances do the following:
<?php
class TestUserModel extends PHPUnit_Framework_TestCase {
public function __construct()
{
// Prepare FactoryMuff
$this->factory = new FactoryMuff;
}
public function testSampleFactory()
{
// Creates a new instance
$message = $this->factory->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:
$message->user->username;
// And you can also get attributes for a new
// instance
$new_message = new Message( $this->factory->attributesFor( 'Message' ) )
// For both methods (create and attributesFor
// you can pass fixed attributes. Those will be
// merged into the object before save.
$muffin_message = $this->factory->create(
'Message', array(
'subject' => 'About Muffin',
'message' => 'Its tasty!',
),
);
}
- string
- Grab a random word from the wordlist. Ex: "bucket","mouse","laptop","America"
- An word from the wordlist + domain. Ex: "[email protected]", "[email protected]"
- text
- A text of about 7 words from the list. Ex: "something table underrated blackboard"
- factory|ModelName
- Will trigger the create for the given model and return it's id.
- call|staticMethodName
- Will call staticMethodName() on the class being created.
- call|staticMethodName|string
- Will call staticMethodName() on the class being created, and pass in a parameter (use any of the kinds supported by FactoryMuff such as email, text, etc)
- call|staticMethodName|factory|User
- The gotcha here is that staticMethodName() will be passed the model instance here, rather than the id which is the case with the normal "factory|User" style.
- Any thing else
- Will be returned. Ex: kind "tuckemuffin" will become the value of the attribute in the instantiated object.
If a model cannot be saved to the database, for example if it fails validation through a library like Ardent, a Zizaco\FactoryMuff\SaveException will be raised.
Read the source code. There is alot of comments there. ;)
or contact me.