PHPackages                             ensi/laravel-test-factories - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Testing &amp; Quality](/categories/testing)
4. /
5. ensi/laravel-test-factories

ActiveLibrary[Testing &amp; Quality](/categories/testing)

ensi/laravel-test-factories
===========================

laravel test factories

1.0.12(1w ago)148.7k↓56.5%1MITPHPPHP ^8.1CI passing

Since Mar 14Pushed 1w ago1 watchersCompare

[ Source](https://github.com/ensi-platform/laravel-test-factories)[ Packagist](https://packagist.org/packages/ensi/laravel-test-factories)[ RSS](/packages/ensi-laravel-test-factories/feed)WikiDiscussions master Synced 2d ago

READMEChangelog (10)Dependencies (20)Versions (27)Used By (1)

Laravel Test factories
======================

[](#laravel-test-factories)

[![Latest Version on Packagist](https://camo.githubusercontent.com/6717cf5b2bd7121630766d4d1c6ef8badb6ed4bce9847d982aca16536d28fca1/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f656e73692f6c61726176656c2d746573742d666163746f726965732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/ensi/laravel-test-factories)[![Tests](https://github.com/ensi-platform/laravel-test-factories/actions/workflows/run-tests.yml/badge.svg?branch=master)](https://github.com/ensi-platform/laravel-test-factories/actions/workflows/run-tests.yml)[![Total Downloads](https://camo.githubusercontent.com/a825f308424f27c22700b574899711924c954e68761022a8cecc3005676a9f28/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f656e73692f6c61726176656c2d746573742d666163746f726965732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/ensi/laravel-test-factories)

This package provides factories for typical data structures

Installation
------------

[](#installation)

You can install the package via composer:

```
composer require ensi/laravel-test-factories --dev
```

Version Compatibility
---------------------

[](#version-compatibility)

Laravel Test factoriesLaravelPHP^0.0.1^8.0^8.0^0.1.0^8.0^8.0^0.2.0^8.0^8.0^0.2.4^8.0 || ^9.0 || ^10.0^8.0^0.2.7^8.0 || ^9.0 || ^10.0 || ^11.0^8.0^1.0.0^9.0 || ^10.0 || ^11.0^8.1Basic usage
-----------

[](#basic-usage)

Let's create a factory and extend abstract Factory. All you need is to define `definition` and `make` methods.

```
use Ensi\LaravelTestFactories\Factory;

class CustomerFactory extends Factory
{
    public ?int $id = null;
    public ?FileFactory $avatarFactory = null;
    public ?array $addressFactories = null;

    protected function definition(): array
    {
        return [
            'id' => $this->whenNotNull($this->id, $this->id),
            'user_id' => $this->faker->randomNumber(),
            'is_active' => $this->faker->boolean(),
            'date_start' => $this->faker->dateTime(),
            'avatar' => $this->avatarFactory?->make(),
            'addresses' => $this->executeNested($this->addressFactories, new FactoryMissingValue()),
        ];
    }

    public function make(array $extra = []): CustomerDTO
    {
        static::$index += 1;

        return new CustomerDTO($this->mergeDefinitionWithExtra($extra));
    }

    public function withId(?int $id = null): self
    {
        return $this->immutableSet('id', $id ?? $this->faker->randomNumber());
    }

    public function withAvatar(FileFactory $factory = null): self
    {
        return $this->immutableSet('avatarFactory', $factory ?? FileFactory::new());
    }

    public function includesAddresses(?array $factories = null): self
    {
        return $this->immutableSet('addressFactories', $factories ?? [CustomerAddressFactory::new()]);
    }

    public function active(): self
    {
        return $this->state([
            'is_active' => true,
            'date_start' => $this->faker->dateTimeBetween('-30 years', 'now'),
        ]);
    }
}

// Now we can use Factory like that
$customerData1 = CustomerFactory::new()->make();
$customerData2 = CustomerFactory::new()->active()->make();
$customerData3 = CustomerFactory::new()->withId()->withAvatar(FileFactory::new()->someCustomMethod())->make();
```

As you can see the package uses `fakerphp/faker` to generate test data.

You can override any fields in `make` method:

```
$customerData1 = CustomerFactory::new()->make(['user_id' => 2]);
```

If you target is an array, then you can use a helper method `makeArray`:

```
    public function make(array $extra = []): array
    {
        return $this->makeArray($extra);
    }
```

It's recommended to use `$this->immutableSet` in state change methods to make sure previously created factories are not affected.

### Making several objects

[](#making-several-objects)

```
$customerDataObjects = CustomerFactory::new()->makeSeveral(3); // returns Illuminate\Support\Collection with 3 elements
```

Additional Faker methods
------------------------

[](#additional-faker-methods)

```
$this->faker->randomList(fn() => $this->faker->numerify(), 0, 10) // => ['123', ..., '456']
$this->faker->nullable() // equivalent for $this->faker->optional(), but work with boolean parameter or global static setting
$this->faker->exactly($value) // return $value. Example: $this->faker->nullable()->exactly(AnotherFactory::new()->make())
$this->faker->carbon() // return CarbonInterface
$this->faker->dateMore()
$this->faker->modelId() // return unsigned bit integer value
```

Additional traits
-----------------

[](#additional-traits)

### WithSetPkTrait

[](#withsetpktrait)

If your model has unique index consisting of multiple fields, WithSetPkTrait trait should be used to ensure generated values for these fields are unique.

In order for trait to work, you have to define methods `getPkFields`, `setPk` and `generatePk` and include `getPk` call in `definition`. Following is the example of a factory ('client\_id' and 'location\_id' are fields forming unique index):

```
    class ClientAmountFactory extends BaseModelFactory
{
    use WithSetPkTrait;

    protected $model = ClientAmount::class;

    public function definition(): array
    {
        return array_merge($this->getPk(), [
            'amount' => $this->faker->numberBetween(1, 1_000_000),
        ]);
    }

    public function getPkFields(): array
    {
        return ['client_id', 'location_id'];
    }

    public function setPk(?int $clientId = null, ?string $locationId = null): self // Use in tests to define values
    {
        return $this->state(function () use ($clientId, $locationId) {
            return $this->generatePk($clientId, $locationId);
        });
    }

    protected function generatePk(?int $clientId = null, ?string $locationId = null): array
    {
        $clientIdFormat = $clientId ?: '\d{10}';
        $locationIdFormat = $locationId ?: '[0-9]{1,10}';

        $unique = $this->faker->unique()->regexify("/^{$clientIdFormat}_{$locationIdFormat}");

        $uniqueArr = explode('_', $unique);

        return [
            'client_id' => (int)$uniqueArr[0],
            'location_id' => $uniqueArr[1],
        ];
    }
}
```

*Important note* - fields must be declared in the same order in `getPkFields` and `setPk` methods.

Parent classes
--------------

[](#parent-classes)

### BaseApiFactory

[](#baseapifactory)

This class is the base for your factories of various Api responses/requests.

It also provides the `generateResponseSearch` method, which allows you to generate a response in the `:search` format of the endpoint described [here](https://docs.ensi.tech/guidelines/api#%D1%81%D1%82%D0%B0%D0%BD%D0%B4%D0%B0%D1%80%D1%82%D0%BD%D1%8B%D0%B5-%D0%BC%D0%B5%D1%82%D0%BE%D0%B4%D1%8B-search)

### BaseModelFactory

[](#basemodelfactory)

This class is the base class for your Eloquent model factories

Factories
---------

[](#factories)

- `PaginationFactory` - factory for generating response pieces and pagination requests
- `PromiseFactory` - factory for generating `GuzzleHttp\Promise\PromiseInterface`

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](.github/CONTRIBUTING.md) for details.

### Testing

[](#testing)

1. composer install
2. composer test

Security Vulnerabilities
------------------------

[](#security-vulnerabilities)

Please review [our security policy](.github/SECURITY.md) on how to report security vulnerabilities.

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

56

—

FairBetter than 97% of packages

Maintenance98

Actively maintained with recent releases

Popularity31

Limited adoption so far

Community20

Small or concentrated contributor base

Maturity64

Established project with proven stability

 Bus Factor1

Top contributor holds 68.4% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~47 days

Recently: every ~57 days

Total

26

Last Release

11d ago

Major Versions

0.2.7 → 1.0.02024-06-26

PHP version history (2 changes)0.0.1PHP ^8.0

1.0.0PHP ^8.1

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/8089373?v=4)[Наталия](/maintainers/MsNatali)[@MsNatali](https://github.com/MsNatali)

![](https://avatars.githubusercontent.com/u/7352966?v=4)[Andrey](/maintainers/dimionx)[@DimionX](https://github.com/DimionX)

---

Top Contributors

[![MsNatali](https://avatars.githubusercontent.com/u/8089373?v=4)](https://github.com/MsNatali "MsNatali (39 commits)")[![valerialukinykh](https://avatars.githubusercontent.com/u/123940772?v=4)](https://github.com/valerialukinykh "valerialukinykh (9 commits)")[![DimionX](https://avatars.githubusercontent.com/u/7352966?v=4)](https://github.com/DimionX "DimionX (5 commits)")[![vladksh](https://avatars.githubusercontent.com/u/84857010?v=4)](https://github.com/vladksh "vladksh (1 commits)")[![imamberdievf](https://avatars.githubusercontent.com/u/79088728?v=4)](https://github.com/imamberdievf "imamberdievf (1 commits)")[![egmanoylin](https://avatars.githubusercontent.com/u/77325193?v=4)](https://github.com/egmanoylin "egmanoylin (1 commits)")[![C0rTeZ13](https://avatars.githubusercontent.com/u/120840631?v=4)](https://github.com/C0rTeZ13 "C0rTeZ13 (1 commits)")

###  Code Quality

TestsPest

Static AnalysisPHPStan

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/ensi-laravel-test-factories/health.svg)

```
[![Health](https://phpackages.com/badges/ensi-laravel-test-factories/health.svg)](https://phpackages.com/packages/ensi-laravel-test-factories)
```

###  Alternatives

[backpack/crud

Quickly build admin interfaces using Laravel, Bootstrap and JavaScript.

3.4k3.7M222](/packages/backpack-crud)[statamic/cms

The Statamic CMS Core Package

4.8k3.6M985](/packages/statamic-cms)[orchestra/testbench

Laravel Testing Helper for Packages Development

2.2k42.5M40.8k](/packages/orchestra-testbench)[unopim/unopim

UnoPim Laravel PIM

10.5k2.4k](/packages/unopim-unopim)[blair2004/nexopos

The Free Modern Point Of Sale System build with Laravel, TailwindCSS and Vue.js.

1.2k2.4k](/packages/blair2004-nexopos)[lion/bundle

Lion-framework configuration and initialization package

122.3k4](/packages/lion-bundle)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
