1
0
mirror of https://github.com/fzaninotto/Faker.git synced 2025-01-17 22:28:55 +01:00

Merge pull request #350 from fzaninotto/random_number_between

Throw Exception when randomNumber is used instead of numberBetween
This commit is contained in:
Francois Zaninotto 2014-06-02 21:33:13 +02:00
commit 9ebd952cb5
2 changed files with 15 additions and 5 deletions

View File

@ -49,7 +49,7 @@ class Base
/**
* Returns a random number with 0 to $nbDigits digits
*
* @param integer $nbDigits
* @param integer $nbDigits Defaults to a random number between 1 and 9
* @param boolean $strict Whether the returned number should have exactly $nbDigits
* @example 79907610
*
@ -57,8 +57,11 @@ class Base
*/
public static function randomNumber($nbDigits = null, $strict = false)
{
if (!is_bool($strict)) {
throw new \InvalidArgumentException('randomNumber() generates numbers of fixed width. To generate numbers between two boundaries, use numberBetween() instead.');
}
if (null === $nbDigits) {
$nbDigits = static::randomDigit();
$nbDigits = static::randomDigitNotNull();
}
if ($strict) {
return mt_rand(pow(10, $nbDigits - 1), pow(10, $nbDigits) - 1);

View File

@ -23,9 +23,18 @@ class BaseTest extends \PHPUnit_Framework_TestCase
$this->assertTrue(BaseProvider::randomDigitNotNull() < 10);
}
/**
* @expectedException \InvalidArgumentException
*/
public function testRandomNumberThrowsExceptionWhenCalledWithAMax()
{
BaseProvider::randomNumber(100, 200);
}
public function testRandomNumberReturnsInteger()
{
$this->assertTrue(is_integer(BaseProvider::randomNumber()));
$this->assertTrue(is_integer(BaseProvider::randomNumber(5, false)));
}
public function testRandomNumberReturnsDigit()
@ -36,9 +45,7 @@ class BaseTest extends \PHPUnit_Framework_TestCase
public function testRandomNumberAcceptsStrictParamToEnforceNumberSize()
{
for ($i=0; $i < 10; $i++) {
$this->assertEquals(10, strlen((string) BaseProvider::randomNumber(10, true)));
}
$this->assertEquals(10, strlen((string) BaseProvider::randomNumber(10, true)));
}
public function testNumberBetween()