PHPackages                             dansan/fixture-handler - 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. dansan/fixture-handler

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

dansan/fixture-handler
======================

A module to handle fixtures in tests

0.1.1(7y ago)11.0k↓33.3%7MITPHPPHP &gt;=7.1

Since Jul 20Pushed 6y ago1 watchersCompare

[ Source](https://github.com/danielsan80/fixture-handler)[ Packagist](https://packagist.org/packages/dansan/fixture-handler)[ RSS](/packages/dansan-fixture-handler/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (2)Versions (9)Used By (7)

FixtureHandler
==============

[](#fixturehandler)

You can use FixtureHandler in your php test to compact your setup.

It is inspired by Doctrine Fixtures but FixtureHandler is not related only to the model or Doctrine entities.

It is inspired by the Symfony Dependency Injection Container but is not only a way to get services.

FixtureHandler allows you to save the references of data, models and services in a key-value registry.

It allows you to define some Fixtures and load them in a lazy way.

It allows you to define dependencies between Fixtures.

It allows you to define some Scenarios.

Getting started
---------------

[](#getting-started)

Add FixtureHandler as dev requirement in your composer.json

```
composer require dansan/fixture-handler --dev

```

Use it in a PHPUnit TestCase

```
...
use Dan\FixtureHandler\FixtureHandler;
...
$fh = new FixtureHandler();
$fh->setRef('user.mario', new User('Mario');
...
$mario = $fh->getRef('user.mario');
...
```

How it works
------------

[](#how-it-works)

The basic use is...

```
$fh = new FixtureHandler();
$fh->setRef('user.mario', new User('Mario');
$fh->setRef('user.luigi', new User('Luigi');

$mario = $fh->getRef('user.mario');
$luigi = $fh->getRef('user.luigi');

$mario->helps($luigi);
...
```

But it make sense to put the user instantiation into a Fixture...

```
class UserFixture extends AbstractFixture
{
    public function load(): void
    {
        $this->setRef('user.mario', new User('Mario');
        $this->setRef('user.luigi', new User('Luigi');
    }
}
```

...and add it to the FixtureHandler

```
$fh = new FixtureHandler();
$fh->addFixture(new UserFixture());

$mario = $fh->getRef('user.mario');
$luigi = $fh->getRef('user.luigi');

$mario->helps($luigi);
...
```

You can define an item fixture depending on the users (not depending on the UserFixture)...

```
class ItemFixture extends AbstractFixture
{
    public function load(): void
    {
        $this->setRef('item.mushroom', $mushroom = new Item('mushroom'));
        $this->setRef('item.star', $star = new Item('star'));
        $this->setRef('item.flower', $flower = new Item('flower'));

        $this->>getRef('user.mario')->collect($star);
        $this->>getRef('user.mario')->collect($mushroom);
        $this->>getRef('user.luigi')->collect($flower);
    }

    puplic function dependsOn(): array
    {
        return [
            'user.mario',
            'user.luigi',
        ];
    }
}
```

You don't need to add Fixture in the right order: FixtureHandler will load added fixture in the right order thanks to dependsOn() method.

```
$fh = new FixtureHandler();
$fh->addFixture(new ItemFixture());
$fh->addFixture(new UserFixture());

$mario = $fh->getRef('user.mario');
$luigi = $fh->getRef('user.luigi');
$mushroom = $this->getRef('item.mushroom');

$mario->give($luigi, $mushroom);
...
```

> dependsOn() method specifies the list of keys that load() method needs for the getRef() calls. Anyway if dependsOn() is not in sync with load() FixtureHandler will notify you the missing and exceeding keys so you can fix it.

You can create a Scenario, a special fixture depending on nothing, to add several fixtures or set some refs...

```
class MyScenario extends AbstractScenario
{
    public function load()
    {
        $this->setRef('guzzle', new FakeGuzzleClient());

        $this->addFixture(new ItemFixture());
        $this->addFixture(new UserFixture());
    }
}
```

...so you can be ready in a few lines of code

```
$fh = new FixtureHandler();
$fh->addScenario(new MyScenario());

$guzzle = $fh->getRef('guzzle');
...
```

About refs...

- you can set refs,
- you can get refs and specify a default,
- you can get refs and specify to trig an exception if they don't exist,
- you can check if refs exist.

```
$fh = new FixtureHandler();

$fh->setRef('a_key', 'a value');

$value = $fh->getRef('a_key');
$value = $fh->getRef('a_not_existing_key', 'a default value');

$value = $fh->getRefOrFail('a_not_existing_key');

if ($fh->hasRef('a_key')) {
    ...
}
...
```

You can use the trait in your TestCase...

```
MyTest extends TestCase
{
    use FixtureHandlerTrait;

     public function setUp()
     {
        parent::setUp();

        $this->addFixture(new UserFixture());
     }

     /**
      * @test
      */
     public it_works()
     {
        $mario = $this->getRef('user.mario');
        ...
     }

     ...
}
```

Fixture loading will happen when you ask for a ref for the first time...

```
$fh = new FixtureHandler();
$fh->addFixture(new ItemFixture()); // addFixture(new UserFixture()); // getRef('user.mario'); // addFixture(new ItemFixture()); // addFixture(new UserFixture()); // loadFixtures(); // getRef('user.mario'); // addFixture(new ItemFixture());
$fh->addFixture(new UserFixture());
$fh->loadFixtures();
$entityManager->flush();
...
```

That's all!

Development
-----------

[](#development)

Clone the project:

```
git clone
cd

```

Run `cp .env.dist .env` and edit the `.env` file properly.

> The `dc` script will copy your ssh keys from your `~/.ssh` dir into the `php` container.

Start te project and run the tests:

```
./dc up -d
./dc enter
test

```

Credits
-------

[](#credits)

Thanks to [Matiux](https://github.com/matiux) for the Docker images used in the project

###  Health Score

29

—

LowBetter than 60% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity19

Limited adoption so far

Community14

Small or concentrated contributor base

Maturity53

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

Recently: every ~35 days

Total

7

Last Release

2600d ago

PHP version history (2 changes)0.0.0PHP &gt;=7.0

0.0.1PHP &gt;=7.1

### Community

Maintainers

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

---

Top Contributors

[![danielsan80](https://avatars.githubusercontent.com/u/452881?v=4)](https://github.com/danielsan80 "danielsan80 (25 commits)")

---

Tags

composerpackagisttests

### Embed Badge

![Health badge](/badges/dansan-fixture-handler/health.svg)

```
[![Health](https://phpackages.com/badges/dansan-fixture-handler/health.svg)](https://phpackages.com/packages/dansan-fixture-handler)
```

###  Alternatives

[phpspec/prophecy

Highly opinionated mocking framework for PHP 5.3+

8.5k551.7M682](/packages/phpspec-prophecy)[vimeo/psalm

A static analysis tool for finding errors in PHP applications

5.8k77.5M6.7k](/packages/vimeo-psalm)[brianium/paratest

Parallel testing for PHP

2.5k118.8M754](/packages/brianium-paratest)[beberlei/assert

Thin assertion library for input validation in business models.

2.4k96.9M571](/packages/beberlei-assert)[mikey179/vfsstream

Virtual file system to mock the real file system in unit tests.

1.4k108.0M2.7k](/packages/mikey179-vfsstream)[orchestra/testbench

Laravel Testing Helper for Packages Development

2.2k39.1M32.1k](/packages/orchestra-testbench)

PHPackages © 2026

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