109 lines
3.1 KiB
PHP
109 lines
3.1 KiB
PHP
<?php
|
|||
|
|
|
||
|
|
namespace Tests\Feature;
|
||
|
|
|
||
|
|
use App\Models\User;
|
||
|
|
use App\Models\Workout;
|
||
|
|
use App\Models\WorkoutSession;
|
||
|
|
use App\Notifications\InactivityReminder;
|
||
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||
|
|
use Illuminate\Support\Facades\Notification;
|
||
|
|
use Tests\TestCase;
|
||
|
|
|
||
|
|
class SendInactivityRemindersTest extends TestCase
|
||
|
|
{
|
||
|
|
use RefreshDatabase;
|
||
|
|
|
||
|
|
public function test_it_reminds_a_verified_inactive_user(): void
|
||
|
|
{
|
||
|
|
Notification::fake();
|
||
|
|
|
||
|
|
$user = User::factory()->create([
|
||
|
|
'email_verified_at' => now()->subDays(10),
|
||
|
|
'created_at' => now()->subDays(10),
|
||
|
|
]);
|
||
|
|
|
||
|
|
$this->artisan('app:send-inactivity-reminders')->assertSuccessful();
|
||
|
|
|
||
|
|
Notification::assertSentTo($user, InactivityReminder::class);
|
||
|
|
$this->assertNotNull($user->fresh()->last_reminder_sent_at);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_it_does_not_remind_a_user_with_a_recent_completed_session(): void
|
||
|
|
{
|
||
|
|
Notification::fake();
|
||
|
|
|
||
|
|
$user = User::factory()->create([
|
||
|
|
'email_verified_at' => now()->subDays(10),
|
||
|
|
'created_at' => now()->subDays(10),
|
||
|
|
]);
|
||
|
|
$workout = Workout::factory()->create(['user_id' => $user->id]);
|
||
|
|
WorkoutSession::factory()->create([
|
||
|
|
'user_id' => $user->id,
|
||
|
|
'workout_id' => $workout->id,
|
||
|
|
'completed_at' => now()->subDay(),
|
||
|
|
]);
|
||
|
|
|
||
|
|
$this->artisan('app:send-inactivity-reminders');
|
||
|
|
|
||
|
|
Notification::assertNotSentTo($user, InactivityReminder::class);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_it_does_not_remind_a_user_still_within_the_cooldown(): void
|
||
|
|
{
|
||
|
|
Notification::fake();
|
||
|
|
|
||
|
|
$user = User::factory()->create([
|
||
|
|
'email_verified_at' => now()->subDays(10),
|
||
|
|
'created_at' => now()->subDays(10),
|
||
|
|
'last_reminder_sent_at' => now()->subDays(2),
|
||
|
|
]);
|
||
|
|
|
||
|
|
$this->artisan('app:send-inactivity-reminders');
|
||
|
|
|
||
|
|
Notification::assertNotSentTo($user, InactivityReminder::class);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_it_reminds_again_once_the_cooldown_has_passed(): void
|
||
|
|
{
|
||
|
|
Notification::fake();
|
||
|
|
|
||
|
|
$user = User::factory()->create([
|
||
|
|
'email_verified_at' => now()->subDays(10),
|
||
|
|
'created_at' => now()->subDays(10),
|
||
|
|
'last_reminder_sent_at' => now()->subDays(8),
|
||
|
|
]);
|
||
|
|
|
||
|
|
$this->artisan('app:send-inactivity-reminders');
|
||
|
|
|
||
|
|
Notification::assertSentTo($user, InactivityReminder::class);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_it_does_not_remind_an_unverified_user(): void
|
||
|
|
{
|
||
|
|
Notification::fake();
|
||
|
|
|
||
|
|
$user = User::factory()->unverified()->create([
|
||
|
|
'created_at' => now()->subDays(10),
|
||
|
|
]);
|
||
|
|
|
||
|
|
$this->artisan('app:send-inactivity-reminders');
|
||
|
|
|
||
|
|
Notification::assertNotSentTo($user, InactivityReminder::class);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_it_does_not_remind_a_brand_new_user(): void
|
||
|
|
{
|
||
|
|
Notification::fake();
|
||
|
|
|
||
|
|
$user = User::factory()->create([
|
||
|
|
'email_verified_at' => now(),
|
||
|
|
'created_at' => now(),
|
||
|
|
]);
|
||
|
|
|
||
|
|
$this->artisan('app:send-inactivity-reminders');
|
||
|
|
|
||
|
|
Notification::assertNotSentTo($user, InactivityReminder::class);
|
||
|
|
}
|
||
|
|
}
|