#Gos Fixtures Component#
This project is currently in development, so please take care.
Create fixture can be painful mainly when you write them for little entity.You would create them faster as possible and with specific values.
Here an example from doctrine-data-fixture.
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 !
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 :
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
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.
[] Generate reference directly from YAML [] Fully support for Collection
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();
}
PHPUnit 3.5 or newer together with Mock_Object package is required. To setup and run tests follow these steps:
- go to the root directory of the project
- run: composer install --dev
- run: phpunit
The project is under MIT lisence, for more information see the LICENSE file inside the project