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:
commit
9ebd952cb5
@ -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);
|
||||
|
@ -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()
|
||||
|
Loading…
x
Reference in New Issue
Block a user