diff --git a/src/Faker/Provider/Base.php b/src/Faker/Provider/Base.php index eec675ae20..d48b57d5d6 100644 --- a/src/Faker/Provider/Base.php +++ b/src/Faker/Provider/Base.php @@ -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 */ @@ -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) { @@ -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']) { @@ -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]; diff --git a/test/Faker/Provider/BaseTest.php b/test/Faker/Provider/BaseTest.php index 54b2dab9a1..f1a1d61808 100644 --- a/test/Faker/Provider/BaseTest.php +++ b/test/Faker/Provider/BaseTest.php @@ -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');