diff --git a/test/Faker/Provider/BaseTest.php b/test/Faker/Provider/BaseTest.php index 65ae520c3b..2bed8810ac 100644 --- a/test/Faker/Provider/BaseTest.php +++ b/test/Faker/Provider/BaseTest.php @@ -569,6 +569,66 @@ public function testRandomElements(): void self::assertCount(3, $allowDuplicates); self::assertContainsOnly('string', $allowDuplicates); } + + public function testRandomElements(): void + { + self::assertCount(1, BaseProvider::randomElements(), 'Should work without any input'); + + $empty = BaseProvider::randomElements([], 0); + self::assertIsArray($empty); + self::assertCount(0, $empty); + + $emptyTraversable = BaseProvider::randomElements(new \ArrayIterator(), 0); + self::assertIsArray($emptyTraversable); + self::assertCount(0, $emptyTraversable); + + $shuffled = BaseProvider::randomElements(['foo', 'bar', 'baz'], 3); + self::assertContains('foo', $shuffled); + self::assertContains('bar', $shuffled); + self::assertContains('baz', $shuffled); + + $allowDuplicates = BaseProvider::randomElements(['foo', 'bar'], 3, true); + self::assertCount(3, $allowDuplicates); + self::assertContainsOnly('string', $allowDuplicates); + } + + public function testRandomElementsWorksWithoutArgument(): void + { + self::assertCount(1, BaseProvider::randomElements(), 'Should work without any input'); + } + + public function testRandomElementsWorksWithEmptyArray(): void + { + $randomElements = BaseProvider::randomElements([], 0); + + self::assertIsArray($randomElements); + self::assertCount(0, $randomElements); + } + + public function testRandomElementsWorksWithEmptyTraversable(): void + { + $randomElements = BaseProvider::randomElements(new \ArrayIterator(), 0); + + self::assertIsArray($randomElements); + self::assertCount(0, $randomElements); + } + + public function testRandomElementsWorksWithNonEmptyTraversable(): void + { + $randomElements = BaseProvider::randomElements(['foo', 'bar', 'baz'], 3); + + self::assertContains('foo', $randomElements); + self::assertContains('bar', $randomElements); + self::assertContains('baz', $randomElements); + } + + public function testRandomElementsWorksWithAllowDuplicates(): void + { + $randomElements = BaseProvider::randomElements(['foo', 'bar'], 3, true); + + self::assertCount(3, $randomElements); + self::assertContainsOnly('string', $randomElements); + } } /**