add some test
This commit is contained in:
@@ -2,11 +2,13 @@
|
||||
|
||||
use App\Models\ToDo;
|
||||
use App\Models\User;
|
||||
use Carbon\Carbon;
|
||||
use Laravel\Sanctum\Sanctum;
|
||||
|
||||
test('user can start a time tracker', function () {
|
||||
$this->withoutExceptionHandling();
|
||||
Sanctum::actingAs($user = User::factory()->create());
|
||||
$toDo = ToDo::factory()->create(['user_id' => $user->id, 'checked' => false]);
|
||||
$toDo = ToDo::factory()->create(['user_id' => $user->id, 'checked' => null]);
|
||||
|
||||
$this->postJson('/api/time-trackers', ['todo_id' => $toDo->id])
|
||||
->assertCreated()
|
||||
@@ -25,7 +27,7 @@ test('user can start a time tracker', function () {
|
||||
|
||||
test('user can retrieve his current timer', function () {
|
||||
Sanctum::actingAs($user = User::factory()->create());
|
||||
$toDo = ToDo::factory()->create(['user_id' => $user->id, 'checked' => false]);
|
||||
$toDo = ToDo::factory()->create(['user_id' => $user->id, 'checked' => null]);
|
||||
|
||||
$this->postJson('/api/time-trackers', ['todo_id' => $toDo->id])
|
||||
->assertCreated();
|
||||
@@ -40,7 +42,7 @@ test('user can retrieve his current timer', function () {
|
||||
'id' => $toDo->id,
|
||||
'user_id' => $user->id,
|
||||
'name' => $toDo->name,
|
||||
'checked' => false,
|
||||
'checked' => null,
|
||||
],
|
||||
]);
|
||||
});
|
||||
@@ -54,7 +56,7 @@ test('user has no content response if not current time tracker', function () {
|
||||
|
||||
test('user can stop current time tracker', function () {
|
||||
Sanctum::actingAs($user = User::factory()->create());
|
||||
$toDo = ToDo::factory()->create(['user_id' => $user->id, 'checked' => false]);
|
||||
$toDo = ToDo::factory()->create(['user_id' => $user->id, 'checked' => null]);
|
||||
|
||||
$this->postJson('/api/time-trackers', ['todo_id' => $toDo->id])
|
||||
->assertCreated();
|
||||
@@ -65,3 +67,51 @@ test('user can stop current time tracker', function () {
|
||||
expect($toDo->timeTrackers->first())
|
||||
->end_at->format('Y-m-d H:i:s')->toBe(now()->format('Y-m-d H:i:s'));
|
||||
});
|
||||
|
||||
test('user can destroy one of his time tracker', function () {
|
||||
Sanctum::actingAs($user = User::factory()->create());
|
||||
$toDo = ToDo::factory()->create(['user_id' => $user->id, 'duration' => 600, 'checked' => null]);
|
||||
$timeTracker = $toDo->timeTrackers()->create([
|
||||
'start_at' => now()->subMinutes(20),
|
||||
'end_at' => now()->subMinutes(10),
|
||||
]);
|
||||
|
||||
$this->deleteJson('/api/time-trackers/'.$timeTracker->id)
|
||||
->assertNoContent();
|
||||
});
|
||||
|
||||
test('user can retrieve all his time trackers', function () {
|
||||
Sanctum::actingAs($user = User::factory()->create());
|
||||
$toDo = ToDo::factory()->create(['user_id' => $user->id, 'duration' => 600, 'checked' => null]);
|
||||
for ($i = 10; $i > 0; $i--) {
|
||||
$toDo->timeTrackers()->create([
|
||||
'start_at' => now()->subMinutes(20 + $i * 10),
|
||||
'end_at' => now()->subMinutes(10 + $i * 10),
|
||||
]);
|
||||
}
|
||||
|
||||
$this->get('/api/time-trackers')
|
||||
->assertOk()
|
||||
->assertJsonCount(10)
|
||||
->assertJson([
|
||||
['id' => $toDo->timeTrackers()->orderBy('start_at', 'desc')->first()->id],
|
||||
]);
|
||||
});
|
||||
|
||||
test('user can update a time tracker', function () {
|
||||
Sanctum::actingAs($user = User::factory()->create());
|
||||
$toDo = ToDo::factory()->create(['user_id' => $user->id, 'duration' => 600, 'checked' => null]);
|
||||
|
||||
$date = '2024-04-01 14:00:00';
|
||||
$timeTracker = $toDo->timeTrackers()->create([
|
||||
'start_at' => Carbon::parse($date)->subMinutes(20),
|
||||
'end_at' => Carbon::parse($date)->subMinutes(10),
|
||||
]);
|
||||
|
||||
$this->putJson('/api/time-trackers/'.$timeTracker->id, [
|
||||
'start_at' => $timeTracker->start_at->format('Y-m-d H:i:s'),
|
||||
'end_at' => Carbon::parse($date)->subMinutes(9)->format('Y-m-d H:i:s'),
|
||||
]);
|
||||
|
||||
expect($toDo->refresh())->duration->toBe(660);
|
||||
});
|
||||
|
||||
@@ -71,7 +71,7 @@ test('an user can retrieve a to do', function () {
|
||||
Sanctum::actingAs($this->user);
|
||||
$toDos = ToDo::factory()->count(10)->create([
|
||||
'user_id' => $this->user->id,
|
||||
'checked' => false,
|
||||
'checked' => null,
|
||||
]);
|
||||
|
||||
$toDo = $toDos[rand(0, 9)];
|
||||
@@ -91,7 +91,7 @@ test('an user can update a to do', function () {
|
||||
Sanctum::actingAs($this->user);
|
||||
$toDos = ToDo::factory()->count(10)->create([
|
||||
'user_id' => $this->user->id,
|
||||
'checked' => false,
|
||||
'checked' => null,
|
||||
]);
|
||||
|
||||
$toDo = $toDos[rand(0, 9)];
|
||||
@@ -103,7 +103,7 @@ test('an user can update a to do', function () {
|
||||
'id' => $toDo->id,
|
||||
'user_id' => $toDo->user_id,
|
||||
'name' => 'update test',
|
||||
'checked' => false,
|
||||
'checked' => null,
|
||||
]);
|
||||
|
||||
expect(ToDo::find($toDo->id))
|
||||
@@ -114,7 +114,7 @@ test('an user can delete a to do', function () {
|
||||
Sanctum::actingAs($this->user);
|
||||
$toDos = ToDo::factory()->count(10)->create([
|
||||
'user_id' => $this->user->id,
|
||||
'checked' => false,
|
||||
'checked' => null,
|
||||
]);
|
||||
|
||||
$toDo = $toDos[rand(0, 9)];
|
||||
|
||||
Reference in New Issue
Block a user