Skip to content

Commit

Permalink
Use a seed by default to provide constant random results
Browse files Browse the repository at this point in the history
  • Loading branch information
Seldaek committed Nov 22, 2012
1 parent fe78ee9 commit d95f73b
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 8 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,16 @@ $objects = \Nelmio\Alice\Fixtures::load(__DIR__.'/fixtures.php', $objectManager)
Note: You can also pass an array of filenames if you have multiple files with
references spanning more than one.

#### Options

`Fixtures::load` accepts a third `$options` argument that is an array
with the following keys:

- locale: the default locale
- providers: an array of additional Faker providers
- seed: a seed to make sure Faker generates data consistently across runs, set
to null to disable (defaults to 1)

### Detailed Usage ###

If you want a bit more control you can instantiate the various object yourself
Expand Down
5 changes: 4 additions & 1 deletion src/Nelmio/Alice/Fixtures.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,15 @@ class Fixtures
* @param array $options available options:
* - providers: an array of additional faker providers
* - locale: the faker locale
* - seed: a seed to make sure faker generates data consistently across
* runs, set to null to disable
*/
public static function load($files, $container, array $options = array())
{
$defaults = array(
'locale' => 'en_US',
'providers' => array(),
'seed' => 1,
);
$options = array_merge($defaults, $options);

Expand Down Expand Up @@ -67,7 +70,7 @@ private static function getLoader($class, $options)
{
if (!isset(self::$loaders[$class])) {
$fqcn = 'Nelmio\Alice\Loader\\'.$class;
self::$loaders[$class] = new $fqcn($options['locale'], $options['providers']);
self::$loaders[$class] = new $fqcn($options['locale'], $options['providers'], $options['seed']);
}

return self::$loaders[$class];
Expand Down
25 changes: 18 additions & 7 deletions src/Nelmio/Alice/Loader/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
class Base implements LoaderInterface
{
protected $references = array();

/**
* @var ORMInterface
*/
Expand Down Expand Up @@ -72,11 +73,17 @@ class Base implements LoaderInterface
* specified in the expression
* @param array $providers custom faker providers in addition to the default
* ones from faker
* @param int $seed a seed to make sure faker generates data consistently across
* runs, set to null to disable
*/
public function __construct($locale = 'en_US', array $providers = array())
public function __construct($locale = 'en_US', array $providers = array(), $seed = 1)
{
$this->defaultLocale = $locale;
$this->providers = $providers;

if (is_numeric($seed)) {
mt_srand($seed);
}
}

/**
Expand Down Expand Up @@ -150,7 +157,7 @@ public function fake($formatter, $locale = null, $arg = null, $arg2 = null, $arg
if ($this->currentRangeId === null) {
throw new \UnexpectedValueException('Cannot use <current()> out of fixtures ranges.');
}

return $this->currentRangeId;
}

Expand Down Expand Up @@ -279,7 +286,7 @@ private function process($data, array $variables)
if (substr($threshold, -1) === '%') {
$threshold = substr($threshold, 0, -1) / 100;
}
$randVal = rand(0, 100) / 100;
$randVal = mt_rand(0, 100) / 100;
if ($threshold > 0 && $randVal <= $threshold) {
return $trueVal;
} else {
Expand Down Expand Up @@ -357,7 +364,7 @@ private function getRandomReferences($mask, $count = 1)
$availableRefs = array();
foreach ($this->references as $key => $val) {
if (preg_match('{^'.str_replace('*', '.+', $mask).'$}', $key)) {
$availableRefs[$key] = $val;
$availableRefs[] = $val;
}
}

Expand All @@ -366,12 +373,16 @@ private function getRandomReferences($mask, $count = 1)
}

if (null === $count) {
return $availableRefs[array_rand($availableRefs)];
return $availableRefs[mt_rand(0, count($availableRefs) - 1)];
}

shuffle($availableRefs);
$res = array();
while ($count-- && $availableRefs) {
$ref = array_splice($availableRefs, mt_rand(0, count($availableRefs) - 1), 1);
$res[] = current($ref);
}

return array_slice($availableRefs, 0, min($count, count($availableRefs)));
return $res;
}

private function findAdderMethod($obj, $key)
Expand Down

0 comments on commit d95f73b

Please sign in to comment.