Search code examples
phpsymfonydoctrine-ormnelmio-alicealice-fixtures

Using custom faker data providers without standard doctrine fixtures in Nelmio Alice


I am setting up NelmioAlice and Faker in a Symfony2 project through AlixeFixturesBundle. I need a composed fixture like for example:

representative{1..100}:
    veeva_rep_id (unique): qlv_005800000067SwzAAE

which is a qlv_ prefix followed by a random 18 characters string. The best way I found to get this done (if anyone knows another one or a better way to get this done let me know) was using a custom faker and I wrote this piece of code:

<?php
/**
 * FakerProvider: VeevaProvider.
 */

namespace PDI\PDOneBundle\DataFixtures;

use ReverseRegex\Lexer;
use ReverseRegex\Random\SimpleRandom;
use ReverseRegex\Parser;
use ReverseRegex\Generator\Scope;

class VeevaProvider extends \Faker\Provider\Base
{
    public function veevaRepId()
    {
        $lexer = new  Lexer('[a-zA-Z0-9]{18}');
        $gen = new SimpleRandom(10007);
        $result = '';

        $parser = new Parser($lexer, new Scope(), new Scope());
        $parser->parse()->getResult()->generate($result, $gen);

        return 'qlv_' . $result;
    }
}

As explain here in Faker docs. Now, here at NelmioAlice the author explain how to add Custom Faker Data Providers but it uses Doctrine Fixtures which I don't so, having this, how do I load and use the provider I wrote on the fixtures? Any advice around this?


Solution

  • It should be as simple as passing an instance of the provider into the Loader when you construct it:

    $loader = new Loader('en_US', [new VeeveProvider]);