This commit is contained in:
Romulus21
2024-04-01 20:16:04 +02:00
parent 73a5cd4e2f
commit a172cdeb37
5 changed files with 53 additions and 5 deletions

View File

@@ -53,7 +53,7 @@ class TimeTrackerController extends Controller
*/ */
public function show(TimeTracker $timeTracker) public function show(TimeTracker $timeTracker)
{ {
// return response()->json(new TimeTrackerResource($timeTracker->load('toDo')));
} }
/** /**

View File

@@ -19,7 +19,7 @@ class ToDoResource extends JsonResource
'user_id' => $this->user_id, 'user_id' => $this->user_id,
'name' => $this->name, 'name' => $this->name,
'description' => $this->description, 'description' => $this->description,
'checked' => $this->checked, 'checked' => $this->checked?->format('Y-m-d H:i:s'),
'duration' => $this->duration, 'duration' => $this->duration,
]; ];
} }

View File

@@ -17,6 +17,10 @@ class ToDo extends Model
'description', 'description',
]; ];
protected $casts = [
'checked' => 'datetime',
];
public function user(): BelongsTo public function user(): BelongsTo
{ {
return $this->belongsTo(User::class); return $this->belongsTo(User::class);

View File

@@ -54,6 +54,27 @@ test('user has no content response if not current time tracker', function () {
->assertNoContent(); ->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 () { test('user can stop current time tracker', function () {
Sanctum::actingAs($user = User::factory()->create()); Sanctum::actingAs($user = User::factory()->create());
$toDo = ToDo::factory()->create(['user_id' => $user->id, 'checked' => null]); $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); 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);
});

View File

@@ -96,9 +96,8 @@ test('an user can update a to do', function () {
$toDo = $toDos[rand(0, 9)]; $toDo = $toDos[rand(0, 9)];
$response = $this->put('api/todos/'.$toDo->id, ['name' => 'update test']); $this->put('api/todos/'.$toDo->id, ['name' => 'update test'])
->assertOk()
$response->assertOk()
->assertJson([ ->assertJson([
'id' => $toDo->id, 'id' => $toDo->id,
'user_id' => $toDo->user_id, 'user_id' => $toDo->user_id,
@@ -108,6 +107,15 @@ test('an user can update a to do', function () {
expect(ToDo::find($toDo->id)) expect(ToDo::find($toDo->id))
->name->toBe('update test'); ->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 () { test('an user can delete a to do', function () {