Skip to content
This repository has been archived by the owner on Mar 14, 2024. It is now read-only.

Commit

Permalink
Laravel 5.4.32 changes (#49)
Browse files Browse the repository at this point in the history
  • Loading branch information
besologic authored Aug 3, 2017
1 parent d194763 commit 7c232b5
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/Illuminate/Support/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -1070,7 +1070,7 @@ public function put($key, $value)
}

/**
* Get one or more items randomly from the collection.
* Get zero or more items randomly from the collection.
*
* @param int|null $amount
* @return mixed
Expand All @@ -1079,6 +1079,10 @@ public function put($key, $value)
*/
public function random($amount = null)
{
if ($amount === 0) {
return new static;
}

if (($requested = $amount ?: 1) > ($count = $this->count())) {
throw new InvalidArgumentException(
"You requested {$requested} items, but there are only {$count} items in the collection."
Expand Down
13 changes: 13 additions & 0 deletions tests/Support/SupportCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -911,6 +911,10 @@ public function testRandom()
{
$data = new Collection([1, 2, 3, 4, 5, 6]);

$random = $data->random(0);
$this->assertInstanceOf(Collection::class, $random);
$this->assertCount(0, $random);

$random = $data->random(1);
$this->assertInstanceOf(Collection::class, $random);
$this->assertCount(1, $random);
Expand All @@ -920,6 +924,15 @@ public function testRandom()
$this->assertCount(3, $random);
}

public function testRandomOnEmptyCollection()
{
$data = new Collection();

$random = $data->random(0);
$this->assertInstanceOf(Collection::class, $random);
$this->assertCount(0, $random);
}

public function testRandomWithoutArgument()
{
$data = new Collection([1, 2, 3, 4, 5, 6]);
Expand Down

0 comments on commit 7c232b5

Please sign in to comment.