2017 © Pedro Peláez
 

library factory

Generate and build model factories for Laravel using classes and methods instead of closures

image

styde/factory

Generate and build model factories for Laravel using classes and methods instead of closures

  • Wednesday, January 17, 2018
  • by sileence
  • Repository
  • 2 Watchers
  • 16 Stars
  • 4 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 2 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

Stydnet Factory Package

This packages allows you to build model factories for Laravel 5.4 using classes and methods instead of closures., (*1)

How to install

Install by running composer require styde/factory:"dev-master" --dev or adding "styde/factory": "dev-master" to the dev dependencies (require-dev) in the project's composer.json file and then running composer update., (*2)

Then create a "factory-classes" directory inside "database" and add the following to the "autoload-dev" section in your composer.json file:, (*3)

      "autoload-dev": {
          "classmap": [
              "database/factory-classes",

And then execute composer dump-autoload in the console., (*4)

Note: you can put the factory classes anywhere you want and of course you can also use PSR-4 if you want to., (*5)

Warning: Laravel loads the database/factories/ multiple times during the test cycle. This will cause a conflict since you cannot override classes. So please don't put the factory classes in the database/factories directory, unless you are completely sure you are not using the factory helper., (*6)

Creating Factory Classes:

The factory classes have the following structure:, (*7)

<?php

class UserFactory extends Styde\Factory\Factory
{
    protected $model = 'App\User';

    public function data()
    {
        return [
            /***/
        ];
    }
}

You can name the factory class whatever you like, but you need to extend Styde\Factory\Factory, (*8)

You also need to define the $model property and assign it the name of the model you want to create, (example: App\User)., (*9)

And you need to add a data() method that will return an array with the default attributes for this model factory:, (*10)

Example:, (*11)

     public function data()
     {
         return [
             'first_name' => $this->firstName,
             'last_name' => $this->lastName,
             'username' => $this->unique()->userName,
             'email' => $this->unique()->safeEmail,
             'remember_token' => str_random(10),
         ];
     }

Notice in the case $this-> is a proxy of $this->faker->, (*12)

States:

Since we are using classes now, states become simple methods inside the factory class:, (*13)

    public function stateDelinquent()
    {
        return ['account_status' => 'delinquent'];
    }

Note these methods need to be prefixed with the "state" and need to return the corresponding attributes., (*14)

Usage:

With factory classe you won't use the factory helper, instead you can write this:, (*15)

UserFactory::create() in order to create a new user and register it in the database., (*16)

Other examples:

  • UserFactory::make() creates a new user without persisting it in the database., (*17)

  • UserFactory::times(3)->create() creates 3 users and persist them in the database., (*18)

  • UserFactory::delinquent()->create() creates a user with delinquent status (following the previous example about states)., (*19)

This package is in development: more tests, features and a stable version will be added later., (*20)

If you want to collaborate please send a pull request or report an issue here in GitHub., (*21)

The Versions

17/01 2018

dev-master

9999999-dev

Generate and build model factories for Laravel using classes and methods instead of closures

  Sources   Download

MIT

The Requires