Skip to content

Commit

Permalink
fix: pass notifiable object to shouldInterrupt (#31)
Browse files Browse the repository at this point in the history
- Pass object to method
- Cover with tests + improve existing tests
  • Loading branch information
atymic authored and thomasjohnkane committed Nov 13, 2019
1 parent f1072ce commit 57f534a
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 14 deletions.
14 changes: 10 additions & 4 deletions src/Models/ScheduledNotification.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public function send(): void
$notifiable = $this->serializer->unserializeNotifiable($this->target);
$notification = $this->serializer->unserializeNotification($this->notification);

if ($this->shouldInterrupt($notification)) {
if ($this->shouldInterrupt($notification, $notifiable)) {
$this->cancel();
event(new NotificationInterrupted($this));

Expand All @@ -81,18 +81,24 @@ public function send(): void
}

/**
* @param object $notification
* @param object|null $notification
*
* @param object|null $notifiable
*
* @return bool
*/
public function shouldInterrupt(?object $notification = null): bool
public function shouldInterrupt(?object $notification = null, ?object $notifiable = null): bool
{
if (! $notification) {
$notification = $this->serializer->unserializeNotification($this->notification);
}

if (! $notifiable) {
$notifiable = $this->serializer->unserializeNotifiable($this->target);
}

if (method_exists($notification, 'shouldInterrupt')) {
return (bool) $notification->shouldInterrupt();
return (bool) $notification->shouldInterrupt($notifiable);
}

return false;
Expand Down
43 changes: 35 additions & 8 deletions tests/CanInterruptNotificationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,63 @@
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Notification;
use Thomasjohnkane\Snooze\Events\NotificationInterrupted;
use Thomasjohnkane\Snooze\Models\ScheduledNotification;
use Thomasjohnkane\Snooze\Tests\Models\User;
use Thomasjohnkane\Snooze\Tests\Notifications\TestInterruptableNotification;

class CanInterruptNotificationTest extends TestCase
{
public function testNotificationIsInterrupted()
{
// Arrange
// Create Stub
Notification::fake();

Event::fake();

// User id 1 should be interrupted
$target = User::find(1);

// Act
$notification = $target->notifyAt(new TestInterruptableNotification(User::find(2)), Carbon::now()->subSeconds(10));
$this->assertDatabaseHas('scheduled_notifications', ['id' => $notification->getId()]);

$this->artisan('snooze:send');

// Assert
// Check wasn't sent (exception?)
$notification->refresh();
$this->assertFalse($notification->isSent());
$this->assertTrue($notification->isCancelled());
$this->assertTrue($notification->shouldInterrupt());

Notification::assertNothingSent();

Event::assertDispatched(NotificationInterrupted::class, 1);
}

public function testNotificationIsNotInterrupted()
{
Notification::fake();
Event::fake();

// User id 2 should NOT be interrupted
$target = User::find(2);

$notification = $target->notifyAt(new TestInterruptableNotification(User::find(2)), Carbon::now()->subSeconds(10));
$this->assertDatabaseHas('scheduled_notifications', ['id' => $notification->getId()]);
$this->artisan('snooze:send');

$notification->refresh();
$this->assertTrue($notification->isSent());
$this->assertFalse($notification->isCancelled());
$this->assertFalse($notification->shouldInterrupt());
}

public function testInterruptMethodReceivesNotifiable()
{
$target = User::find(3);

$notificationMock = $this->createMock(TestInterruptableNotification::class);
$notificationMock
->expects($this->once())
->method('shouldInterrupt')
->with($target)
->willReturn(true);

$model = new ScheduledNotification();
$model->shouldInterrupt($notificationMock, $target);
}
}
4 changes: 2 additions & 2 deletions tests/Notifications/TestInterruptableNotification.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ public function toMail($notifiable): MailMessage
->line(sprintf('Email: %s', $this->newUser->email));
}

public function shouldInterrupt()
public function shouldInterrupt(object $notifiable)
{
return true;
return $notifiable->id === 1;
}
}

0 comments on commit 57f534a

Please sign in to comment.