bwrigley's avatar

Faker and locale

Hello everyone,

I'm pretty sure I'm being dumb here but I'm having issues with Faker locale.

In my config/app.php:

    'locale' => 'en',
    'faker_locale' => 'en_GB',

this works fine in a factory:

use Faker\Generator as Faker;

$factory->define(\App\Address::class, function (Faker $faker) {
    return [
        'country' => $faker->randomElement(['England','Wales','Scotland','Northern Ireland','Southern Ireland','United Kingdom']),
        'country_code' => 'GB',
        'town' => $faker->boolean(50) ? $faker->city() : null,
        'county' => $faker->boolean(50) ? $faker->county() : null,
        'address1' => $faker->boolean(50) ? $faker->buildingNumber() . ' ' . $faker->streetName() : null,
        'address2' => $faker->boolean(80) ? $faker->streetName() : null,
        'postcode' => $faker->boolean(50) ? $faker->postcode() : substr($faker->postcode(),0,3),
        'is_verified' => $faker->boolean(50),
        'is_live' => $faker->boolean(50),
        'is_visible' => $faker->boolean(50),
        'is_primary' => $faker->boolean(50),
        'raw' => str_replace("\n",'<br>',$faker->address()),
        'latitude' => $faker->latitude(49.82,59.47),
        'longitude' => $faker->longitude(-10.85,2.02),
        'profile_id' => 1,
    ];
});

but when I try in a test:

use Faker\Factory as Faker;

       //

        $faker = Faker::create();

        $addressDetails = [
            'country' => $faker->randomElement(['England', 'Wales', 'Scotland', 'Northern Ireland', 'Southern Ireland', 'United Kingdom']),
            'country_code' => 'GB',
            'town' => $faker->city(),
            'county' => $faker->county(),
            'address1' => $faker->buildingNumber() . ' ' . $faker->streetName(),
            'address2' => $faker->streetName(),
            'postcode' => $faker->postcode(),
            'is_verified' => $faker->boolean(50),
            'is_live' => $faker->boolean(50),
            'is_visible' => $faker->boolean(50),
            'is_primary' => true,
            'latitude' => $faker->latitude(49.82,59.47),
            'longitude' => $faker->longitude(-10.85,2.02),
        ];

then I get the error:

InvalidArgumentException: Unknown formatter "county"

and with

 $faker = Faker::create('en_GB');

it works again fine!

I'm guessing it's to do with how I'm creating my instance of Faker that's causing the issue as it's not coming from the service container.

Thanks for any help.

0 likes
6 replies
bwrigley's avatar

@sti3bas thank you so much for replying.

So I have tried this but I still get

InvalidArgumentException: Unknown formatter "county"

which is en_GB locale specific. So it seems it's still not picking up the locale for some reason.

Sti3bas's avatar
Sti3bas
Best Answer
Level 53

@bwrigley seems like Laravel 5.8 doesn't pick up the faker_locale config value.

https://github.com/laravel/framework/blob/5.8/src/Illuminate/Foundation/Testing/WithFaker.php#L45

You can create your own WithFaker trait and use that instead of built-in one:

tests/WithFaker.php:

namespace Tests;

use Illuminate\Foundation\Testing\WithFaker as BaseWithFaker;

trait WithFaker
{
    use BaseWithFaker;

    protected function setUpFaker()
    {
        $this->faker = $this->makeFaker(config('app.faker_locale'));
    }
}

Test:

use Tests\WithFaker;

//...

class ExampleTest extends TestCase
{
   use WithFaker;

   /** @test */
   public function example()
   {
      // you will be able to access Faker with:
      $this->faker();
   }
}

You will be able to remove it after upgrading to Laravel 6.

bwrigley's avatar

@sti3bas

Thank you, good idea!

I just assumed it would pick it up automatically when i instantiated a new Faker. Thanks for your help!

Please or to participate in this conversation.