Skip to content

Commit

Permalink
add seed feature
Browse files Browse the repository at this point in the history
  • Loading branch information
niklongstone committed Jun 18, 2015
1 parent f3e8d2e commit b7160d8
Show file tree
Hide file tree
Showing 11 changed files with 57 additions and 12 deletions.
4 changes: 4 additions & 0 deletions CHANGES.md
@@ -1,3 +1,7 @@
0.8.0 / 2015-06-18
==================
* Add seed feature, to return same value in different calls

0.7.2 / 2015-06-16
==================
* Fix regex reverter version dependency
Expand Down
6 changes: 3 additions & 3 deletions README.md
Expand Up @@ -18,9 +18,9 @@ Fakerino is a fake data generator framework fully extensible.
* Fake data in __different languages__.
* Fake __regular expression__ data (e.g. url => '/www\.\w+\.com/').
* Fake data multiple times.
* Fake a __database__ table row/s in one row of code.
* Fake a __database__ table row/s with one line of code.
* Fake a __Twig__ string or file automatically (e.g. Hello Mr {{ surname }})
* Fake a PHP __object/entity__ in one row of code (fills public properties and setters with fake data).
* Fake a PHP __object/entity__ in one line of code (fills public properties and setters with fake data).
* Support __JSON__, __array__ and string output.
* Support array, __Yaml__, __XML__, __PHP__, __Txt__ and __Ini__ configurations.
* Fake from __command line__ interface.
Expand All @@ -33,7 +33,7 @@ please read the __[Fakerino wiki](https://github.com/niklongstone/Fakerino/wiki)
Use [Composer](https://getcomposer.org/download/) to manage the dependencies of your project.
####In your project folder run:
```sh
composer require fakerino/fakerino='~0.7'
composer require fakerino/fakerino='~0.8'
vendor/fakerino/fakerino/build/ods
```
#### Like a stand-alone project run:
Expand Down
14 changes: 14 additions & 0 deletions src/Fakerino/Core/FakeDataFactory.php
Expand Up @@ -163,6 +163,20 @@ public function num($num = 1)
return $this;
}

/**
* @param int|null $seed
*
* @return FakeDataFactory $this
*/
public function seed($seed = null)
{
if ($seed !== null) {
mt_srand($seed);
}

return $this;
}

/**
* @return array
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Fakerino/Core/Filler/DbFiller.php
Expand Up @@ -85,7 +85,7 @@ private function fakeColumn($fakeName, $fakeType)
{
switch ($fakeType) {
case self::NUMERIC:
$result = $this->faker->fake(array('Integer' => array('length' => rand(6, 8))))->num(1);
$result = $this->faker->fake(array('Integer' => array('length' => mt_rand(6, 8))))->num(1);
break;
case self::DATE:
$result = $this->faker->fake(array('date'))->num(1);
Expand Down
2 changes: 1 addition & 1 deletion src/Fakerino/FakeData/Custom/Integer.php
Expand Up @@ -31,7 +31,7 @@ class Integer extends AbstractFakeData
public function getDefaultOptions()
{
return array(
'length' => rand(1, 5),
'length' => mt_rand(1, 5),
'negative' => false,
'type' => self::DECIMAL
);
Expand Down
2 changes: 1 addition & 1 deletion src/Fakerino/FakeData/Custom/Text.php
Expand Up @@ -26,7 +26,7 @@ class Text extends AbstractFakeData
public function getDefaultOptions()
{
return array(
'length' => rand(3, 20),
'length' => mt_rand(3, 20),
'addChar' => null
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Fakerino/FakeData/Generator/DateGenerator.php
Expand Up @@ -29,7 +29,7 @@ public function generate()
$startDate = $this->getOption('startDate');
$endDate = $this->getOption('endDate');

$time = rand(strtotime($startDate), strtotime($endDate));
$time = mt_rand(strtotime($startDate), strtotime($endDate));

return date($format, $time);
}
Expand Down
3 changes: 2 additions & 1 deletion src/Fakerino/FakeData/Generator/FileFakeGenerator.php
Expand Up @@ -34,7 +34,8 @@ public function generate()
. DIRECTORY_SEPARATOR;
if ($fakeFile = FakeTxtFile::getSource($fileName, $path)) {
$lines = file($fakeFile);
$element = $lines[array_rand($lines)];
$index = mt_rand(0, count($lines) - 1);
$element = $lines[$index];

return preg_replace("/\r|\n/", "", $element);
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/Fakerino/FakeData/Generator/IntegerGenerator.php
Expand Up @@ -62,7 +62,7 @@ private function createDecimal($length)
{
$min = pow(10, $length - 1);
$max = pow(10, $length) -1;
$fakeInteger = rand($min, $max);
$fakeInteger = mt_rand($min, $max);

return $fakeInteger;
}
Expand Down
16 changes: 14 additions & 2 deletions src/Fakerino/FakeData/Generator/TextGenerator.php
Expand Up @@ -38,14 +38,26 @@ public function generate()
if ($addChars !== null) {
$chars .= $addChars;
}
$stringShuffle = str_shuffle(str_repeat($chars, self::SHUFFLE));
$stringShuffle = $this->stringShuffle($chars);
$length = $this->getOption('length');
if ($length !== null) {
$randomString = substr($stringShuffle, 0, $length);
} else {
$randomString = substr($stringShuffle, 0, rand(self::MINLENGTH, self::MAXLENGTH));
$randomString = substr($stringShuffle, 0, mt_rand(self::MINLENGTH, self::MAXLENGTH));
}

return $randomString;
}

private function stringShuffle($chars)
{
$shuffledString = '';
$charsLength = strlen($chars);
for ($i = 0; $i < $charsLength; $i++) {
$rand = mt_rand(0, $charsLength - 1);
$shuffledString .= substr($chars, $rand, 1);
}

return $shuffledString;
}
}
16 changes: 15 additions & 1 deletion tests/Fakerino/Test/FakerinoTest.php
Expand Up @@ -50,14 +50,28 @@ public function testFakeData($conf, $element, $expected)
$this->assertRegExp($expected, $result, sprintf("The result '%s' of '%s' doesn't match the expected regex '%s'", $result, serialize($element), $expected));
}

/**
* @dataProvider provider
*/
public function testFakeWithSeed($conf, $element)
{
$fakerino = Fakerino::create($conf);
mt_srand(2);
$result1 = $fakerino->fake($element)->__toString();
mt_srand(2);
$result2 = $fakerino->fake($element)->__toString();

$this->assertEquals($result1, $result2, sprintf("The first result '%s' and the second '%s'", $result1, $result2));
}

public function provider()
{
return array(
array(null, array('text' => array('length' => 10)), '/\w{10}/'),
array(null, array('text' => array('length' => 10)), '/\w{10}/'),
array(null, 'surname', '/[^0-9.]/'),
array(null, 'integer', '/[0-9]/'),
array(null, 'lorem', '/\w/'),
array(null, 'date', '/[\d-]/'),
array(
array(
'fake' => array('fakeTest' =>
Expand Down

0 comments on commit b7160d8

Please sign in to comment.