New

Just launched: Vueblocks, a brand-new search engine for Vue packages! Explore Vueblocks →

soyhuce/array-factory

This is my package array-factory
10,438 1
Install
composer require soyhuce/array-factory
Latest Version:2.2.0
PHP:^8.3
Laravel:11 · 12
Older releases:9 · 10
License:MIT
Last Updated:Apr 27, 2026
Links: GitHub  ·  Packagist
Maintainer: SoyHuCe

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.

Related Packages

soyhuce/phpstan-extension

Extra rules for phpstan analysis

23,747 1
soyhuce/dev-tools

Laravel dev-tools

17,145 1
soyhuce/laravel-testing

Helpers for Laravel tests

15,875 3
soyhuce/laravel-json-resources

An opinionated JsonResource for Laravel

15,204 2
soyhuce/laravel-model-injection

Extended Model injection for Laravel

14,848 0

Version History

Version Released PHP Laravel License
2.2.0 ^8.3 ^11.0|^12.0 MIT
2.1.0 ^8.3 ^11.0|^12.0 MIT
2.0.0 ^8.3 ^11.0 MIT
1.1.0 ^8.2 ^10.0 || ^11.0 MIT
1.0.0 ^8.2 ^10.0 MIT