family-office/fixtures-library

An easy-to-use library for fixture and dependency loading.

1.0.0 2021-11-28 19:45 UTC

This package is auto-updated.

Last update: 2021-11-28 19:54:33 UTC


README

Fixtures Library

An easy-to-use library for fixture and dependency loading.

⚠️ Disclaimer: This solution was developed for a very specific use-case. The best way to load fixtures in your use-case is most likely covered by Doctrines data fixtures.

Installation

composer require family-office/fixtures-library

Usage

Creating a fixture

Fixtures are regular classes implementing the FixtureInterface.

namespace FamilyOffice\FixturesLibrary\Example\Basic\Fixtures;

use FamilyOffice\FixturesLibrary\FixtureInterface;

final class EarFixture implements FixtureInterface
{
    public function getDependencies(): array
    {
        return [];
    }

    public function load(): void
    {
        // todo: implement data loading
    }
}

All code that should be executed within the fixture should live in the load method.

Sometimes, fixtures need to depend on each other because they must be executed in a certain order.

All dependencies a fixture is dependent on should be returned from the getDependencies method.

namespace FamilyOffice\FixturesLibrary\Example\Basic\Fixtures;

use FamilyOffice\FixturesLibrary\FixtureInterface;

final class ElephantFixture implements FixtureInterface
{
    public function getDependencies(): array
    {
        return [EarFixture::class];
    }

    public function load(): void
    {
        // todo: implement data loading
    }
}

Loading the Fixtures

The quickest and easiest way to load the fixtures is by creating a default chain builder instance.

$defaultChainBuilder = ChainBuilder::createQuickLoader();

The fixtures can then be easily loaded on-the-fly as the dependency tree is built.

$defaultChainBuilder->build([new ElephantFixture()]);

Full Example

A full example of this can be found here.

Advanced Usage

The dependency chain building and loading process can be fully customized to your needs.

An extended documentation on the advanced capabilities of this library can be found here.

License

This project is licensed under the MIT license.
Feel free to do whatever you want with the code!