/array-factory

Primary LanguagePHPMIT LicenseMIT

This is array-factory package

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status GitHub PHPStan Action Status Total Downloads

When testing your application, you may need to write the same data structures (lists, collections, Etc.) to populate models or something else. These often duplicated in multiple test files.

A bit like Laravel's Factory classes, ArrayFactory permits to generate and reuse custom data structures and usable among your tests.

Installation

You can install the package via composer:

composer require --dev soyhuce/array-factory

Usage

Create a factory

use Soyhuce\ArrayFactory;

$factory = new ArrayFactory(['foo' => 'bar']); // from an array
$factory = ArrayFactory::new(['foo' => 'bar']); // from static method new
$factory = ArrayFactory::new(fn () => ['foo' => 'bar']); // with a callable
$factory = ArrayFactory::new(fn () => ['foo' => faker()->word() ]); // using \Faker\Generator generation

Use it

$factory->createOne();
// ['foo' => 'bar']
$factory->createMany(['foo' => 'qux'], ['foo' => 'bar'])
// [['foo' => 'qux'], ['foo' => 'bar']]
$factory->count(2)->asCollection();
// Illuminate\Support\Collection([ ['foo' => 'bar'], ['foo' => 'bar'] ])

Add states

You may need to define states and use it in a specific context.

$factory = new ArrayFactory(
    definition: ['foo' => 'bar'],
    states: [
        'qux' => ['foo' => 'qux'],
    ]
);

$factory->createOne();
// ['foo' => 'bar']
$factory->state('qux')->createOne();
// ['foo' => 'qux']

Add custom Collection

You can transform the array into a custom Collection.

$factory = new ArrayFactory(
    collection: FooCollection::class,
);

$foo = FooFactory::new()->asCollection();

You can also transform the array into a custom Data object by defining the Spatie\LaravelData\Data class.

Add custom \Spatie\LaravelData\Data

$factory = new ArrayFactory(
    data: FooData::class,
);

$factory = FooFactory::new()->asData();

It allows get many results as collection of datas

$datas = $factory->manyAsDataCollection(['id' => 2], ['id' => 3]);
// Collection([FooData(id: 2), FooData(id: 3)])

Create an extended class

class FooFactory extends ArrayFactory
{    
    protected string $collection = FooCollection::class;
    
    protected string $data = FooData::class;
    
    public function definition(): array
    {
        return [
            'id' => 1,
            'activated' => true,
            'message' => faker()->sentence(),
            'value' => 0,
        ];
    }

    public function id(int $id): static
    {
        return $this->state(['id' => $id]);
    }

    public function activated(bool $activated = true): static
    {
        return $this->state(['activated' => $activated]);
    }
}

Then, you can simply use it in your tests.

    $foo1 = FooFactory::new()->createOne(); 
    // ['id' => 1, 'activated' => true, 'value' => 0]
    $foo1 = FooFactory::new()->createOne(['value' => 1]]);
     // ['id' => 1, 'activated' => true, 'value' => 1]
    $foo2 = FooFactory::new()->id(2)->createOne();
     // ['id' => 2, 'activated' => true, 'value' => 0]
    $foo3 = FooFactory::new()->activated(false)->createOne();
     // ['id' => 1, 'activated' => false, 'value' => 0]

Testing

Runs tests.

composer test

Runs all pre-commit checks.

composer all

Changelog

Please see CHANGELOG for more information on what has changed recently.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.