2017 © Pedro Peláez
 

library neo4j-data-fixtures

Data Fixtures for Neo4j OGM, based on Doctrine Data Fixtures

image

expio/neo4j-data-fixtures

Data Fixtures for Neo4j OGM, based on Doctrine Data Fixtures

  • Saturday, July 6, 2013
  • by sgarner
  • Repository
  • 2 Watchers
  • 1 Stars
  • 173 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 208 Forks
  • 0 Open issues
  • 3 Versions
  • 0 % Grown

The README.md

Neo4j Data Fixtures Extension

This extension aims to provide a simple way to manage and execute the loading of data fixtures for the Neo4j OGM. You can write fixture classes by implementing the Expio\Common\Neo4jDataFixtures\FixtureInterface interface:, (*1)

namespace MyDataFixtures;

use Kwattro\Neo4j\GraphManager;
use Expio\Common\Neo4jDataFixtures\FixtureInterface;

class LoadUserData implements FixtureInterface
{
    public function load(GraphManager $manager)
    {
        $user = new User();
        $user->setUsername('jwage');
        $user->setPassword('test');

        $manager->persist($user);
        $manager->flush();
    }
}

Now you can begin adding the fixtures to a loader instance:, (*2)

use Expio\Common\Neo4jDataFixtures\Loader;
use MyDataFixtures\LoadUserData;

$loader = new Loader();
$loader->addFixture(new LoadUserData);

You can load a set of fixtures from a directory as well:, (*3)

$loader->loadFromDirectory('/path/to/MyDataFixtures');

You can get the added fixtures using the getFixtures() method:, (*4)

$fixtures = $loader->getFixtures();

Now you can easily execute the fixtures:, (*5)

use Expio\Common\Neo4jDataFixtures\Executor\OGMExecutor;
use Expio\Common\Neo4jDataFixtures\Purger\OGMPurger;

$purger = new OGMPurger();
$executor = new OGMExecutor($em, $purger);
$executor->execute($loader->getFixtures());

If you want to append the fixtures instead of purging before loading then pass true to the 2nd argument of execute:, (*6)

$executor->execute($loader->getFixtures(), true);

Sharing objects between fixtures

In case if fixture objects have relations to other fixtures, it is now possible to easily add a reference to that object by name and later reference it to form a relation. Here is an example fixtures for Role and User relation, (*7)

namespace MyDataFixtures;

use Expio\Common\Neo4jDataFixtures\AbstractFixture;
use HireVoice\Neo4j\EntityManager;

class LoadUserRoleData extends AbstractFixture
{
    public function load(ObjectManager $manager)
    {
        $adminRole = new Role();
        $adminRole->setName('admin');

        $anonymousRole = new Role;
        $anonymousRole->setName('anonymous');

        $manager->persist($adminRole);
        $manager->persist($anonymousRole);
        $manager->flush();

        // store reference to admin role for User relation to Role
        $this->addReference('admin-role', $adminRole);
    }
}

And the User data loading fixture:, (*8)

namespace MyDataFixtures;

use Expio\Common\Neo4jDataFixtures\AbstractFixture;
use HireVoice\Neo4j\EntityManager;

class LoadUserData extends AbstractFixture
{
    public function load(ObjectManager $manager)
    {
        $user = new User();
        $user->setUsername('jwage');
        $user->setPassword('test');
        $user->setRole(
            $this->getReference('admin-role') // load the stored reference
        );

        $manager->persist($user);
        $manager->flush();

        // store reference of admin-user for other Fixtures
        $this->addReference('admin-user', $user);
    }
}

Fixture ordering

Notice that the fixture loading order is important! To handle it manually implement one of the following interfaces:, (*9)

OrderedFixtureInterface

Set the order manually:, (*10)

namespace MyDataFixtures;

use Expio\Common\Neo4jDataFixtures\AbstractFixture;
use Expio\Common\Neo4jDataFixtures\OrderedFixtureInterface;
use HireVoice\Neo4j\EntityManager;

class MyFixture extends AbstractFixture implements OrderedFixtureInterface
{
    public function load(ObjectManager $manager)
    {}

    public function getOrder()
    {
        return 10; // number in which order to load fixtures
    }
}

DependentFixtureInterface

Provide an array of fixture class names:, (*11)

namespace MyDataFixtures;

use Expio\Common\Neo4jDataFixtures\AbstractFixture;
use Expio\Common\Neo4jDataFixtures\DependentFixtureInterface;
use HireVoice\Neo4j\EntityManager;

class MyFixture extends AbstractFixture implements DependentFixtureInterface
{
    public function load(ObjectManager $manager)
    {}

    public function getDependencies()
    {
        return array('MyDataFixtures\MyOtherFixture'); // fixture classes fixture is dependent on
    }
}

class MyOtherFixture extends AbstractFixture
{
    public function load(ObjectManager $manager)
    {}
}

Notice the ordering is relevant to Loader class., (*12)

Running the tests:

PHPUnit 3.5 or newer together with Mock_Object package is required. To setup and run tests follow these steps:, (*13)

  • go to the root directory of data-fixtures
  • run: git submodule init
  • run: git submodule update
  • copy the phpunit config cp phpunit.xml.dist phpunit.xml
  • run: phpunit

The Versions

06/07 2013

dev-master

9999999-dev https://github.com/sgarner/neo4j-data-fixtures

Data Fixtures for Neo4j OGM, based on Doctrine Data Fixtures

  Sources   Download

MIT

The Requires

 

by Simon Garner

database

20/09 2012

v1.0.0-ALPHA3

1.0.0.0-alpha3 http://www.doctrine-project.org

Data Fixtures for all Doctrine Object Managers

  Sources   Download

MIT

The Requires

  • php >=5.3.2

 

database

25/01 2012

v1.0.0-ALPHA2

1.0.0.0-alpha2 http://www.doctrine-project.org

Data Fixtures for all Doctrine Object Managers

  Sources   Download

LGPL

The Requires

  • php >=5.3.2

 

database