Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using custom faker data providers without standard doctrine fixtures #44

Closed
reypm opened this issue May 10, 2015 · 2 comments
Closed

Using custom faker data providers without standard doctrine fixtures #44

reypm opened this issue May 10, 2015 · 2 comments

Comments

@reypm
Copy link

reypm commented May 10, 2015

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? How the custom provider should be added and used on this bundle?

@h4cc
Copy link
Owner

h4cc commented May 15, 2015

Using custom FakerProvider is the way to generate domain-specific data like you have there.
But i cant see, why the Provider has to extend \Faker\Provider\Base, because it does not use anything of that class.

There is a quite straigtforward example in AliceDemo.
Have a look for the provider itself here: https://github.com/h4cc/AliceDemo/blob/master/src/h4cc/AliceDemoBundle/DataFixtures/Alice/Provider/CategoryProvider.php
And the registration of such a provider using dependency injection here: https://github.com/h4cc/AliceDemo/blob/master/src/h4cc/AliceDemoBundle/Resources/config/services.yml

@h4cc
Copy link
Owner

h4cc commented May 29, 2015

If the given information does not solve your problem, please reopen.

@h4cc h4cc closed this as completed May 29, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants