PHPackages                             apinstein/fixturenator - 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. apinstein/fixturenator

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

apinstein/fixturenator
======================

A factory-based fixture generator for PHP.

77.0k1[1 issues](https://github.com/apinstein/fixturenator/issues)PHP

Since Dec 28Pushed 8y ago1 watchersCompare

[ Source](https://github.com/apinstein/fixturenator)[ Packagist](https://packagist.org/packages/apinstein/fixturenator)[ RSS](/packages/apinstein-fixturenator/feed)WikiDiscussions master Synced today

READMEChangelogDependenciesVersions (1)Used By (0)

Fixturenator
============

[](#fixturenator)

Fixturenator is a factory-style test data generator inspired by [factory\_girl](http://github.com/thoughtbot/factory_girl), thoughtfully ported to PHP.

Download / Installation
=======================

[](#download--installation)

pear install apinstein.pearfarm.org/fixturenator

require 'fixturenator/Fixturenator.php';

[Source Code](http://github.com/apinstein/fixturenator) available on GitHub.

Why Factories?
==============

[](#why-factories)

There are many patterns for preparing test data for running tests against, but in my experience they all have problems in terms of maintenance or ease of creation.

The factory pattern provides a DRY way to specify basic valid data for objects, and then to re-use that specification as a prototype for making similar objects.

The result is test data generation that doesn't break in 1000 places when you add a column to a model object, or change a validator. Fix the core factory definition and all your tests are working again!

Usage
=====

[](#usage)

Each factory is identified by a name, and some default data for the created objects.

```
// Static data for each instance
Fixturenator::define('User', array('username' => 'joe'));

// Closure-passing syntax on 5.3
Fixturenator::define('User', function($factory) {
  $factory->username = 'joe';
});

// Closure-passing syntax on 5.2
Fixturenator::define('User', create_function('$factory', '
  $factory->username = 'joe';
'});

```

It is recommended for you to have one factory per class that provides the minimal valid data for that class. Additional factories can be created via inheritance to easily create common scenario data.

Factory names must be unique.

**Using Factories**

There are four main ways to create data based on factories:

- create - creates a new, unsaved instance
- saved - same as create, but calls the defined save method ('save' by default)
- stub - creates a MagicArray instance for easy stubbing functionality.
- asArray - a php array (hash) with all data values set by the factory

Examples:

```
// Returns an unsaved User instance
$user = Fixturenator::create('User');

// Customer the generated object
$user = Fixturenator::create('User', array('password' => '1234'));

// returns a "saved" User object, as if you called $user->save($dbCon)
$user = Fixturenator::saved('User', array(), $dbCon);

```

**Dynamically Generated Attributes**

You might want to generate some data on the fly each time:

```
// the 'return ...' gets turned into a lambda; on 5.3 you can pass true lambda
$user = Fixturenator::create('User', array('password' => 'return rand(1000,9999);'));

// Generate one attribute based on others
$user = Fixturenator::create('User', array('email' => 'return "{$o->username}@domain.com";'));

```

**Sequences**

Sequences are very useful for generating unique and predictable data:

```
Fixturenator::createSequence('username', 'return "username{$n}";');
Fixturenator::getSequence('username')->next();    // => username1
Fixturenator::getSequence('username')->next();    // => username2

```

You can use these in your defines like so:

```
Fixturenator::define(TestObject, array(
    'username' => new FixturenatorSequence('return "username{$n}";')
));
Fixturenator::define(TestObject, array(
    'username' => 'return Fixturenator::getSequence('username')->next()'
));

```

Thanks
======

[](#thanks)

Thanks to the factory\_girl team for all the research and time that went in to figuring out the factory pattern and the great factory\_girl implementation!

###  Health Score

25

—

LowBetter than 37% of packages

Maintenance17

Infrequent updates — may be unmaintained

Popularity24

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity41

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.

### Community

Maintainers

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

---

Top Contributors

[![apinstein](https://avatars.githubusercontent.com/u/34889?v=4)](https://github.com/apinstein "apinstein (17 commits)")

### Embed Badge

![Health badge](/badges/apinstein-fixturenator/health.svg)

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

###  Alternatives

[ralphjsmit/laravel-seo

A package to handle the SEO in any Laravel application, big or small.

841433.3k21](/packages/ralphjsmit-laravel-seo)

PHPackages © 2026

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