PHPackages                             silvertipsoftware/factorygirl - 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. silvertipsoftware/factorygirl

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

silvertipsoftware/factorygirl
=============================

A port of FactoryGirl to Laravel4/Eloquent

v0.2.1(12y ago)912.6k↓50%1MITPHPPHP &gt;=5.3.0

Since Mar 28Pushed 10y ago1 watchersCompare

[ Source](https://github.com/SilvertipSoftware/factorygirl)[ Packagist](https://packagist.org/packages/silvertipsoftware/factorygirl)[ RSS](/packages/silvertipsoftware-factorygirl/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (2)Versions (4)Used By (0)

SilvertipSoftware/FactoryGirl
=============================

[](#silvertipsoftwarefactorygirl)

FactoryGirl is a (or hopes to be) a relatively faithful port of [thoughtbot/factory\_girl](https://github.com/thoughtbot/factory_girl), a Ruby-land object factory library for testing.

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

[](#installation)

### Composer

[](#composer)

Add `silvertipsoftware/factorygirl` to the `require-dev` section of your `composer.json`:

```
"require-dev": {
    "silvertipsoftware/factorygirl": "dev-master"
}
```

You can obviously choose any available version(s). Run `composer update` to get it.

### Laravel

[](#laravel)

Add the FactoryGirl ServiceProvider to your Laravel application:

```
'providers' => array(
    ...
    'SilvertipSoftware\FactoryGirl\FactoryGirlServiceProvider',
    ...
),
```

(optional) Add the following snippets to your `app/config/app.php` file:

```
'aliases' => array(
    ...
    'Factory' => 'SilvertipSoftware\FactoryGirl\Facades\FactoryGirl',
    ...
),
```

Supported PHP Versions
----------------------

[](#supported-php-versions)

Currently, PHP 5.3+ is supported. PHP 5.4 would make some things nicer, so that may change for future releases.

Documentation
-------------

[](#documentation)

See `FactoryGirl` documentation to get a feel for what it does and is used for. The syntax is hopefully a straightforward port to PHP.

### Factory Definitions

[](#factory-definitions)

The `app/tests/factories.php` file is used to store the factory definitions, and is automatically loaded on the first build/create on an object.

### Basic Factory

[](#basic-factory)

```
Factory::define('room', function($f) {
    return array(
        'name' => 'Meeting Room',
        'capacity' => 5,
        'notes' => 'Great views'
    });
});
```

defines a factory for a `Room` Eloquent model, and sets the `name`,`capacity`, and `notes` attributes to the given values. The model class name is inferred from the factory name. Where that's not intended, the model class name can also be specified:

```
Factory::define('large_room', function($f) {
   return array(
      'name' => 'Banquet Hall',
      'capacity' => 200
   );
}, array(
    'class' => 'Room'
));
```

### Building/Creating Objects

[](#buildingcreating-objects)

With the factory defined above, your test code can do:

```
$room = Factory::build('room');
$another = Factory::build('large_room');
```

to get new room instances, which are not saved to the database. If you want them persisted, use `create`:

```
$room = Factory::create('room');
```

In either case, attributes may be overridden by passing an array as a second parameter:

```
$extra_large_room = Factory::create('large_room', array(
    'capacity' => 1000
));
```

### Sequences

[](#sequences)

Sequences return a value based on a increasing index passed to the closure. Useful for creating uni que attributes in a standard way.

```
Factory::sequence('email', function($n) {
    return 'noreply'.$n'.@somedomain.com';
});
```

Then, in a factory, you can use the sequence by:

```
Factory::define('user', function($f) {
    'username' => 'Joe Public',
    'email' => $f->next('email'),
    'status' => 'active'
});
```

### Associations

[](#associations)

Given a factory for an `account` model, you can associate a `user` model to it with:

```
Factory::define('user', function($f) {
    return array(
        'username' => 'Joe Public',
        'email' => $f->next('email'),
        'account' => $f->associate()
    );
});
```

When a `user` is built, an `account` instance will be created and the `account_id` of `user` will be set to reference the new `account`. Currently, only `belongsTo` relations are supported, and the only "build strategy" is to save the associated object in the database.

Attribute values in the associated object can be overridden by passing an array:

```
        ...
        'account' => $f->associate( array(
            'plan' => 'platinum'
        ))
        ...
```

The factory to use is inferred from the attribute name. If a different factory is desired, pass it to the `associate` call:

```
Factory::define('user', function($f) {
   return array(
      'username' => 'Richie Rich',
      'email' => $f->next('email'),
      'account' => $f->associate('paid_account')
   );
});
```

Or, specify both the factory and overrides:

```
Factory::define('user', function($f) {
   return array(
      'username' => 'Richie Rich',
      'email' => $f->next('email'),
      'account' => $f->associate('paid_account', array(
         'plan' => 'platinum'
      ))
   );
});
```

### Closures as Attributes

[](#closures-as-attributes)

A closure can also be passed as an attribute value, and it is evaluated during model build. This lets you do more complex logic at build-time. This is particularly useful for 3-object associations. For example:

```
Factory::define('room', function($f) {
    return array(
        'name' => 'Meeting Room',
        'account' => $f->associate(),
        'location' => function($room,$f) {
            return $f->associate( array(
                'account' => $room['account']
            );
        }
    );
});
```

Attribute values are evaluated in the order they are given in the factory definition, so swapping `account` and `location` above would not have worked.

### Inheritance

[](#inheritance)

Factories can be chained to reuse common attribute definitions as follows:

```
Factory::define('room', function($f) {
    return array(
        'name' => 'Room 100',
        'capacity' => 5
    );
});

Factory::define('room_with_notes', function($f) {
    return array(
        'notes' => 'This is a nice room.'
    );
}, array(
    'parent' => 'room'
));

$room = Factory::build('room_with_notes');
echo $room->name; // Room 100
echo $room->notes; // This is a nice room.
```

###  Health Score

31

—

LowBetter than 68% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity31

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity50

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 ~0 days

Total

2

Last Release

4435d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/181c1808871c0b143792e2a5db97971850429ff960d46ffa821a954a9aeed745?d=identicon)[info@silvertipsoftware.com](/maintainers/info@silvertipsoftware.com)

---

Top Contributors

[![aksonnic](https://avatars.githubusercontent.com/u/1252956?v=4)](https://github.com/aksonnic "aksonnic (8 commits)")

---

Tags

testinglaravelfactorygirl

### Embed Badge

![Health badge](/badges/silvertipsoftware-factorygirl/health.svg)

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

###  Alternatives

[larastan/larastan

Larastan - Discover bugs in your code without running it. A phpstan/phpstan extension for Laravel

6.4k43.5M5.2k](/packages/larastan-larastan)[laracasts/testdummy

Easy test stubs

4671.4M36](/packages/laracasts-testdummy)[timacdonald/log-fake

A drop in fake logger for testing with the Laravel framework.

4235.9M56](/packages/timacdonald-log-fake)[sti3bas/laravel-scout-array-driver

Array driver for Laravel Scout

971.5M3](/packages/sti3bas-laravel-scout-array-driver)[davestewart/sketchpad

An innovative front-end environment for interactive Laravel development

29512.9k1](/packages/davestewart-sketchpad)[illuminated/testing-tools

Laravel-specific Testing Helpers and Assertions.

5420.4k17](/packages/illuminated-testing-tools)

PHPackages © 2026

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