Skip to content

Commit

Permalink
Fix: Reject invalid arguments (#642)
Browse files Browse the repository at this point in the history
  • Loading branch information
localheinz authored May 2, 2023
1 parent 71997e4 commit cd773f4
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
21 changes: 20 additions & 1 deletion src/Faker/Provider/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,8 @@ public static function randomAscii()
* @param int $count Number of elements to take.
* @param bool $allowDuplicates Allow elements to be picked several times. Defaults to false
*
* @throws \LengthException When requesting more elements than provided
* @throws \InvalidArgumentException
* @throws \LengthException When requesting more elements than provided
*
* @return array New array with $count elements from $array
*/
Expand All @@ -195,6 +196,14 @@ public static function randomElements($array = ['a', 'b', 'c'], $count = 1, $all
$elements = \iterator_to_array($array, false);
}

if (!is_array($elements)) {
throw new \InvalidArgumentException(sprintf(
'Argument for parameter $array needs to be array or an instance of %s, got %s instead.',
\Traversable::class,
is_object($array) ? get_class($array) : gettype($array),
));
}

$numberOfElements = count($elements);

if (!$allowDuplicates && $numberOfElements < $count) {
Expand Down Expand Up @@ -237,6 +246,8 @@ public static function randomElements($array = ['a', 'b', 'c'], $count = 1, $all
* Returns a random element from a passed array
*
* @param array|\Traversable $array
*
* @throws \InvalidArgumentException
*/
public static function randomElement($array = ['a', 'b', 'c'])
{
Expand All @@ -250,6 +261,14 @@ public static function randomElement($array = ['a', 'b', 'c'])
return null;
}

if (!is_array($elements)) {
throw new \InvalidArgumentException(sprintf(
'Argument for parameter $array needs to be array or an instance of %s, got %s instead.',
\Traversable::class,
is_object($array) ? get_class($array) : gettype($array),
));
}

$randomElements = static::randomElements($elements, 1);

return $randomElements[0];
Expand Down
24 changes: 24 additions & 0 deletions test/Faker/Provider/BaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,30 @@ public function testRandomElementsThrowsWhenRequestingTooManyKeys(): void
BaseProvider::randomElements(['foo'], 2);
}

public function testRandomElementsRejectsInvalidArgument(): void
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage(sprintf(
'Argument for parameter $array needs to be array or an instance of %s, got %s instead.',
\Traversable::class,
\stdClass::class,
));

BaseProvider::randomElements(new \stdClass());
}

public function testRandomElementRejectsInvalidArgument(): void
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage(sprintf(
'Argument for parameter $array needs to be array or an instance of %s, got %s instead.',
\Traversable::class,
'string',
));

BaseProvider::randomElement('foo');
}

public function testRandomElementsWorksWithoutArgument(): void
{
self::assertCount(1, BaseProvider::randomElements(), 'Should work without any input');
Expand Down

0 comments on commit cd773f4

Please sign in to comment.