Skip to content
This repository has been archived by the owner on Dec 11, 2020. It is now read-only.

Allow to prevent seed break on destruct #2014

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,9 @@ echo $faker->name; // 'Jess Mraz I';
> // good
> $faker->realText($faker->numberBetween(10,20));
> ```
>
> **Tip**: When the faker object is destroyed, the seed is restored to generate unpredictable values (default behavior for mt_rand that is used internally). But you can't guess when the php garbage collector is going to collect the faker object. It can occur during a values generation sequence from another instance of faker (like in a test suite). In this case it will break your predictable values suite. To prevent this kind of problem, when you've finished to generate values, you should use `restoreSeed` on your faker instance to control the seed restore instant.
>



Expand Down
13 changes: 12 additions & 1 deletion src/Faker/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ class Generator
{
protected $providers = array();
protected $formatters = array();
protected $seedRestored = false;

public function addProvider($provider)
{
Expand All @@ -223,6 +224,16 @@ public function seed($seed = null)
}
}

public function restoreSeed()
{
if ($this->seedRestored) {
return;
}

$this->seed();
$this->seedRestored = true;
}

public function format($formatter, $arguments = array())
{
return call_user_func_array($this->getFormatter($formatter), $arguments);
Expand Down Expand Up @@ -287,6 +298,6 @@ public function __call($method, $attributes)

public function __destruct()
{
$this->seed();
$this->restoreSeed();
}
}
26 changes: 26 additions & 0 deletions test/Faker/GeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,32 @@ public function testSeed()
$generator->seed('10');
$this->assertTrue(true, 'seeding with a non int value doesn\'t throw an exception');
}

public function testRestoreSeedPreventSeedBreak()
{
$generator1 = new Generator;
$generator1->seed(0);

$generated1 = array();
for ($i=0; $i<5; $i++) {
$generated1[] = mt_rand(1, 10);
}
$generator1->restoreSeed();
$this->assertEquals(array(5, 6, 8, 2, 7), $generated1);

$generator2 = new Generator;
$generator2->seed(0);

$generated2 = array();
for ($i=0; $i<5; $i++) {
if ($i == 2) {
$generator1->__destruct();
}
$generated2[] = mt_rand(1, 10);
}
$generator2->restoreSeed();
$this->assertEquals(array(5, 6, 8, 2, 7), $generated2);
}
}

final class FooProvider
Expand Down