-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOverdueNotificationTest.php
More file actions
141 lines (106 loc) · 4.03 KB
/
Copy pathOverdueNotificationTest.php
File metadata and controls
141 lines (106 loc) · 4.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<?php
declare(strict_types=1);
namespace Tests\Feature;
use App\Enums\TaskStatus;
use App\Jobs\NotifyTaskOverdue;
use App\Models\Project;
use App\Models\Task;
use App\Models\User;
use App\Notifications\TaskOverdueNotification;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Bus;
use Illuminate\Support\Facades\Notification;
use PHPUnit\Framework\Attributes\Test;
use Tests\TestCase;
class OverdueNotificationTest extends TestCase
{
use RefreshDatabase;
private User $user;
private Project $project;
protected function setUp(): void
{
parent::setUp();
$this->user = User::factory()->create();
$this->project = Project::factory()->for($this->user)->create();
}
#[Test]
public function the_command_queues_a_job_for_every_overdue_task(): void
{
Bus::fake();
$overdue = Task::factory(3)->overdue()->for($this->project)->create();
Task::factory(2)->for($this->project)->create(['due_date' => now()->addWeek()]);
Task::factory()->done()->for($this->project)->create(['due_date' => now()->subWeek()]);
$this->artisan('tasks:notify-overdue')
->expectsOutputToContain('Queued 3 overdue notification(s).')
->assertSuccessful();
Bus::assertDispatchedTimes(NotifyTaskOverdue::class, 3);
Bus::assertDispatched(
NotifyTaskOverdue::class,
fn (NotifyTaskOverdue $job) => $overdue->contains($job->task->id),
);
}
#[Test]
public function the_command_skips_tasks_that_were_already_notified(): void
{
Bus::fake();
Task::factory()->overdue()->for($this->project)->create(['overdue_notified_at' => now()]);
$this->artisan('tasks:notify-overdue')->assertSuccessful();
Bus::assertNothingDispatched();
}
#[Test]
public function the_job_notifies_the_owner_and_stamps_the_task(): void
{
Notification::fake();
$task = Task::factory()->overdue()->for($this->project)->create();
(new NotifyTaskOverdue($task))->handle();
Notification::assertSentTo(
$this->user,
TaskOverdueNotification::class,
fn (TaskOverdueNotification $notification) => $notification->task->is($task),
);
$this->assertNotNull($task->fresh()->overdue_notified_at);
}
#[Test]
public function the_job_does_nothing_when_the_task_was_finished_meanwhile(): void
{
Notification::fake();
$task = Task::factory()->overdue()->for($this->project)->create();
$task->update(['status' => TaskStatus::Done]);
(new NotifyTaskOverdue($task))->handle();
Notification::assertNothingSent();
$this->assertNull($task->fresh()->overdue_notified_at);
}
#[Test]
public function the_job_does_nothing_when_the_task_was_deleted_meanwhile(): void
{
Notification::fake();
$task = Task::factory()->overdue()->for($this->project)->create();
$task->delete();
(new NotifyTaskOverdue($task))->handle();
Notification::assertNothingSent();
}
#[Test]
public function rescheduling_a_task_allows_a_new_notification(): void
{
$task = Task::factory()->overdue()->for($this->project)->create([
'overdue_notified_at' => now(),
]);
$this->actingAs($this->user, 'sanctum')
->patchJson(route('api.v1.tasks.update', $task), [
'due_date' => now()->addWeek()->toDateTimeString(),
])
->assertOk();
$this->assertNull($task->fresh()->overdue_notified_at);
}
#[Test]
public function editing_another_field_keeps_the_notification_stamp(): void
{
$task = Task::factory()->overdue()->for($this->project)->create([
'overdue_notified_at' => now(),
]);
$this->actingAs($this->user, 'sanctum')
->patchJson(route('api.v1.tasks.update', $task), ['title' => 'Renamed'])
->assertOk();
$this->assertNotNull($task->fresh()->overdue_notified_at);
}
}