Skip to content

Commit

Permalink
Throw exception on empty collection (#31471)
Browse files Browse the repository at this point in the history
  • Loading branch information
driesvints authored Feb 14, 2020
1 parent 0c4ef0f commit 3399284
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/Illuminate/Support/Testing/Fakes/NotificationFake.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Illuminate\Support\Testing\Fakes;

use Exception;
use Illuminate\Contracts\Notifications\Dispatcher as NotificationDispatcher;
use Illuminate\Contracts\Notifications\Factory as NotificationFactory;
use Illuminate\Contracts\Translation\HasLocalePreference;
Expand Down Expand Up @@ -35,10 +36,16 @@ class NotificationFake implements NotificationDispatcher, NotificationFactory
* @param string $notification
* @param callable|null $callback
* @return void
*
* @throws \Exception
*/
public function assertSentTo($notifiable, $notification, $callback = null)
{
if (is_array($notifiable) || $notifiable instanceof Collection) {
if (count($notifiable) === 0) {
throw new Exception('No notifiable given.');
}

foreach ($notifiable as $singleNotifiable) {
$this->assertSentTo($singleNotifiable, $notification, $callback);
}
Expand Down Expand Up @@ -79,10 +86,16 @@ public function assertSentToTimes($notifiable, $notification, $times = 1)
* @param string $notification
* @param callable|null $callback
* @return void
*
* @throws \Exception
*/
public function assertNotSentTo($notifiable, $notification, $callback = null)
{
if (is_array($notifiable) || $notifiable instanceof Collection) {
if (count($notifiable) === 0) {
throw new Exception('No notifiable given.');
}

foreach ($notifiable as $singleNotifiable) {
$this->assertNotSentTo($singleNotifiable, $notification, $callback);
}
Expand Down
16 changes: 16 additions & 0 deletions tests/Support/SupportTestingNotificationFakeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

namespace Illuminate\Tests\Support;

use Exception;
use Illuminate\Contracts\Translation\HasLocalePreference;
use Illuminate\Foundation\Auth\User;
use Illuminate\Notifications\Notification;
use Illuminate\Support\Collection;
use Illuminate\Support\Testing\Fakes\NotificationFake;
use PHPUnit\Framework\Constraint\ExceptionMessage;
use PHPUnit\Framework\ExpectationFailedException;
Expand Down Expand Up @@ -63,6 +65,20 @@ public function testAssertNotSentTo()
}
}

public function testAssertSentToFailsForEmptyArray()
{
$this->expectException(Exception::class);

$this->fake->assertSentTo([], NotificationStub::class);
}

public function testAssertSentToFailsForEmptyCollection()
{
$this->expectException(Exception::class);

$this->fake->assertSentTo(new Collection, NotificationStub::class);
}

public function testResettingNotificationId()
{
$this->fake->send($this->user, $this->notification);
Expand Down

0 comments on commit 3399284

Please sign in to comment.