Faker icon indicating copy to clipboard operation
Faker copied to clipboard

Faker generates same word 2 times in a row

Open loginwashere opened this issue 11 years ago • 6 comments

We use faker in our project and generally are happy with it.

But from time to time one of our tests fails because faker generates 2 same words in one test. I've tried to investigate this issue and added this test.

    public function testFakerGenerateUniqueWordsTwoTimesInARow()
    {
        $faker = \Faker\Factory::create();
        $faker->seed(5);

        for ($i = 0; $i < 1000; $i++) {
            $firstWord = $faker->word;
            $secondWord = $faker->word;

            $this->assertNotEquals($firstWord, $secondWord, json_encode([
                'firstWord' => $firstWord,
                'secondWord' => $secondWord,
                'index' => $i
            ]));
        }
    }

When I run it, it fails every time with this message

$ vendor/bin/phpunit --filter testFakerGenerateUniqueWords
PHPUnit 4.7.6 by Sebastian Bergmann and contributors.

F

Time: 4.56 seconds, Memory: 33.00Mb

There was 1 failure:

1) App\Http\Controllers\RecipesControllerTest::testFakerGenerateUniqueWords
{"firstWord":"est","secondWord":"est","index":290}
Failed asserting that 'est' is not equal to <string:est>.

If I comment out line with setting seed test continue failing but at random index.

I'm not sure if this is intended behavior and if not it will be nice to fix it.

Some info about environment

$ composer info -i | grep faker
fzaninotto/faker                      v1.5.0             Faker is a PHP library that generates fake data for you.

$ php -v
PHP 5.5.26-1+deb.sury.org~trusty+1 (cli) (built: Jun 15 2015 10:13:03) 
Copyright (c) 1997-2015 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2015 Zend Technologies
    with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2015, by Zend Technologies
    with Xdebug v2.3.2, Copyright (c) 2002-2015, by Derick Rethans

$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 14.04.2 LTS
Release:    14.04
Codename:   trusty

loginwashere avatar Jul 30 '15 13:07 loginwashere

Sounds like the intended behaviour to me. You need to say you want "unique" if you want them unique...

GrahamCampbell avatar Jul 30 '15 13:07 GrahamCampbell

@GrahamCampbell you mean something like this?

$faker->unique()->word

loginwashere avatar Jul 30 '15 13:07 loginwashere

This way if I run this code

    public function testFakerGenerateUniqueWords()
    {
        $faker = \Faker\Factory::create();
        $faker->seed(5);

        for ($i=0; $i < 1000; $i++) {
            try {
                $firstWord = $faker->unique()->word;
                $secondWord = $faker->unique()->word;
            } catch (\OverflowException $e) {
                dd([
                    'exceptionMessage' => $e->getMessage(),
                    'i' => $i
                ]);
            }

            $this->assertNotEquals($firstWord, $secondWord, json_encode([
                'firstWord' => $firstWord,
                'secondWord' => $secondWord,
                'index' => $i
            ]));
        }
    }

it will return this every time

$ vendor/bin/phpunit --filter testFakerGenerateUniqueWords
PHPUnit 4.7.6 by Sebastian Bergmann and contributors.

array:2 [
  "exceptionMessage" => "Maximum retries of 10000 reached without finding a unique value"
  "i" => 91
]

loginwashere avatar Jul 30 '15 13:07 loginwashere

Aha I think I've got it. This way test passes.

public function testFakerGenerateUniqueWords()
    {
        $faker = \Faker\Factory::create();
        $faker->seed(5);

        for ($i=0; $i < 1000; $i++) {
            try {
                $firstWord = $faker->unique()->word;
                $secondWord = $faker->unique()->word;
            } catch (\OverflowException $e) {
                $firstWord = $faker->unique(true)->word;
                $secondWord = $faker->unique()->word;
            }

            $this->assertNotEquals($firstWord, $secondWord, json_encode([
                'firstWord' => $firstWord,
                'secondWord' => $secondWord,
                'index' => $i
            ]));
        }
    }

Maybe there is a better way?

loginwashere avatar Jul 30 '15 13:07 loginwashere

Because the generators are (pseudo)random, it is possible for them to produce the same value twice, even twice in a row. Exactly how likely that is depends on the generator and how many possible values there are, but it's generally unlikely.

If the generator is seeded with the same number, however, it will always produce the same sequence. If the first two words of that sequence happen to be the same, your test will fail every single time.

If you want to make sure the generator never repeats values, unique() makes sure that all produced values are unique. If there are fewer than 1000 words for it to choose from, the generator in this test simply runs out and it throws an error because it can't produce any more unique words.

Your tests seem to be working as expected, so the question is, what exactly are you trying to do?

okj579 avatar Jul 30 '15 18:07 okj579

I'm simple man, I'm just trying to make tests in our app work without failing randomly :)

I've looked at word and unique implementation and now understand that there are only 249 unique words and unique will only return unique values or throw exception.

I've added tests above while I was trying to understand the issue. And these tests are obviously synthetic and do not exactly represent test cases we are working on.

At the moment I think adding $faker->unique()->word where it is needed will solve the issue for us. Thanks for help.

loginwashere avatar Jul 30 '15 19:07 loginwashere