Doctrine Fixtures
with Fake Data
Doctrine Fixtures
● Installation
○ composer.json
{
"require-dev": {
"doctrine/doctrine-fixtures-bundle": "dev-master"
}
}
○ php composer.phar update doctrine/doctrine-fixtures-
bundle
○ app/appKernel.php
if (in_array($this->getEnvironment(), array('dev', 'test'))) {
/* ... other bundles */
$bundles[] = new DoctrineBundleFixturesBundleDoctrineFixturesBundle();
}
DoctrineFixtures Structure
● Source folder : {BundlesName}DataFixturesORMLoad{EntityName}Data.php
● class naming : Load{EntityName}Data
● extends class:
○ DoctrineCommonDataFixturesAbstractFixture
○ method: Load(DoctrineCommonPersistenceObjectManager $em)
● Implements interface:
○ DoctrineCommonDataFixturesOrderedFixtureInterface
○ method: getOrder()
● Internal method
○ addReference($name,$entityObject);
○ hasReference($name)
○ getReference($name)
● Command: php app/console doctrine:fixtures:load
fzaninotto/Faker
● Installation
php composer.phar require “fzaninotto/faker dev-master”
● Base Usage
$faker = FakerFactory::create();
$name = $faker->name;
$phone = $faker->phoneNumber;
$url = $faker->url;
fzaninotto/Faker - Formats
Localation
● Person
● Address
● Company
● PhoneNumber
General
● Lorem
● Internet / UserAgent
● File
● Color
● Uuid
● DateTime
● Base(Random, Regex)
● Miscellaneous
● CreditCard
zaninotto/Faker
● Localization
$faker = FakerFactory::create('en_US');
$faker = FakerFactory::create('fr_FR');
● Seed
$faker = FakerFactory::create();
$faker->seed(1234);
fzaninotto/Faker
Unique and Optional modifiers
● required value
$faker->randomDigit;
● non-required values (default 0.5)
$faker->optional($weight = 0.1)->randomDigit; // 10% chance to get null
$faker->optional($weight = 0.9)->randomDigit; // 90% chance to get null
● unique value();
$faker->unique()->randomDigit;
$faker->unique($reset=true)->randomDigit;
fzaninotto/Faker (work with ORM entity)
● Orm adapters : Propel, Doctrine2,
Mandango
$generator = FakerFactory::create();
$populator = new FakerORMPropelPopulator($generator);
$populator->addEntity('Author', 5);
$populator->addEntity('SomeOneBundleEntityBook', 10);
$insertedPKs = $populator->execute();
● Populator by guessing
○ column name
○ column type
○ column length
fzaninotto/Faker (work with ORM entity)
custom populating value
● Null Value
$populator->addEntity('Book', 5, array(
'CreatedAt' => null
));
● anonyomous function
$populator->addEntity('SomeOneBundleEntityPerson', 10,array(
'birthday' => function() use ($gen) {
return $gen->dateTimeBetween($startDate = '-80 years', $endDate
= '-10 years')->format("Y-m-d") ;
}
));
fzaninotto/Faker
● Internal Provider
○ FakerGenerator
○ FakerFactory::create();
● Custom Provider
Other Agenda
● Alice Faker Library
○ https://github.com/nelmio/alice
○ https://github.com/hautelook/AliceBundle
● Image Faker ?
○ http://image-faker.rmhdev.net/
○ http://fakeimg.pl/
Thank For your Listenting !

Doctrine fixtures

  • 1.
  • 2.
    Doctrine Fixtures ● Installation ○composer.json { "require-dev": { "doctrine/doctrine-fixtures-bundle": "dev-master" } } ○ php composer.phar update doctrine/doctrine-fixtures- bundle ○ app/appKernel.php if (in_array($this->getEnvironment(), array('dev', 'test'))) { /* ... other bundles */ $bundles[] = new DoctrineBundleFixturesBundleDoctrineFixturesBundle(); }
  • 3.
    DoctrineFixtures Structure ● Sourcefolder : {BundlesName}DataFixturesORMLoad{EntityName}Data.php ● class naming : Load{EntityName}Data ● extends class: ○ DoctrineCommonDataFixturesAbstractFixture ○ method: Load(DoctrineCommonPersistenceObjectManager $em) ● Implements interface: ○ DoctrineCommonDataFixturesOrderedFixtureInterface ○ method: getOrder() ● Internal method ○ addReference($name,$entityObject); ○ hasReference($name) ○ getReference($name) ● Command: php app/console doctrine:fixtures:load
  • 4.
    fzaninotto/Faker ● Installation php composer.pharrequire “fzaninotto/faker dev-master” ● Base Usage $faker = FakerFactory::create(); $name = $faker->name; $phone = $faker->phoneNumber; $url = $faker->url;
  • 5.
    fzaninotto/Faker - Formats Localation ●Person ● Address ● Company ● PhoneNumber General ● Lorem ● Internet / UserAgent ● File ● Color ● Uuid ● DateTime ● Base(Random, Regex) ● Miscellaneous ● CreditCard
  • 6.
    zaninotto/Faker ● Localization $faker =FakerFactory::create('en_US'); $faker = FakerFactory::create('fr_FR'); ● Seed $faker = FakerFactory::create(); $faker->seed(1234);
  • 7.
    fzaninotto/Faker Unique and Optionalmodifiers ● required value $faker->randomDigit; ● non-required values (default 0.5) $faker->optional($weight = 0.1)->randomDigit; // 10% chance to get null $faker->optional($weight = 0.9)->randomDigit; // 90% chance to get null ● unique value(); $faker->unique()->randomDigit; $faker->unique($reset=true)->randomDigit;
  • 8.
    fzaninotto/Faker (work withORM entity) ● Orm adapters : Propel, Doctrine2, Mandango $generator = FakerFactory::create(); $populator = new FakerORMPropelPopulator($generator); $populator->addEntity('Author', 5); $populator->addEntity('SomeOneBundleEntityBook', 10); $insertedPKs = $populator->execute(); ● Populator by guessing ○ column name ○ column type ○ column length
  • 9.
    fzaninotto/Faker (work withORM entity) custom populating value ● Null Value $populator->addEntity('Book', 5, array( 'CreatedAt' => null )); ● anonyomous function $populator->addEntity('SomeOneBundleEntityPerson', 10,array( 'birthday' => function() use ($gen) { return $gen->dateTimeBetween($startDate = '-80 years', $endDate = '-10 years')->format("Y-m-d") ; } ));
  • 10.
    fzaninotto/Faker ● Internal Provider ○FakerGenerator ○ FakerFactory::create(); ● Custom Provider
  • 11.
    Other Agenda ● AliceFaker Library ○ https://github.com/nelmio/alice ○ https://github.com/hautelook/AliceBundle ● Image Faker ? ○ http://image-faker.rmhdev.net/ ○ http://fakeimg.pl/
  • 12.
    Thank For yourListenting !