From 57f534a4c958bb7aaceda0426527b29d364dca04 Mon Sep 17 00:00:00 2001 From: atymic Date: Thu, 14 Nov 2019 10:57:19 +1100 Subject: [PATCH] fix: pass notifiable object to shouldInterrupt (#31) - Pass object to method - Cover with tests + improve existing tests --- src/Models/ScheduledNotification.php | 14 ++++-- tests/CanInterruptNotificationTest.php | 43 +++++++++++++++---- .../TestInterruptableNotification.php | 4 +- 3 files changed, 47 insertions(+), 14 deletions(-) diff --git a/src/Models/ScheduledNotification.php b/src/Models/ScheduledNotification.php index 28b6e85..baba94f 100644 --- a/src/Models/ScheduledNotification.php +++ b/src/Models/ScheduledNotification.php @@ -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)); @@ -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; diff --git a/tests/CanInterruptNotificationTest.php b/tests/CanInterruptNotificationTest.php index a5527da..0c47f58 100644 --- a/tests/CanInterruptNotificationTest.php +++ b/tests/CanInterruptNotificationTest.php @@ -6,6 +6,7 @@ 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; @@ -13,29 +14,55 @@ 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); + } } diff --git a/tests/Notifications/TestInterruptableNotification.php b/tests/Notifications/TestInterruptableNotification.php index e1e09fd..034502e 100644 --- a/tests/Notifications/TestInterruptableNotification.php +++ b/tests/Notifications/TestInterruptableNotification.php @@ -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; } }