Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Reject invalid arguments #642

Merged
merged 1 commit into from
May 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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