Skip to content

Commit

Permalink
Merge pull request #12 from faissaloux/fix-pick-count
Browse files Browse the repository at this point in the history
Fix `pick($count)`
  • Loading branch information
valorin authored Jan 3, 2024
2 parents 280b869 + c4b6f51 commit 0d446cd
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 5 deletions.
18 changes: 13 additions & 5 deletions src/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -229,16 +229,24 @@ public function shuffle($values, bool $preserveKeys = false)
*/
public function pick($values, int $count)
{
if ($count < 1) {
throw new \InvalidArgumentException('Can not pick less than one item.');
}

if (! is_string($values) && ! is_array($values) && ! $values instanceof Collection) {
throw new \InvalidArgumentException('$value must be a string, array, or \Illuminate\Support\Collection.');
}

if ((is_string($values) && $count > strlen($values)) || (! is_string($values) && $count > count($values))) {
throw new \InvalidArgumentException('Can not pick more than existing elements.');
}

$values = $this->shuffle($values);

if ($count === 1) {
return $values[0];
}

if (is_array($values)) {
return array_slice($values, 0, $count);
}

if (is_string($values)) {
return substr($values, 0, $count);
}
Expand All @@ -247,7 +255,7 @@ public function pick($values, int $count)
return $values->slice(0, $count);
}

throw new \InvalidArgumentException('$value must be a string, array, or \Illuminate\Support\Collection.');
return array_slice($values, 0, $count);
}

/**
Expand Down
93 changes: 93 additions & 0 deletions tests/PickTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,99 @@ class PickTest extends TestCase
{
use Assertions;

public function testCantPickZeroElementFromArray()
{
for ($i = 0; $i < 10; $i++) {
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Can not pick less than one item.');

Random::pick(range('a', 'z'), 0);
}
}

public function testCantPickLessThanZeroElementFromArray()
{
for ($i = 0; $i < 10; $i++) {
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Can not pick less than one item.');

Random::pick(range('a', 'z'), -1);
}
}

public function testCantPickMoreThanArrayElements()
{
for ($i = 0; $i < 10; $i++) {
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Can not pick more than existing elements.');

Random::pick(range('a', 'z'), 27);
}
}

public function testCantPickZeroElementFromString()
{
for ($i = 0; $i < 10; $i++) {
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Can not pick less than one item.');

Random::pick('abcdefghijklmnopqrstuvwxyz', 0);
}
}

public function testCantPickLessThanZeroElementFromString()
{
for ($i = 0; $i < 10; $i++) {
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Can not pick less than one item.');

Random::pick('abcdefghijklmnopqrstuvwxyz', -1);
}
}

public function testCantPickMoreThanStringElements()
{
for ($i = 0; $i < 10; $i++) {
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Can not pick more than existing elements.');

Random::pick('abcdefghijklmnopqrstuvwxyz', 27);
}
}

public function testCantPickZeroElementFromCollection()
{
for ($i = 0; $i < 10; $i++) {
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Can not pick less than one item.');

$collection = new Collection(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']);
Random::pick($collection, 0);
}
}

public function testCantPickLessThanZeroElementFromCollection()
{
for ($i = 0; $i < 10; $i++) {
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Can not pick less than one item.');

$collection = new Collection(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']);
Random::pick($collection, -1);
}
}

public function testCantPickMoreThanCollectionElements()
{
for ($i = 0; $i < 10; $i++) {
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Can not pick more than existing elements.');

$collection = new Collection(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']);
Random::pick($collection, 10);
}
}

public function testPickSingleFromArray()
{
$differentPick = false;
Expand Down

0 comments on commit 0d446cd

Please sign in to comment.