|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Thomasjohnkane\Snooze\Tests; |
| 4 | + |
| 5 | +use Carbon\Carbon; |
| 6 | +use Illuminate\Support\Facades\Event; |
| 7 | +use Illuminate\Support\Facades\Notification; |
| 8 | +use Thomasjohnkane\Snooze\Events\NotificationDeleted; |
| 9 | +use Thomasjohnkane\Snooze\Tests\Models\User; |
| 10 | +use Thomasjohnkane\Snooze\Tests\Notifications\TestNotification; |
| 11 | + |
| 12 | +class CanDeleteNotificationTest extends TestCase |
| 13 | +{ |
| 14 | + public function testNotificationIsDeleted() |
| 15 | + { |
| 16 | + Notification::fake(); |
| 17 | + Event::fake(); |
| 18 | + |
| 19 | + $this->app->config->set('snooze.deleteWhenMissingModels', true); |
| 20 | + |
| 21 | + $target = User::find(1); |
| 22 | + $notification = $target->notifyAt(new TestNotification(User::find(1)), Carbon::now()->subSeconds(10)); |
| 23 | + $target->delete(); |
| 24 | + |
| 25 | + $this->assertDatabaseHas('scheduled_notifications', ['id' => $notification->getId()]); |
| 26 | + |
| 27 | + $this->artisan('snooze:send'); |
| 28 | + |
| 29 | + $this->assertDatabaseMissing('scheduled_notifications', ['id' => $notification->getId()]); |
| 30 | + |
| 31 | + Notification::assertNothingSent(); |
| 32 | + Event::assertDispatched(NotificationDeleted::class, 1); |
| 33 | + } |
| 34 | + |
| 35 | + public function testNotificationIsNotDeleted() |
| 36 | + { |
| 37 | + Notification::fake(); |
| 38 | + Event::fake(); |
| 39 | + |
| 40 | + $this->app->config->set('snooze.deleteWhenMissingModels', false); |
| 41 | + |
| 42 | + $target = User::find(2); |
| 43 | + |
| 44 | + $notification = $target->notifyAt(new TestNotification(User::find(2)), Carbon::now()->subSeconds(10)); |
| 45 | + $target->delete(); |
| 46 | + |
| 47 | + $this->assertDatabaseHas('scheduled_notifications', ['id' => $notification->getId()]); |
| 48 | + |
| 49 | + $this->artisan('snooze:send'); |
| 50 | + |
| 51 | + $this->assertDatabaseHas('scheduled_notifications', ['id' => $notification->getId()]); |
| 52 | + $this->assertFalse($notification->isSent()); |
| 53 | + |
| 54 | + Notification::assertNothingSent(); |
| 55 | + Event::assertNotDispatched(NotificationDeleted::class); |
| 56 | + } |
| 57 | +} |
0 commit comments