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.
You can install the package via composer:
composer require --dev soyhuce/array-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
$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'] ])
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']
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.
$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)])
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]
Runs tests.
composer test
Runs all pre-commit checks.
composer all
Please see CHANGELOG for more information on what has changed recently.
Please review our security policy on how to report security vulnerabilities.
The MIT License (MIT). Please see License File for more information.