adlacruzes/php-factory

Factories to generate classes and arrays with default values

1.4.0 2021-11-04 11:23 UTC

This package is auto-updated.

Last update: 2024-03-26 14:47:34 UTC


README

Minimum PHP Version Packagist Github actions

PHP Factory allows generating classes and arrays with default values and reduce test bloatware.

The main purpose of this library is help with the creation of tests and fixtures.

Table of Contents

Requirements

PHP needs to be a minimum version of PHP 7.2.

Installation

The recommended way to install is through Composer.

composer require adlacruzes/php-factory

Factories

Every factory has to extend Adlacruzes\Factory\Factory to obtain the required methods.

Array factory

The array factory allows generating an array with predefined values.

use Adlacruzes\Factory\Factory;
use Adlacruzes\Factory\Factories\ArrayFactory;
use Adlacruzes\Factory\Factories\FactoryInterface;

class ArraysFactory extends Factory
{
    protected static function setFactory(): FactoryInterface
    {
        return new ArrayFactory(
            [
                'one' => 'one',
                'two' => 'two',
                'three' => 'three',
                'four' => 'four',
            ]
        );
    }
}

You can call ArraysFactory::create() and obtain the defined array:

[
    'one' => 'one',
    'two' => 'two',
    'three' => 'three',
    'four' => 'four',
]

If you want to change some information, you can override the values with a new partial array:

ArraysFactory::create(
    [
        'four' => 'another number'
    ]
);

The returned array will be:

[
    'one' => 'one',
    'two' => 'two',
    'three' => 'three',
    'four' => 'another number',
]

Class factory

The class factory allows generating a concrete class with default constructor parameters.

class ValidClass
{
    private $id;

    private $name;

    private $isEnabled;

    private $createdAt;

    public function __construct($id, $name, $isEnabled, \DateTime $createdAt)
    {
        $this->id = $id;
        $this->name = $name;
        $this->isEnabled = $isEnabled;
        $this->createdAt = $createdAt;
    }

Create a class that extends from Factory.

use Adlacruzes\Factory\Factories\ClassFactory;
use Adlacruzes\Factory\Factories\FactoryInterface;
use Adlacruzes\Factory\Factory;

class ValidClassFactory extends Factory
{
    protected static function setFactory(): FactoryInterface
    {
        return new ClassFactory(
            ValidClass::class,
            [
                'id' => 1,
                'name' => 'name',
                'isEnabled' => true,
                'createdAt' => new \DateTime(),
            ]
        );
    }
}

You can call ValidClassFactory::create() and obtain the defined class:

class ValidClass (4) {
  private $id =>
  int(1)
  private $name =>
  string(4) "name"
  private $isEnabled =>
  bool(true)
  private $createdAt =>
  class DateTime (3) {
    public $date =>
    string(26) "2019-01-01 00:00:00.000000"
    public $timezone_type =>
    int(3)
    public $timezone =>
    string(3) "UTC"
  }
}

If you want to change some information, you can override default values with a new partial array:

ValidClassFactory::create(
    [
        'name' => 'other',
        'isEnabled' => false
    ]
);

The returned class will be:

class ValidClass (4) {
  private $id =>
  int(1)
  private $name =>
  string(4) "other"
  private $isEnabled =>
  bool(false)
  private $createdAt =>
  class DateTime (3) {
    public $date =>
    string(26) "2019-01-01 00:00:00.000000"
    public $timezone_type =>
    int(3)
    public $timezone =>
    string(3) "UTC"
  }
}

Methods

There are three methods for every factory. They can be called with no arguments or can receive an array of values to override the defaults parameters.

create

Create returns an instance of the factory.

create()

Returns the predefined factory.

SampleFactory::create();

create(array)

Returns the predefined factory with the new specified values.

SampleFactory::create(
    [
        'field' => 'value'
    ]
);

create array

CreateArray returns an array of instances of the factory. The number of instances can be specified as first argument. The default number is one.

createArray()

Returns an array of one predefined factory.

SampleFactory::createArray();

createArray(n)

Returns an array of n predefined factories.

SampleFactory::createArray(n);

createArray(n, array)

Returns an array of n predefined factories with the new specified values.

SampleFactory::create(
    n,
    [
        'field' => 'value'
    ]
);

create nullable

CreateNullable returns an instance of the factory with all possibly null values.

createNullable()

Returns the predefined factory with nulls.

SampleFactory::createNullable();

createNullable(array)

Returns the predefined factory with all possibly null values except the new specified ones.

SampleFactory::createNullable(
    [
        'field' => 'value'
    ]
);

Exceptions

An FactoryException is thrown when the parameters to override the default values are not part of the original factory.

For example:

class ArraysFactory extends Factory
{
    protected static function setFactory(): FactoryInterface
    {
        return new ArrayFactory(
            [
                'one' => 'one',
                'two' => 'two',
                'three' => 'three',
                'four' => 'four',
            ]
        );
    }
}

ArraysFactory::create(
    [
        'five' => 'five'
    ]
);
Factory: invalid fields: five