2017 © Pedro Peláez
 

library fixture

Provide solution to process simple fixtures

image

gos/fixture

Provide solution to process simple fixtures

  • Wednesday, October 1, 2014
  • by ProPheT777
  • Repository
  • 2 Watchers
  • 0 Stars
  • 91 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

Gos Fixtures Component

Build Status Scrutinizer Code Quality Code Coverage SensioLabsInsight, (*1)

This project is currently in development, so please take care., (*2)

Create fixture can be painful mainly when you write them for little entity.You would create them faster as possible and with specific values., (*3)

Here an example from doctrine-data-fixture., (*4)

namespace MyDataFixtures;

use Doctrine\Common\DataFixtures\AbstractFixture;
use Doctrine\Common\Persistence\ObjectManager;

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);
    }
}

If you hate store data in php array and dont want take time to create a dedicated component, it's for you. Store their in YAML file and fetch easily your data !, (*5)

How to use

namespace MyDataFixtures;

use Doctrine\Common\DataFixtures\AbstractFixture;
use Doctrine\Common\Persistence\ObjectManager;

class LoadUserRoleData extends AbstractFixture
{
    public function load(ObjectManager $manager)
    {
        //Instanciate the fixture component with the path of folder who contains our YAML data fixture
        //File are retrieve via Symfony Finder Component, so your do path/*/*/folder and all think who is
        //interpreted by the finder
        $fixture = new Fixture('path/to/my/fixture/folder', $this);

        //If you want split / order your fixture you can also add dynamically some folder
        $this->fixture->addDirectory('path/to/my/fixture/another_folder');

        //Now load your specific file
        $fixture->load('myDataFixture.yml');

        //And now you can fetch your data
        foreach($this->fixture->fetch() as $data){
            //attach to your entity with PropertyAccess or by the hand.
        }

    }
}

Here an example of YAML data :, (*6)

database:
    name:
        - 'fr'
        - 'en'
        - 'it'
        - 'es'
    locale:
        - 'fr_FR'
        - 'en_US'
        - 'it_IT'
        - 'es_ES'
    status:
        - 'active'
        - 'active'
        - 'active'
        - 'inactive'
    default:
        - true

Handle one to many with ArrayCollection

collection:
    scope: [ "roles" ]
database:
    username:
        - 'alice'
        - 'bob'
        - 'peter'
    roles: #this is a one to Many
        - 'client'
        - 'editor'
        - 'admin'

Retrieve reference

database:
    user:
        - &alice

Roadmap

Actually this component not cover all features you can meet. You can't create reference from YAML, and collection are not fully supported, currently they just convert array into ArrayCollection because we dont have meet this use case at this time, but it's we will, so we plan., (*7)

[] Generate reference directly from YAML [] Fully support for Collection, (*8)

Concret example

public function load(ObjectManager $manager)
{
    $fixture = new Fixture('src/*/*/DataFixtures/YML/');
    $this->localeManager = $this->container->get('gos.i18n_bundle.locale_entity.manager');

    $this->fixture->load('LocaleData.yml', $this);

    foreach ($this->fixture->fetch() as $data) {
        $locale = $this->localeManager->create($data);
        $this->setReference($locale->getName(), $locale);
    }

    $this->localeManager->save();
}

You can also see :

Running the tests

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

  • go to the root directory of the project
  • run: composer install --dev
  • run: phpunit

License

The project is under MIT lisence, for more information see the LICENSE file inside the project, (*10)

The Versions

01/10 2014

dev-master

9999999-dev

Provide solution to process simple fixtures

  Sources   Download

MIT

The Requires

 

by Johann Saunier

fixtures easy simple tool doctrine fixtures