118 lines
3.9 KiB
PHP
118 lines
3.9 KiB
PHP
<?php
|
|
|
|
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' => null]);
|
|
|
|
$this->postJson('/api/time-trackers', ['todo_id' => $toDo->id])
|
|
->assertCreated()
|
|
->assertJson([
|
|
'id' => $toDo->timeTrackers()->value('id'),
|
|
'start_at' => now()->format('Y-m-d H:i:s'),
|
|
'end_at' => null,
|
|
'to_do' => [
|
|
'id' => $toDo->id,
|
|
'user_id' => $user->id,
|
|
'name' => $toDo->name,
|
|
'checked' => false,
|
|
],
|
|
]);
|
|
});
|
|
|
|
test('user can retrieve his current timer', function () {
|
|
Sanctum::actingAs($user = User::factory()->create());
|
|
$toDo = ToDo::factory()->create(['user_id' => $user->id, 'checked' => null]);
|
|
|
|
$this->postJson('/api/time-trackers', ['todo_id' => $toDo->id])
|
|
->assertCreated();
|
|
|
|
$this->get('/api/time-trackers/user')
|
|
->assertOk()
|
|
->assertJson([
|
|
'id' => $toDo->timeTrackers()->value('id'),
|
|
'start_at' => now()->format('Y-m-d H:i:s'),
|
|
'end_at' => null,
|
|
'to_do' => [
|
|
'id' => $toDo->id,
|
|
'user_id' => $user->id,
|
|
'name' => $toDo->name,
|
|
'checked' => null,
|
|
],
|
|
]);
|
|
});
|
|
|
|
test('user has no content response if not current time tracker', function () {
|
|
Sanctum::actingAs($user = User::factory()->create());
|
|
|
|
$this->get('/api/time-trackers/user')
|
|
->assertNoContent();
|
|
});
|
|
|
|
test('user can stop current time tracker', function () {
|
|
Sanctum::actingAs($user = User::factory()->create());
|
|
$toDo = ToDo::factory()->create(['user_id' => $user->id, 'checked' => null]);
|
|
|
|
$this->postJson('/api/time-trackers', ['todo_id' => $toDo->id])
|
|
->assertCreated();
|
|
|
|
$this->delete('/api/time-trackers/user')
|
|
->assertNoContent();
|
|
|
|
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);
|
|
});
|