orchestral/testbench

Unable to set faker_locale

Closed this issue · 4 comments

I'm raising this as both a question and a feature request, which I'm happy to code if the maintainers think the way forward is correct.

app.faker_locale is set in config/app.php.

However, it does not seem possible to change the faker_locale for testbench in any meaningful way. I suspect that this is because the faker instance being using is booting very early in the process, so setting the config variable via $app->config or config(['app.faker.locale' => 'en_GB'] come too late to actually make a difference.

I've tried a number of different approaches - setting the config variable in a Factory class, creating a new faker Generator in a Factory class and passing it into the Factory, creating a faker singleton in my ServiceProvider, none of which produce the desired result.

I have resorted to the CI/CD job editing the orchestra/testbench-core/laravel/config/app.php file after composer install to set the locale, but obviously this a nasty hack.

An approach which I've identified would be to populate the app.faker_locale variable from an environment variable in app.php, allowing the locale to be changed from phpunit.xml, but I understand that changing Laravel files included in the package might be problematic in terms of maintenance, hence this issue.

So, is there a way to do this that I'm missing, if not, is my proposal acceptable or is there a better way?

Can you confirm that it wouldn't work when configured under getEnvironmentSetUp()

/**
 * Define environment setup.
 *
 * @param  \Illuminate\Foundation\Application  $app
 * @return void
 */
protected function getEnvironmentSetUp($app)
{
    config(['app.faker.locale' => 'en_GB']);
}

Yes, indeed that was my first attempt before I started down the rabbit-hole :-)

In that case feel free to send a PR, I would say getFakerLocale($app) which should return string should be good.

Refer to https://github.com/orchestral/testbench-core/blob/5.x/src/Concerns/CreatesApplication.php#L12-L22

PR should target branch 4.x so we can use this on Laravel 6 tests.

Will do - thanks for the feedback