diff --git a/app/Http/Controllers/TimeTrackerController.php b/app/Http/Controllers/TimeTrackerController.php index ef5fe25..d92d823 100644 --- a/app/Http/Controllers/TimeTrackerController.php +++ b/app/Http/Controllers/TimeTrackerController.php @@ -53,7 +53,7 @@ class TimeTrackerController extends Controller */ public function show(TimeTracker $timeTracker) { - // + return response()->json(new TimeTrackerResource($timeTracker->load('toDo'))); } /** diff --git a/app/Http/Resources/ToDoResource.php b/app/Http/Resources/ToDoResource.php index 9826013..408246e 100644 --- a/app/Http/Resources/ToDoResource.php +++ b/app/Http/Resources/ToDoResource.php @@ -19,7 +19,7 @@ class ToDoResource extends JsonResource 'user_id' => $this->user_id, 'name' => $this->name, 'description' => $this->description, - 'checked' => $this->checked, + 'checked' => $this->checked?->format('Y-m-d H:i:s'), 'duration' => $this->duration, ]; } diff --git a/app/Models/ToDo.php b/app/Models/ToDo.php index c2b59ee..0c52900 100644 --- a/app/Models/ToDo.php +++ b/app/Models/ToDo.php @@ -17,6 +17,10 @@ class ToDo extends Model 'description', ]; + protected $casts = [ + 'checked' => 'datetime', + ]; + public function user(): BelongsTo { return $this->belongsTo(User::class); diff --git a/tests/Feature/TimeTrackerTest.php b/tests/Feature/TimeTrackerTest.php index 7285663..bd85050 100644 --- a/tests/Feature/TimeTrackerTest.php +++ b/tests/Feature/TimeTrackerTest.php @@ -54,6 +54,27 @@ test('user has no content response if not current time tracker', function () { ->assertNoContent(); }); +test('user can retrieve a time tracker', function () { + Sanctum::actingAs($user = User::factory()->create()); + $toDo = ToDo::factory()->create(['user_id' => $user->id, 'checked' => null]); + $timeTracker = $toDo->timeTrackers()->create([ + 'start_at' => now()->subMinutes(20), + 'end_at' => now()->subMinutes(10), + ]); + + $this->get('/api/time-trackers/'.$timeTracker->id) + ->assertOk() + ->assertJson([ + 'id' => $timeTracker->id, + 'start_at' => now()->subMinutes(20), + 'end_at' => now()->subMinutes(10), + 'to_do' => [ + 'id' => $toDo->id, + 'user_id' => $user->id, + ], + ]); +}); + test('user can stop current time tracker', function () { Sanctum::actingAs($user = User::factory()->create()); $toDo = ToDo::factory()->create(['user_id' => $user->id, 'checked' => null]); @@ -115,3 +136,18 @@ test('user can update a time tracker', function () { expect($toDo->refresh())->duration->toBe(660); }); + +test('user can retrieve time trackers of a todo', 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/todos/{$toDo->id}/time-trackers") + ->assertOk() + ->assertJsonCount(10); +}); diff --git a/tests/Feature/ToDoTest.php b/tests/Feature/ToDoTest.php index 968695d..cfaa04b 100644 --- a/tests/Feature/ToDoTest.php +++ b/tests/Feature/ToDoTest.php @@ -96,9 +96,8 @@ test('an user can update a to do', function () { $toDo = $toDos[rand(0, 9)]; - $response = $this->put('api/todos/'.$toDo->id, ['name' => 'update test']); - - $response->assertOk() + $this->put('api/todos/'.$toDo->id, ['name' => 'update test']) + ->assertOk() ->assertJson([ 'id' => $toDo->id, 'user_id' => $toDo->user_id, @@ -108,6 +107,15 @@ test('an user can update a to do', function () { expect(ToDo::find($toDo->id)) ->name->toBe('update test'); + + $this->put('api/todos/'.$toDo->id, ['checked' => true]) + ->assertOk() + ->assertJson([ + 'id' => $toDo->id, + 'user_id' => $toDo->user_id, + 'name' => 'update test', + 'checked' => now()->format('Y-m-d H:i:s'), + ]); }); test('an user can delete a to do', function () {