-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: atymic <[email protected]>
- Loading branch information
1 parent
7a5a7d2
commit bf0d701
Showing
5 changed files
with
182 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
namespace Thomasjohnkane\Snooze\Events; | ||
|
||
use Illuminate\Queue\SerializesModels; | ||
use Thomasjohnkane\Snooze\Models\ScheduledNotification; | ||
|
||
class NotificationRescheduled | ||
{ | ||
use SerializesModels; | ||
|
||
public $scheduledNotification; | ||
|
||
/** | ||
* Create a new event instance. | ||
* | ||
* @param \Thomasjohnkane\Snooze\Models\ScheduledNotification $scheduledNotification | ||
* @return void | ||
*/ | ||
public function __construct(ScheduledNotification $scheduledNotification) | ||
{ | ||
$this->scheduledNotification = $scheduledNotification; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<?php | ||
|
||
namespace Thomasjohnkane\Snooze\Tests; | ||
|
||
use Carbon\Carbon; | ||
use Illuminate\Support\Facades\Event; | ||
use Illuminate\Support\Facades\Notification; | ||
use Thomasjohnkane\Snooze\Events\NotificationRescheduled; | ||
use Thomasjohnkane\Snooze\Models\ScheduledNotification; | ||
use Thomasjohnkane\Snooze\Tests\Models\User; | ||
use Thomasjohnkane\Snooze\Tests\Notifications\TestReschedulableNotification; | ||
|
||
class CanRescheduleNotificationTest extends TestCase | ||
{ | ||
public function testNotificationIsRescheduled() | ||
{ | ||
Notification::fake(); | ||
Event::fake(); | ||
|
||
// User id 1 should be interrupted | ||
$target = User::find(1); | ||
$notification = $target->notifyAt(new TestReschedulableNotification(User::find(2)), Carbon::now()->subSeconds(10)); | ||
$this->assertDatabaseHas('scheduled_notifications', ['id' => $notification->getId()]); | ||
|
||
$this->artisan('snooze:send'); | ||
|
||
$notification->refresh(); | ||
$this->assertFalse($notification->isSent()); | ||
$this->assertFalse($notification->isCancelled()); | ||
$this->assertNotNull($notification->shouldRescheduleFor()); | ||
|
||
Notification::assertNothingSent(); | ||
Event::assertDispatched(NotificationRescheduled::class, 1); | ||
} | ||
|
||
public function testNotificationIsNotRescheduled() | ||
{ | ||
Notification::fake(); | ||
Event::fake(); | ||
|
||
// User id 2 should NOT be rescheduled. | ||
$target = User::find(2); | ||
|
||
$notification = $target->notifyAt(new TestReschedulableNotification(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->assertNull($notification->shouldRescheduleFor()); | ||
} | ||
|
||
public function testInterruptMethodReceivesNotifiable() | ||
{ | ||
$target = User::find(3); | ||
|
||
$notificationMock = $this->createMock(TestReschedulableNotification::class); | ||
$notificationMock | ||
->expects($this->once()) | ||
->method('shouldRescheduleFor') | ||
->with($target) | ||
->willReturn(null); | ||
|
||
$model = new ScheduledNotification(); | ||
$model->shouldRescheduleFor($notificationMock, $target); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
namespace Thomasjohnkane\Snooze\Tests\Notifications; | ||
|
||
use Illuminate\Bus\Queueable; | ||
use Illuminate\Contracts\Queue\ShouldQueue; | ||
use Illuminate\Notifications\Messages\MailMessage; | ||
use Illuminate\Notifications\Notification; | ||
use Thomasjohnkane\Snooze\Tests\Models\User; | ||
|
||
class TestReschedulableNotification extends Notification implements ShouldQueue | ||
{ | ||
use Queueable; | ||
|
||
/** @var User */ | ||
public $newUser; | ||
|
||
/** | ||
* @param User $newUser | ||
*/ | ||
public function __construct(User $newUser) | ||
{ | ||
$this->newUser = $newUser; | ||
} | ||
|
||
/** | ||
* Get the notification's channels. | ||
* | ||
* @param mixed $notifiable | ||
* @return array|string | ||
*/ | ||
public function via($notifiable) | ||
{ | ||
return ['mail']; | ||
} | ||
|
||
public function toMail($notifiable): MailMessage | ||
{ | ||
return (new MailMessage) | ||
->subject('New User') | ||
->line(sprintf('Email: %s', $this->newUser->email)); | ||
} | ||
|
||
public function shouldRescheduleFor(object $notifiable) | ||
{ | ||
if ($notifiable->id === 1) { | ||
return now()->addMinutes(5); | ||
} | ||
|
||
return null; | ||
} | ||
} |