PHPackages                             directorytree/dummy - 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. directorytree/dummy

ActiveLibrary

directorytree/dummy
===================

v1.3.1(10mo ago)417.7k↓25%[1 PRs](https://github.com/DirectoryTree/Dummy/pulls)MITPHPCI passing

Since Dec 15Pushed 9mo ago1 watchersCompare

[ Source](https://github.com/DirectoryTree/Dummy)[ Packagist](https://packagist.org/packages/directorytree/dummy)[ RSS](/packages/directorytree-dummy/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (5)Dependencies (3)Versions (8)Used By (0)

[![](https://github.com/DirectoryTree/Dummy/raw/master/art/logo.svg)](https://github.com/DirectoryTree/Dummy/blob/master/art/logo.svg)

Generate PHP class instances populated with fake dummy data using [Faker](https://github.com/FakerPHP/Faker)

[![](https://camo.githubusercontent.com/d2a13234bb5cb31c8ef78d496e9bc9b0058ad817dafee4f39a9f89fe69509862/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f6469726563746f7279747265652f64756d6d792f72756e2d74657374732e796d6c3f6272616e63683d6d6173746572267374796c653d666c61742d737175617265)](https://github.com/directorytree/dummy/actions)[![](https://camo.githubusercontent.com/cbb6e4604084e1233d3906745fb58f8bfaaa7f1cb4c0361b2f4786dc9d8ad0e7/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6469726563746f7279747265652f64756d6d792e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/directorytree/dummy)[![](https://camo.githubusercontent.com/2a7b16dad5691f17fff13e2afc5e2f49fd8936988c96995b87782219ca61e48a/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6469726563746f7279747265652f64756d6d792e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/directorytree/dummy)[![](https://camo.githubusercontent.com/de78b9d1421fdfd0e874a2796e63242d0e56dc17b4091768798ba954c1ee74a6/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f6469726563746f7279747265652f64756d6d792e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/directorytree/dummy)

---

Index
-----

[](#index)

- [Requirements](#requirements)
- [Installation](#installation)
- [Introduction](#introduction)
- [Setup](#setup)
    - [HasFactory Trait](#hasfactory-trait)
    - [Class Factory](#class-factory)
- [Usage](#usage)
    - [Factory States](#factory-states)
    - [Factory Callbacks](#factory-callbacks)
    - [Factory Sequences](#factory-sequences)
    - [Factory Collections](#factory-collections)

Requirements
------------

[](#requirements)

- PHP &gt;= 8.0

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

[](#installation)

You can install the package via composer:

```
composer require directorytree/dummy --dev
```

Introduction
------------

[](#introduction)

Consider you have a class representing a restaurant reservation:

```
namespace App\Data;

class Reservation
{
    public function __construct(
        public string $name,
        public string $email,
        public DateTime $date,
    ) {}
}
```

To make dummy instances of this class during testing, you have to manually populate it with dummy data.

This can quickly get out of hand as your class grows, and you may find yourself writing the same dummy data generation code over and over again.

Dummy provides you with a simple way to generate dummy instances of your classes using a simple API:

```
// Generate one instance:
$reservation = Reservation::factory()->make();

// Generate multiple instances:
$collection = Reservation::factory()->count(5)->make();
```

Setup
-----

[](#setup)

Dummy provides you two different ways to generate classes with dummy data.

### HasFactory Trait

[](#hasfactory-trait)

The `HasFactory` trait is applied directly to the class you would like to generate dummy instances of.

To use the `HasFactory` trait, you must implement the `toFactoryInstance` and `getFactoryDefinition` methods:

```
namespace App\Data;

use DateTime;
use Faker\Generator;
use DirectoryTree\Dummy\HasFactory;

class Reservation
{
    use HasFactory;

    /**
     * Constructor.
     */
    public function __construct(
        public string $name,
        public string $email,
        public DateTime $date,
    ) {}

    /**
     * Define the factory's default state.
     */
    protected function getFactoryDefinition(Generator $faker): array
    {
        return [
            'name' => $faker->name(),
            'email' => $faker->email(),
            'datetime' => $faker->dateTime(),
        ];
    }

    /**
     * Create a new instance of the class using the factory definition.
     */
    protected static function toFactoryInstance(array $attributes): static
    {
        return new static(
            $attributes['name'],
            $attributes['email'],
            $attributes['datetime'],
        );
    }
}
```

Once implemented, you may call the `Reservation::factory()` method to create a new dummy factory:

```
$factory = Reservation::factory();
```

#### Dynamic State Methods

[](#dynamic-state-methods)

The `HasFactory` trait supports defining dynamic state methods. You can define state methods in your class using the format `get{StateName}State` and call them dynamically on the factory:

```
namespace App\Data;

use DateTime;
use Faker\Generator;
use DirectoryTree\Dummy\HasFactory;

class Reservation
{
    use HasFactory;

    public function __construct(
        public string $name,
        public string $email,
        public DateTime $datetime,
        public string $status = 'pending',
        public string $type = 'standard',
    ) {}

     // Dynamic state methods...

    public static function getConfirmedState(): array
    {
        return ['status' => 'confirmed'];
    }

    public static function getPremiumState(): array
    {
        return [
            'type' => 'premium',
            'status' => 'confirmed',
        ];
    }

    public static function getCancelledState(): array
    {
        return ['status' => 'cancelled'];
    }

    protected static function toFactoryInstance(array $attributes): self
    {
        return new static(
            $attributes['name'],
            $attributes['email'],
            $attributes['datetime'],
            $attributes['status'] ?? 'pending',
            $attributes['type'] ?? 'standard',
        );
    }

    protected static function getFactoryDefinition(Generator $faker): array
    {
        return [
            'name' => $faker->name(),
            'email' => $faker->email(),
            'datetime' => $faker->dateTime(),
        ];
    }
}
```

You can then use these state methods dynamically:

```
// Create a confirmed reservation
$confirmed = Reservation::factory()->confirmed()->make();

// Create a premium reservation
$premium = Reservation::factory()->premium()->make();

// Chain multiple states
$premiumCancelled = Reservation::factory()->premium()->cancelled()->make();
```

### Class Factory

[](#class-factory)

If you need more control over the dummy data generation process, you may use the `Factory` class.

The `Factory` class is used to generate dummy instances of a class using a separate factory class definition.

To use the `Factory` class, you must extend it with your own and override the `definition` and `generate` methods:

```
namespace App\Factories;

use App\Data\Reservation;
use DirectoryTree\Dummy\Factory;

class ReservationFactory extends Factory
{
    /**
     * Define the factory's default state.
     */
    protected function definition(): array
    {
        return [
            'name' => $this->faker->name(),
            'email' => $this->faker->email(),
            'datetime' => $this->faker->dateTime(),
        ];
    }

    /**
     * Generate a new instance of the class.
     */
    protected function generate(array $attributes): Reservation
    {
        return new Reservation(
            $attributes['name'],
            $attributes['email'],
            $attributes['datetime'],
        );
    }
}
```

Usage
-----

[](#usage)

Once you've defined a factory, you can generate dummy instances of your class using the `make` method:

```
// Using the trait:
$reservation = Reservation::factory()->make();

// Using the factory class:
$reservation = ReservationFactory::new()->make();
```

To add or override attributes in your definition, you may pass an array of attributes to the `make` method:

```
$reservation = Reservation::factory()->make([
    'name' => 'John Doe',
]);
```

To generate multiple instances of the class, you may use the `count` method:

> This will return a `Illuminate\SupportCollection` instance containing the generated classes.

```
$collection = Reservation::factory()->count(5)->make();
```

### Factory States

[](#factory-states)

State manipulation methods allow you to define discrete modifications that can be applied to your dummy factories in any combination.

For example, your `App\Factories\Reservation` factory might contain a `tomorrow`state method that modifies one of its default attribute values:

```
class ReservationFactory extends Factory
{
    // ...

    /**
     * Indicate that the reservation is for tomorrow.
     */
    public function tomorrow(): Factory
    {
        return $this->state(function (array $attributes) {
            return ['datetime' => new DateTime('tomorrow')];
        });
    }
}
```

### Factory Callbacks

[](#factory-callbacks)

Factory callbacks are registered using the `afterMaking` method and allow you to perform additional tasks after making or creating a class. You should register these callbacks by defining a `configure` method on your factory class. This method will be automatically called when the factory is instantiated:

```
class ReservationFactory extends Factory
{
    // ...

    /**
     * Configure the dummy factory.
     */
    protected function configure(): static
    {
        return $this->afterMaking(function (Reservation $reservation) {
            // ...
        });
    }
}
```

### Factory Sequences

[](#factory-sequences)

Sometimes you may wish to alternate the value of a given attribute for each generated class.

You may accomplish this by defining a state transformation as a `sequence`:

```
Reservation::factory()
    ->count(3)
     ->sequence(
        ['datetime' => new Datetime('tomorrow')],
        ['datetime' => new Datetime('next week')],
        ['datetime' => new Datetime('next month')],
    )
    ->make();
```

### Factory Collections

[](#factory-collections)

By default, when making more than one dummy class, an instance of `Illuminate\Support\Collection` will be returned.

If you need to customize the collection of classes generated by a factory, you may override the `collect` method:

```
class ReservationFactory extends Factory
{
    // ...

    /**
     * Create a new collection of classes.
     */
    public function collect(array $instances = []): ReservationCollection
    {
        return new ReservationCollection($instances);
    }
}
```

###  Health Score

38

—

LowBetter than 85% of packages

Maintenance55

Moderate activity, may be stable

Popularity34

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity43

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% 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 ~53 days

Total

5

Last Release

306d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/c6dd082636ff8a08df8dfdcd622ea242374d1d76dd33bceec5a6cd3ae26dc24f?d=identicon)[stevebauman](/maintainers/stevebauman)

---

Top Contributors

[![stevebauman](https://avatars.githubusercontent.com/u/6421846?v=4)](https://github.com/stevebauman "stevebauman (57 commits)")

###  Code Quality

TestsPest

Code StyleLaravel Pint

### Embed Badge

![Health badge](/badges/directorytree-dummy/health.svg)

```
[![Health](https://phpackages.com/badges/directorytree-dummy/health.svg)](https://phpackages.com/packages/directorytree-dummy)
```

PHPackages © 2026

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