davidbadura / fixtures
A library to load yaml, json or php fixtures. Easy extendable, resolve object dependency and validate objects.
Installs: 67 013
Dependents: 2
Suggesters: 0
Security: 0
Stars: 22
Watchers: 8
Forks: 11
Open Issues: 6
Requires
- php: ^7.4|^8.0
- symfony/event-dispatcher: ^4.4|^5.3|^6.0
- symfony/finder: ^4.4|^5.3|^6.0
Requires (Dev)
- doctrine/collections: ^1.6
- doctrine/common: ^2.9|^3.0
- fakerphp/faker: ^1.10
- jamesmoss/toml: ^1.1
- phpspec/prophecy-phpunit: ^2.0.1
- phpunit/phpunit: ^9.5.4
- symfony/config: ^4.4.34|^5.3|^6.0
- symfony/console: ^4.4.34|^5.3|^6.0
- symfony/dependency-injection: ^4.4.34|^5.3|^6.0
- symfony/expression-language: ^4.4.34|^5.3|^6.0
- symfony/http-kernel: ^4.4.34|^5.3|^6.0
- symfony/security-core: ^4.4.34|^5.3|^6.0
- symfony/validator: ^4.4.34|^5.3|^6.0
- symfony/yaml: ^4.4.34|^5.3|^6.0
Suggests
- doctrine/mongodb-odm: ^1.2
- doctrine/orm: ^2.0|3.0
- fakerphp/faker: ^1.10
- jamesmoss/toml: ^1.1
- symfony/expression-language: ^4.4.34|^5.3|^6.0
- symfony/http-kernel: ^4.4.34|^5.3|^6.0
- symfony/validator: ^4.4.34|^5.3|^6.0
- symfony/yaml: ^4.4.34|^5.3|^6.0
Conflicts
- doctrine/common: <2.9
- doctrine/persistence: <1.3
- fakerphp/faker: <1.10
- fzaninotto/faker: *
- symfony/expression-language: <4.4.34
- symfony/http-kernel: <4.4.34
- symfony/validator: <4.4.34
- symfony/yaml: <2.9
This package is auto-updated.
Last update: 2024-11-06 10:38:53 UTC
README
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.