mirror of
https://github.com/fzaninotto/Faker.git
synced 2025-03-23 16:59:53 +01:00
Merge pull request #64 from Seldaek/features
Adds/tweaks a few features
This commit is contained in:
commit
db6dcb801f
@ -34,19 +34,41 @@ class Base
|
||||
/**
|
||||
* Returns a random number with 0 to $nbDigits digits
|
||||
*
|
||||
* If $upTo is passed, it returns a number between $nbDigits (read as from) and $upTo
|
||||
*
|
||||
* @param integer $nbDigits
|
||||
* @param integer $upTo
|
||||
* @example 79907610
|
||||
*
|
||||
* @return integer
|
||||
*/
|
||||
public static function randomNumber($nbDigits = null)
|
||||
public static function randomNumber($nbDigits = null, $upTo = null)
|
||||
{
|
||||
if (null === $nbDigits) {
|
||||
$nbDigits = static::randomDigit();
|
||||
}
|
||||
|
||||
if (null !== $upTo) {
|
||||
return static::numberBetween($nbDigits, $upTo);
|
||||
}
|
||||
|
||||
return mt_rand(0, pow(10, $nbDigits) - 1);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns a random number between $from and $to
|
||||
*
|
||||
* @param integer $from
|
||||
* @param integer $too
|
||||
* @example 79907610
|
||||
*
|
||||
* @return integer
|
||||
*/
|
||||
public static function numberBetween($from = null, $to = null)
|
||||
{
|
||||
return mt_rand($from ?: 0, $to ?: PHP_INT_MAX);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a random letter from a to z
|
||||
*
|
||||
|
@ -79,8 +79,8 @@ class DateTime extends \Faker\Provider\Base
|
||||
*/
|
||||
public static function dateTimeBetween($startDate = "-30 years", $endDate = "now")
|
||||
{
|
||||
$startTimestamp = strtotime($startDate);
|
||||
$endTimestamp = strtotime($endDate);
|
||||
$startTimestamp = $startDate instanceof \DateTime ? $startDate->getTimestamp() : strtotime($startDate);
|
||||
$endTimestamp = $endDate instanceof \DateTime ? $endDate->getTimestamp() : strtotime($endDate);
|
||||
$timestamp = mt_rand($startTimestamp, $endTimestamp);
|
||||
|
||||
return new \DateTime('@' . $timestamp);
|
||||
|
@ -57,15 +57,16 @@ class Lorem extends \Faker\Provider\Base
|
||||
*
|
||||
* @example array('Lorem', 'ipsum', 'dolor')
|
||||
* @param integer $nb how many words to return
|
||||
* @return array
|
||||
* @param bool $asText if true the sentences are returned as one string
|
||||
* @return array|string
|
||||
*/
|
||||
public static function words($nb = 3)
|
||||
public static function words($nb = 3, $asText = false)
|
||||
{
|
||||
$words = array();
|
||||
for ($i=0; $i < $nb; $i++) {
|
||||
$words []= static::word();
|
||||
}
|
||||
return $words;
|
||||
return $asText ? join(' ', $words) : $words;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -97,15 +98,16 @@ class Lorem extends \Faker\Provider\Base
|
||||
*
|
||||
* @example array('Lorem ipsum dolor sit amet.', 'Consectetur adipisicing eli.')
|
||||
* @param integer $nb how many sentences to return
|
||||
* @return array
|
||||
* @param bool $asText if true the sentences are returned as one string
|
||||
* @return array|string
|
||||
*/
|
||||
public static function sentences($nb = 3)
|
||||
public static function sentences($nb = 3, $asText = false)
|
||||
{
|
||||
$sentences = array();
|
||||
for ($i=0; $i < $nb; $i++) {
|
||||
$sentences []= static::sentence();
|
||||
}
|
||||
return $sentences;
|
||||
return $asText ? join(' ', $sentences) : $sentences;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -134,15 +136,16 @@ class Lorem extends \Faker\Provider\Base
|
||||
*
|
||||
* @example array($paragraph1, $paragraph2, $paragraph3)
|
||||
* @param integer $nb how many paragraphs to return
|
||||
* @return array
|
||||
* @param bool $asText if true the paragraphs are returned as one string, separated by two newlines
|
||||
* @return array|string
|
||||
*/
|
||||
public static function paragraphs($nb = 3)
|
||||
public static function paragraphs($nb = 3, $asText = false)
|
||||
{
|
||||
$paragraphs = array();
|
||||
for ($i=0; $i < $nb; $i++) {
|
||||
$paragraphs []= static::paragraph();
|
||||
}
|
||||
return $paragraphs;
|
||||
return $asText ? join("\n\n", $paragraphs) : $paragraphs;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -34,6 +34,24 @@ class BaseTest extends \PHPUnit_Framework_TestCase
|
||||
$this->assertTrue(BaseProvider::randomNumber(3) < 1000);
|
||||
}
|
||||
|
||||
public function testRandomNumberAcceptsMinMax()
|
||||
{
|
||||
$min = 5;
|
||||
$max = 6;
|
||||
|
||||
$this->assertGreaterThanOrEqual($min, BaseProvider::randomNumber($min, $max));
|
||||
$this->assertGreaterThanOrEqual(BaseProvider::randomNumber($min, $max), $max);
|
||||
}
|
||||
|
||||
public function testNumberBetween()
|
||||
{
|
||||
$min = 5;
|
||||
$max = 6;
|
||||
|
||||
$this->assertGreaterThanOrEqual($min, BaseProvider::numberBetween($min, $max));
|
||||
$this->assertGreaterThanOrEqual(BaseProvider::numberBetween($min, $max), $max);
|
||||
}
|
||||
|
||||
public function testRandomLetterReturnsString()
|
||||
{
|
||||
$this->assertTrue(is_string(BaseProvider::randomLetter()));
|
||||
|
@ -65,6 +65,28 @@ class LoremTest extends \PHPUnit_Framework_TestCase
|
||||
$this->assertGreaterThan(1, strlen($paragraph));
|
||||
$this->assertGreaterThanOrEqual(1, count(explode(' ', $paragraph)));
|
||||
}
|
||||
|
||||
public function testWordssAsText()
|
||||
{
|
||||
$words = TestableLorem::words(2, true);
|
||||
|
||||
$this->assertEquals('word word', $words);
|
||||
}
|
||||
|
||||
public function testSentencesAsText()
|
||||
{
|
||||
$sentences = TestableLorem::sentences(2, true);
|
||||
|
||||
$this->assertEquals('This is a test sentence. This is a test sentence.', $sentences);
|
||||
}
|
||||
|
||||
public function testParagraphsAsText()
|
||||
{
|
||||
$paragraphs = TestableLorem::paragraphs(2, true);
|
||||
|
||||
$expected = "This is a test paragraph. It has three sentences. Exactly three.\n\nThis is a test paragraph. It has three sentences. Exactly three.";
|
||||
$this->assertEquals($expected, $paragraphs);
|
||||
}
|
||||
}
|
||||
|
||||
class TestableLorem extends Lorem
|
||||
|
Loading…
x
Reference in New Issue
Block a user