1
0
mirror of https://github.com/fzaninotto/Faker.git synced 2025-03-24 09:19:50 +01:00

Merge pull request #266 from terite/randomElements

add Faker\Provider\Base::randomElements
This commit is contained in:
Francois Zaninotto 2014-03-06 14:55:13 +01:00
commit 5be755ad0f
3 changed files with 70 additions and 1 deletions

View File

@ -177,6 +177,7 @@ Each of the generator properties (like `name`, `address`, and `lorem`) are calle
randomNumber($from, $to) // 39049
randomFloat($nbMaxDecimals = NULL, $min = 0, $max = NULL) // 48.8932
randomLetter // 'b'
randomElements($array = array ('a','b','c'), $count = 1) // array('c')
randomElement($array = array ('a','b','c')) // 'b'
numerify($string = '###') // '609'
lexify($string = '????') // 'wgts'

View File

@ -123,6 +123,42 @@ class Base
return chr(mt_rand(97, 122));
}
/**
* Returns random elements from a provided array
*
* @param array $array Array to take elements from. Defaults to a-f
* @param integer $count Number of elements to take.
* @throws \LengthException When requesting more elements than provided
*
* @return array New array with $count elements from $array
*/
public static function randomElements(array $array = array('a', 'b', 'c'), $count = 1)
{
$allKeys = array_keys($array);
$numKeys = count($allKeys);
if ($numKeys < $count) {
throw new \LengthException(sprintf('Cannot get %d elements, only %d in array', $count, $numKeys));
}
$highKey = $numKeys - 1;
$keys = $elements = array();
$numElements = 0;
while ($numElements < $count) {
$num = mt_rand(0, $highKey);
if (isset($keys[$num])) {
continue;
}
$keys[$num] = true;
$elements[] = $array[$allKeys[$num]];
$numElements++;
}
return $elements;
}
/**
* Returns a random element from a passed array
*
@ -131,7 +167,11 @@ class Base
*/
public static function randomElement($array = array('a', 'b', 'c'))
{
return $array ? $array[self::randomKey($array)] : null;
if (!$array) {
return null;
}
$elements = static::randomElements($array, 1);
return $elements[0];
}
/**

View File

@ -84,6 +84,11 @@ class BaseTest extends \PHPUnit_Framework_TestCase
$this->assertTrue(strpos($lowercaseLetters, BaseProvider::randomLetter()) !== false);
}
public function testRandomElementReturnsNullWhenArrayEmpty()
{
$this->assertNull(BaseProvider::randomElement(array()));
}
public function testRandomElementReturnsElementFromArray()
{
$elements = array('23', 'e', 32, '#');
@ -229,4 +234,27 @@ class BaseTest extends \PHPUnit_Framework_TestCase
sort($values);
$this->assertEquals(array(0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9), $values);
}
/**
* @expectedException LengthException
* @expectedExceptionMessage Cannot get 2 elements, only 1 in array
*/
public function testRandomElementsThrowsWhenRequestingTooManyKeys()
{
BaseProvider::randomElements(array('foo'), 2);
}
public function testRandomElements()
{
$this->assertCount(1, BaseProvider::randomElements(), 'Should work without any input');
$empty = BaseProvider::randomElements(array(), 0);
$this->assertInternalType('array', $empty);
$this->assertCount(0, $empty);
$shuffled = BaseProvider::randomElements(array('foo', 'bar', 'baz'), 3);
$this->assertContains('foo', $shuffled);
$this->assertContains('bar', $shuffled);
$this->assertContains('baz', $shuffled);
}
}