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

"<current()>" causes exception when Faker\Generator instance is passed to NativeLoader constructor #971

Open
martingold opened this issue Jan 15, 2019 · 6 comments
Labels
Milestone

Comments

@martingold
Copy link

martingold commented Jan 15, 2019

Issue

When I pass my own Faker\Generator then InvalidArgumentException: Unknown formatter "current" is thrown. My guess is that Alice does not register its current provider to the passed Generator instance.

Why

My motivation to do this is to use $locale parameter in Faker\Factory::create() to set locale as it seems to be the most intuitive way to set locale. It is a handy way instead of extending NativeLoader. But it does not work (locale is still default) but that is subject for another issue.

Reproducing issue

I have created minimal repository and you can try it yourself. The issue is demonstrated in these two classes:
MartinGold\AliceIssueRepo\Factory\DefaultFakerAliceFactory::create() and
MartinGold\AliceIssueRepo\Factory\InjectedFakerAliceFactory::create()

git clone https://github.com/martingold/alice-issue-minimal-repo.git
cd alice-issue-minimal-repo
composer install
php run.php # Everything works
php run.php --injected # Exception is thrown

My main question is what is the purpose of the NativeLoader $fakerGenerator argument purpose if it breaks the Alice itself and whether it is worth investigating this further and fixing it?

@theofidry
Copy link
Member

Thanks for the report! I'll look it up ASAP although I must say that I need to take care of a couple of things in AliceDataFixtures and HautelookAliceBundle first when I manage to free some time (this month is quite busy).

@theofidry theofidry added the Bug label Jan 16, 2019
@theofidry theofidry added this to the 3.x milestone Jan 16, 2019
@martingold
Copy link
Author

I know you have a lot of work and you do not have to invest your free time/money into this issue :). What I wanted wanted to know is why there is NativeLoader $fakerGenerator argument in constructor and what is its purpose and some general direction how could i help. Thanks

@theofidry
Copy link
Member

What I wanted wanted to know is why there is NativeLoader $fakerGenerator argument in constructor and what is its purpose and some general direction how could i help

The NativeLoader class is when you want to use Alice without the framework bridge (e.g. the Alice Symfony bundle NelmioAliceBundle) or when no framework bridge is available. I.e. it's just a helper to make the library usable without any framework.

There is a $fakerGenerator injectable in case you want to have full control on it but I would definitely recommend to extend the NativeLoader and override the createFakerFactory() method instead. But maybe injecting the faker generator is too error prone and should be deprecated...

@martingold
Copy link
Author

My initial guess was right. The AliceProvider is not added to Faker\Generator when passing custom instance.

Not sure if the $this->fakerGenerator->addProvider(new AliceProvider()); should be implemented somewhere in NativeLoader constructor or just to mention in docs the need to add AliceProvider to passed Faker\Generator manually if you want to use <current()> provider.

Either way, I am ready PR to send with required changes. Thanks!

@theofidry
Copy link
Member

The AliceProvider is not added to Faker\Generator when passing custom instance.

Then let's deprecate injecting a custom faker provided since it's more error prone than anything.

@ryrobbo
Copy link

ryrobbo commented Nov 15, 2021

@martingold I was experiencing this exact issue myself when setting a locale and using <current()>. Your suggestion of adding

$generator->addProvider(new AliceProvider());

worked a treat. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants