davidbadura/fixtures

A library to load yaml, json or php fixtures. Easy extendable, resolve object dependency and validate objects.

1.3.2 2022-04-26 15:36 UTC

README

Build Status Latest Stable Version Total Downloads License

Features:

  • Use Services like fzaninotto/Faker, a PHP library that generates fake data for you
  • Resolve object dependency automatically (also bidirectional references)
  • Configurable default fixture converter (constructor, properties, set* and add* methods)
  • Easy to create your own converter
  • Many fixtures loader: Yaml, Json, Toml und PHP
  • Extendable by events (currently with symfony/event-dispatcher)
  • Fixture filtering by tags
  • Object validation (currently with symfony/validator over events)
  • Persist Fixtures with Doctrine ORM, Doctrine MongoDb or Propel
  • Easy to add your own Persister
  • Use an expression-language with symfony/expression-language

Todos:

  • Add cli functionality.
  • Translate documentation ( my english is really bad ;) )
  • Write documentation

Documentation

Installation

You can easily install this package over composer

composer require 'davidbadura/fixtures'

Useage

First, you must create fixtures files in yaml, json, toml, php or mixed. In this example, we have different formats:

YAML

user:
  properties:
    class: DavidBadura\Fixtures\TestObjects\User
    constructor: [name, email]
  data:
    david:
      name: "David Badura"
      email: "d.badura@gmx.de"
      group: ["@group:owner", "@group:developer"]
      role: ["@role:admin"]
    other:
      name: "Somebody"
      email: "test@example.de"
      group: ["@group:developer"]
      role: ["@role:user"]

PHP

<?php

return array(
            'role' =>
            array(
                'properties' =>
                array(
                    'class' => 'DavidBadura\\Fixtures\\TestObjects\\Role',
                ),
                'data' =>
                array(
                    'admin' =>
                    array(
                        'name' => 'Admin',
                    ),
                    'user' =>
                    array(
                        'name' => 'User',
                    ),
                ),
            )
        );

JSON

{
    "group": {
        "properties": {
            "class": "DavidBadura\\Fixtures\\TestObjects\\Group"
        },
        "data": {
            "developer": {
                "name": "Developer",
                "leader": "@@user:david"
            }
        }
    }
}

You can reference to other objects with following expression @{fixture-name}:{fixture-key}. The references will resolved automatically.

What other formats are supported you can read under Loader.

How the fixture manager converte the fixtures to objects can you read in the Converter section. You can use the default fixture converter or write your own converter.

Load Fixtures

Now, you can load the fixtures, crate the objects and persist these in the database. For this, we use the default fixture manager.

use DavidBadura\Fixtures\FixtureManager\FixtureManager;

// $objectManager can be curently Doctrine ORM Entity Manager or other Doctrine DocumentManager like MongoODM or CouchODM
$fixtureManager = FixtureManager::createDefaultFixtureManager($objectManager);

$fixtureManager->load('path/to/fixtures');

You can easily instance your own fixture manager. For more information you can read FixtureManager documentation.