PHPackages                             robier/forge-object - 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. [Utility &amp; Helpers](/categories/utility)
4. /
5. robier/forge-object

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

robier/forge-object
===================

Forge objects with ease

08.3kPHP

Since May 16Pushed 7y ago1 watchersCompare

[ Source](https://github.com/robier/forge-object)[ Packagist](https://packagist.org/packages/robier/forge-object)[ RSS](/packages/robier-forge-object/feed)WikiDiscussions master Synced 4d ago

READMEChangelogDependenciesVersions (1)Used By (0)

Forge object
============

[](#forge-object)

 [ ![Build Status](https://camo.githubusercontent.com/ecee8ec3b4a02a21ddf6cf4f5149a112fd9c7273bb97bdd50a46a19c09f090a3/68747470733a2f2f7472617669732d63692e6f72672f726f626965722f666f7267652d6f626a6563742e7376673f6272616e63683d6d6173746572) ](https://travis-ci.org/robier/forge-object) [ ![](https://camo.githubusercontent.com/8fe67c9778230a1461f302774937ac6a20f6d8791e390926219dacbc6ec991d3/68747470733a2f2f636f6465636f762e696f2f67682f726f626965722f666f7267652d6f626a6563742f6272616e63682f6d61737465722f67726170682f62616467652e737667) ](https://codecov.io/gh/robier/forge-object) [![Mutation score](https://camo.githubusercontent.com/677bde36ff9fc95eee2943f12bd162b4267e819192d392a230e842e0c208f5a5/68747470733a2f2f62616467652e737472796b65722d6d757461746f722e696f2f6769746875622e636f6d2f726f626965722f666f7267652d6f626a6563742f6d6173746572)](https://camo.githubusercontent.com/677bde36ff9fc95eee2943f12bd162b4267e819192d392a230e842e0c208f5a5/68747470733a2f2f62616467652e737472796b65722d6d757461746f722e696f2f6769746875622e636f6d2f726f626965722f666f7267652d6f626a6563742f6d6173746572) [![PHPStan](https://camo.githubusercontent.com/8200e616fde4404cb348e95bb987c65291059e5f89521837e88fc4436073298c/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048505374616e2d656e61626c65642d627269676874677265656e2e737667)](https://camo.githubusercontent.com/8200e616fde4404cb348e95bb987c65291059e5f89521837e88fc4436073298c/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048505374616e2d656e61626c65642d627269676874677265656e2e737667) [![MIT](https://camo.githubusercontent.com/784362b26e4b3546254f1893e778ba64616e362bd6ac791991d2c9e880a3a64e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c6963656e73652d4d49542d677265656e2e737667)](https://camo.githubusercontent.com/784362b26e4b3546254f1893e778ba64616e362bd6ac791991d2c9e880a3a64e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c6963656e73652d4d49542d677265656e2e737667)

Easily forge your own object for testing purposes (not intended for production code).

Heavily inspired by [Larvel's factories](https://laravel.com/docs/5.8/database-testing) but with one big difference, Laravel's factories are used for only creating or persisting models, but this library can create any type of object (model, entity, value object...). This library do not know how to persist something.

Library is framework agnostic and can be implemented in any framework.

Every test has a 3 main parts:

- `setup` - setting up application for test
- `test` - actual asserts
- `cleanup` - killing references in code or database

This library aims to reduce setup part of tests and also to reduce time needed for adding new data to object, especially when you use that object a lot in your tests.

If you find some feature that you would like to see in this library feel free to contribute or open an issue :).

### Install

[](#install)

Library can be installed via composer:

```
composer require --dev robier/forge-object
```

### Usage

[](#usage)

First you need to register forge for your object:

```
$manager = new Robier\ForgeObject\Manager();
$manager->register(\stdClass::class, static function(): \stdClass{
    // apply random valid data to object
    $object = new \stdClass();
    $object->active = (bool)rand(0, 1);
    $object->admin = (bool)rand(0, 1);

    return $object;
})
```

After registration you can add states to object:

```
$manager->registerState(\stdClass::class, 'active', static function(\stdClass $item): void{
    // change random data with exact data in states
    $item->active = true;
})

$manager->registerState(\stdClass::class, 'admin', static function(\stdClass $item): void{
    // change random data with exact data in states
    $item->admin = true;
})
```

Let's say you need one random stdClass object in your test:

```
$oneStdClassObject = $manager->new(\stdClass::class)->one();
```

Or you need a random stdClass that is active:

```
$oneStdClassObject = $manager->new(\stdClass::class)->state('active')->one();
```

Maybe you need multiple random stdClass objects that are active, let's say 15:

```
$manyActiveStdClassObjects = $manager->new(\stdClass::class)->state('active')->many(15);
```

States can be combined and they are applied in order they are provided to `state` method. If you want random stdClass that is also an active and admin you would do it like this:

```
$oneStdClassObject = $manager->new(\stdClass::class)->state('active', 'admin')->one();
```

### Local development

[](#local-development)

Build docker with command

```
docker/build
```

Run any command inside docker

```
docker/run {script}
```

for example:

```
docker/run composer install
```

Run all tests:

```
docker/run composer run test
```

###  Health Score

23

—

LowBetter than 27% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity18

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity37

Early-stage or recently created project

 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.

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/3607521?v=4)[Robert](/maintainers/robier)[@robier](https://github.com/robier)

---

Top Contributors

[![robier](https://avatars.githubusercontent.com/u/3607521?v=4)](https://github.com/robier "robier (9 commits)")

### Embed Badge

![Health badge](/badges/robier-forge-object/health.svg)

```
[![Health](https://phpackages.com/badges/robier-forge-object/health.svg)](https://phpackages.com/packages/robier-forge-object)
```

###  Alternatives

[asyncphp/doorman

Child process management

3221.2M2](/packages/asyncphp-doorman)[maantje/charts

SVG Charts in PHP

32694.4k2](/packages/maantje-charts)[glenscott/url-normalizer

Syntax based normalization of URL's

101922.4k6](/packages/glenscott-url-normalizer)[org_heigl/hyphenator

Word-Hyphenation for PHP based on the TeX-Hyphenation algorithm

541.3M14](/packages/org-heigl-hyphenator)[genealabs/laravel-overridable-model

Provide a uniform method of allowing models to be overridden in Laravel.

92398.0k2](/packages/genealabs-laravel-overridable-model)[yiithings/yii2-dotenv

PHP DotEnv for Yii2 framework

40574.5k7](/packages/yiithings-yii2-dotenv)

PHPackages © 2026

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