Skip to content

Commit

Permalink
make BaseProvider methods public, and therefore accessible as formatters
Browse files Browse the repository at this point in the history
  • Loading branch information
fzaninotto committed Oct 23, 2011
1 parent c584886 commit 1bf9752
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 69 deletions.
12 changes: 10 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,16 @@ Each of the generator properties (like `name`, `address`, and `lorem`) are calle
md5 // 'de99a620c50f2990e87144735cd357e7'
sha1 // 'f08e7f04ca1a413807ebc47551a40a20a0b4de5c'
sha256 // '0061e4c60dac5c1d82db0135a42e00c89ae3a333e7c26485321f24348c7e98a5'
number($nbDigits = 3) // 23
choose(array('a', 'b', 'c')) // 'b'

### `Faker\Provider\Base`

randomDigit // 7
randomNumber($nbDigits = NULL) // 79907610
randomLetter // 'b'
randomElement($array = array ('a','b','c')) // 'b'
numerify($string = '###') // '609'
lexify($string = '????') // 'wgts'
bothify($string = '## ??') // '42 jz'

## Localization

Expand Down
9 changes: 7 additions & 2 deletions src/Faker/Documentor.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,18 @@ public function __construct($generator)
public function getFormatters()
{
$formatters = array();
foreach ($this->generator->getProviders() as $provider) {
$providers = $this->generator->getProviders();
$providers[]= new \Faker\Provider\Base($this->generator);
foreach ($providers as $provider) {
$providerClass = get_class($provider);
$formatters[$providerClass] = array();
$refl = new \ReflectionObject($provider);
foreach ($refl->getMethods(\ReflectionMethod::IS_PUBLIC) as $reflmethod) {
if ($reflmethod->getDeclaringClass()->getName() == 'Faker\Provider\Base' && $providerClass != 'Faker\Provider\Base') {
continue;
}
$methodName = $reflmethod->name;
if ($methodName == '__construct') {
if ($reflmethod->isConstructor()) {
continue;
}
$parameters = array();
Expand Down
2 changes: 1 addition & 1 deletion src/Faker/ORM/Propel/ColumnTypeGuesser.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function guessFormat($column)
case PropelColumnTypes::NUMERIC:
case PropelColumnTypes::DECIMAL:
$size = $column->getSize();
return function() use ($generator, $size) { return $generator->randomNumber($size * 100) / 100; };
return function() use ($generator, $size) { return $generator->randomNumber($size + 2) / 100; };
case PropelColumnTypes::TINYINT:
return function() { return mt_rand(0,255); };
case PropelColumnTypes::SMALLINT:
Expand Down
20 changes: 12 additions & 8 deletions src/Faker/Provider/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function __construct($generator)
*
* @return integer
*/
protected static function randomDigit()
public static function randomDigit()
{
return mt_rand(0, 9);
}
Expand All @@ -25,20 +25,24 @@ protected static function randomDigit()
* Returns a random number with 0 to $nbDigits digits
*
* @param integer $nbDigits
* @example 79907610
*
* @return integer
*/
protected static function randomNumber($nbDigits = 3)
public static function randomNumber($nbDigits = null)
{
return mt_rand(0, $nbDigits * 10);
if (null === $nbDigits) {
$nbDigits = static::randomDigit();
}
return mt_rand(0, pow(10, $nbDigits) - 1);
}

/**
* Returns a random letter from a to z
*
* @return string
*/
protected static function randomLetter()
public static function randomLetter()
{
return chr(mt_rand(97, 122));
}
Expand All @@ -49,7 +53,7 @@ protected static function randomLetter()
* @param array $array
* @return mixed
*/
protected static function randomElement($array)
public static function randomElement($array = array('a', 'b', 'c'))
{
return $array[mt_rand(0, count($array) - 1)];
}
Expand All @@ -60,7 +64,7 @@ protected static function randomElement($array)
* @param string $string String that needs to bet parsed
* @return string
*/
protected static function numerify($string)
public static function numerify($string = '###')
{
return preg_replace_callback('/\#/', 'static::randomDigit', $string);
}
Expand All @@ -71,7 +75,7 @@ protected static function numerify($string)
* @param string $string String that needs to bet parsed
* @return string
*/
protected static function lexify($string)
public static function lexify($string = '????')
{
return preg_replace_callback('/\?/', 'static::randomLetter', $string);
}
Expand All @@ -82,7 +86,7 @@ protected static function lexify($string)
* @param string $string String that needs to bet parsed
* @return string
*/
protected static function bothify($string)
public static function bothify($string = '## ??')
{
return static::lexify(static::numerify($string));
}
Expand Down
10 changes: 0 additions & 10 deletions src/Faker/Provider/Miscellaneous.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,5 @@ public static function sha256()
{
return hash('sha256', mt_rand());
}

public static function number($nbDigits = 3)
{
return static::randomNumber($nbDigits);
}

public static function choose($list)
{
return static::randomElement($list);
}

}
69 changes: 23 additions & 46 deletions test/Faker/Provider/BaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,94 +10,71 @@ class BaseTest extends \PHPUnit_Framework_TestCase
{
public function testRandomDigitReturnsInteger()
{
$this->assertTrue(is_integer(TestProvider::randomDigit()));
$this->assertTrue(is_integer(BaseProvider::randomDigit()));
}

public function testRandomDigitReturnsDigit()
{
$this->assertTrue(TestProvider::randomDigit() >= 0);
$this->assertTrue(TestProvider::randomDigit() < 10);
$this->assertTrue(BaseProvider::randomDigit() >= 0);
$this->assertTrue(BaseProvider::randomDigit() < 10);
}

public function testRandomNumberReturnsInteger()
{
$this->assertTrue(is_integer(BaseProvider::randomNumber()));
}

public function testRandomNumberReturnsDigit()
{
$this->assertTrue(BaseProvider::randomNumber(3) >= 0);
$this->assertTrue(BaseProvider::randomNumber(3) < 1000);
}

public function testRandomLetterReturnsString()
{
$this->assertTrue(is_string(TestProvider::randomLetter()));
$this->assertTrue(is_string(BaseProvider::randomLetter()));
}

public function testRandomLetterReturnsSingleLetter()
{
$this->assertEquals(1, strlen(TestProvider::randomLetter()));
$this->assertEquals(1, strlen(BaseProvider::randomLetter()));
}

public function testRandomLetterReturnsLowercaseLetter()
{
$lowercaseLetters = 'abcdefghijklmnopqrstuvwxyz';
$this->assertTrue(strpos($lowercaseLetters, TestProvider::randomLetter()) !== false);
$this->assertTrue(strpos($lowercaseLetters, BaseProvider::randomLetter()) !== false);
}

public function testRandomElementReturnsElementFromArray()
{
$elements = array('23', 'e', 32, '#');
$this->assertContains(TestProvider::randomElement($elements), $elements);
$this->assertContains(BaseProvider::randomElement($elements), $elements);
}

public function testNumerifyReturnsSameStringWhenItContainsNoHashSign()
{
$this->assertEquals('fooBar?', TestProvider::numerify('fooBar?'));
$this->assertEquals('fooBar?', BaseProvider::numerify('fooBar?'));
}

public function testNumerifyReturnsStringWithHashSignsReplacedByDigits()
{
$this->assertRegExp('/foo\dBa\dr/', TestProvider::numerify('foo#Ba#r'));
$this->assertRegExp('/foo\dBa\dr/', BaseProvider::numerify('foo#Ba#r'));
}

public function testLexifyReturnsSameStringWhenItContainsNoQuestionMark()
{
$this->assertEquals('fooBar#', TestProvider::lexify('fooBar#'));
$this->assertEquals('fooBar#', BaseProvider::lexify('fooBar#'));
}

public function testNumerifyReturnsStringWithQuestionMarksReplacedByLetters()
{
$this->assertRegExp('/foo[a-z]Ba[a-z]r/', TestProvider::lexify('foo?Ba?r'));
$this->assertRegExp('/foo[a-z]Ba[a-z]r/', BaseProvider::lexify('foo?Ba?r'));
}

public function testBothifyCombinesNumerifyAndLexify()
{
$this->assertRegExp('/foo[a-z]Ba\dr/', TestProvider::bothify('foo?Ba#r'));
}

}

class TestProvider extends BaseProvider
{
public static function randomDigit()
{
return parent::randomDigit();
}

public static function randomLetter()
{
return parent::randomLetter();
}

public static function randomElement($array)
{
return parent::randomElement($array);
}

public static function numerify($string)
{
return parent::numerify($string);
$this->assertRegExp('/foo[a-z]Ba\dr/', BaseProvider::bothify('foo?Ba#r'));
}

public static function lexify($string)
{
return parent::lexify($string);
}

public static function bothify($string)
{
return parent::bothify($string);
}

}

0 comments on commit 1bf9752

Please sign in to comment.