Skip to content
This repository has been archived by the owner on Dec 11, 2020. It is now read-only.

Commit

Permalink
Merge pull request #1389 from browner12/random-collection
Browse files Browse the repository at this point in the history
allow `randomElements` to accept a Traversable object
  • Loading branch information
fzaninotto authored Jan 8, 2018
2 parents 429eb5a + 657df1b commit 251ec16
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
18 changes: 14 additions & 4 deletions src/Faker/Provider/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,19 @@ public static function randomAscii()
*
* @return array New array with $count elements from $array
*/
public static function randomElements(array $array = array('a', 'b', 'c'), $count = 1, $allowDuplicates = false)
public static function randomElements($array = array('a', 'b', 'c'), $count = 1, $allowDuplicates = false)
{
$allKeys = array_keys($array);
$traversables = array();

if ($array instanceof \Traversable) {
foreach ($array as $element) {
$traversables[] = $element;
}
}

$arr = count($traversables) ? $traversables : $array;

$allKeys = array_keys($arr);
$numKeys = count($allKeys);

if (!$allowDuplicates && $numKeys < $count) {
Expand All @@ -191,7 +201,7 @@ public static function randomElements(array $array = array('a', 'b', 'c'), $coun
$keys[$num] = true;
}

$elements[] = $array[$allKeys[$num]];
$elements[] = $arr[$allKeys[$num]];
$numElements++;
}

Expand All @@ -206,7 +216,7 @@ public static function randomElements(array $array = array('a', 'b', 'c'), $coun
*/
public static function randomElement($array = array('a', 'b', 'c'))
{
if (!$array) {
if (!$array || ($array instanceof \Traversable && !count($array))) {
return null;
}
$elements = static::randomElements($array, 1);
Expand Down
16 changes: 16 additions & 0 deletions test/Faker/Provider/BaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Faker\Provider\Base as BaseProvider;
use PHPUnit\Framework\TestCase;
use Traversable;

class BaseTest extends TestCase
{
Expand Down Expand Up @@ -134,6 +135,11 @@ public function testRandomElementReturnsNullWhenArrayEmpty()
$this->assertNull(BaseProvider::randomElement(array()));
}

public function testRandomElementReturnsNullWhenCollectionEmpty()
{
$this->assertNull(BaseProvider::randomElement(new Collection(array())));
}

public function testRandomElementReturnsElementFromArray()
{
$elements = array('23', 'e', 32, '#');
Expand All @@ -146,6 +152,12 @@ public function testRandomElementReturnsElementFromAssociativeArray()
$this->assertContains(BaseProvider::randomElement($elements), $elements);
}

public function testRandomElementReturnsElementFromCollection()
{
$collection = new Collection(array('one', 'two', 'three'));
$this->assertContains(BaseProvider::randomElement($collection), $collection);
}

public function testShuffleReturnsStringWhenPassedAStringArgument()
{
$this->assertInternalType('string', BaseProvider::shuffle('foo'));
Expand Down Expand Up @@ -554,3 +566,7 @@ public function testRandomElements()
$this->assertContainsOnly('string', $allowDuplicates);
}
}

class Collection extends \ArrayObject
{
}

0 comments on commit 251ec16

Please sign in to comment.