Giter VIP home page Giter VIP logo

factory-muff's Introduction

FactoryMuff

factory muff poster

Build Status ProjectStatus

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.

License

MIT

Installation

In the require key of composer.json file add the following

"zizaco/factory-muff": "dev-master"

Run the Composer update command

$ composer update

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

Usage

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!',
            ),
        );
    }

Kinds of attribute supported

  • string
  • Grab a random word from the wordlist. Ex: "bucket","mouse","laptop","America"
  • email
  • 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.

Save Failures

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.

More help

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

or contact me.

factory-muff's People

Contributors

jcldavid avatar pete-otaqui avatar zizaco avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.