PHPackages                             xi/fixtures - 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. xi/fixtures

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

xi/fixtures
===========

Convenient creation of Doctrine entities in tests. Like Ruby's FactoryGirl.

1.1.1(13y ago)817.0k[2 issues](https://github.com/xi-project/xi-fixtures/issues)BSD-3-ClausePHPPHP &gt;=5.3.0

Since May 6Pushed 13y ago14 watchersCompare

[ Source](https://github.com/xi-project/xi-fixtures)[ Packagist](https://packagist.org/packages/xi/fixtures)[ Docs](http://github.com/xi-project/xi-fixtures)[ RSS](/packages/xi-fixtures/feed)WikiDiscussions master Synced 4w ago

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

Xi Fixtures
===========

[](#xi-fixtures)

Xi Fixtures provides convenient and scalable creation of Doctrine entities in tests. If you're familiar with [FactoryGirl](https://github.com/thoughtbot/factory_girl) for Ruby, then this is essentially the same thing for Doctrine/PHP.

[![Build Status](https://camo.githubusercontent.com/6db327dc8a805a30b62a2b75ac115e75ad5721ae6a9d932d7efe6a75e591826e/68747470733a2f2f7472617669732d63692e6f72672f78692d70726f6a6563742f78692d66697874757265732e706e673f6272616e63683d6d6173746572)](https://travis-ci.org/xi-project/xi-fixtures)

### In a nutshell

[](#in-a-nutshell)

Imagine we're setting up a test and need 3 users in the database. With Xi Fixtures we can specify in one place that each user needs a unique username and needs to belong to a group (via a one-to-many relation):

```
$this->factory
    ->define('User')
    ->sequence('username', 'user_%d')
    ->field('administrator', false)
    ->reference('group', 'Group');
```

Now in our tests we can simply say:

```
$user1 = $this->factory->get('User');
$user2 = $this->factory->get('User');

// We can selectively override attributes
$user3 = $this->factory->get('User', array('administrator' => true));

testStuffWith($user1, $user2, $user3);
```

### Motivation

[](#motivation)

Many web applications have non-trivial database structures with lots of dependencies between tables. One component of such an application might deal with entities from only one or two tables, but those entities may depend on a complex entity graph to be useful or to pass validation.

For instance, a `User` may be a member of a `Group`, which is part of an `Organization`, which in turn depends on five different tables describing who-knows-what about the organization. You are writing a component that changes the user's password and are currently uninterested in groups, organizations and their dependencies. How do you set up your test?

1. Do you create all dependencies for `Organization` and `Group` to get a valid `User` in your `setUp()`? No, that would be horribly tedious and verbose.
2. Do you make a shared fixture for all your tests that includes an example organization with satisifed dependencies? No, having loads of tests depend on a single fixture makes changing that fixture later difficult.
3. Do you use mock objects? Sure, but in many cases, however, the code you're testing interacts with the entities in such a complex way that mocking them sufficiently is impractical.

Xi Fixtures is a middle ground between *(1)* and *(2)*. You specify how to generate your entities and their dependencies in one central place but explicitly create them in your tests, overriding only the fields you want.

### Tutorial

[](#tutorial)

We'll assume you have a base class for your tests that sets up a fresh `EntityManager` connected to a minimally initialized blank test database. A simple factory setup looks like this.

```
