user = User::factory()->create(); }); test('an user can add todo', function () { Sanctum::actingAs($this->user); $response = $this->post('api/todos', [ 'name' => 'Test', ]); $response->assertStatus(201) ->assertJson([ 'user_id' => $this->user->id, 'name' => 'Test', 'checked' => false, ]); expect($this->user->toDos) ->toHaveCount(1) ->first()->name->toBe('Test') ->first()->checked->toBeNull(); }); test('an user can retrieve his to dos', function () { Sanctum::actingAs($this->user); ToDo::factory()->count(10)->create([ 'user_id' => $this->user->id, 'checked' => null, ]); $response = $this->get('api/todos'); $toDos = $this->user->toDos->map(fn ($toDo) => [ 'id' => $toDo->id, 'user_id' => $toDo->user_id, 'name' => $toDo->name, 'checked' => false, ])->toArray(); $response->assertOk() ->assertJson($toDos); }); test('an user can retrieve his finished to dos', function () { Sanctum::actingAs($this->user); ToDo::factory()->count(10)->create([ 'user_id' => $this->user->id, ]); $response = $this->get('api/todos/finished'); $toDos = $this->user->toDos()->whereNotNull('checked')->get()->map(fn ($toDo) => [ 'id' => $toDo->id, 'user_id' => $toDo->user_id, 'name' => $toDo->name, 'checked' => now(), ])->toArray(); $response->assertOk() ->assertJson($toDos); }); 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, ]); $toDo = $toDos[rand(0, 9)]; $response = $this->get('api/todos/'.$toDo->id); $response->assertOk() ->assertJson([ 'id' => $toDo->id, 'user_id' => $toDo->user_id, 'name' => $toDo->name, 'checked' => false, ]); }); 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, ]); $toDo = $toDos[rand(0, 9)]; $response = $this->put('api/todos/'.$toDo->id, ['name' => 'update test']); $response->assertOk() ->assertJson([ 'id' => $toDo->id, 'user_id' => $toDo->user_id, 'name' => 'update test', 'checked' => false, ]); expect(ToDo::find($toDo->id)) ->name->toBe('update test'); }); 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, ]); $toDo = $toDos[rand(0, 9)]; $response = $this->delete('api/todos/'.$toDo->id); $response->assertNoContent(); expect(ToDo::all())->toHaveCount(9); });