From 1065da076d6130de4d7dcb85303cfdabab27b342 Mon Sep 17 00:00:00 2001 From: Romulus21 Date: Mon, 1 Apr 2024 19:47:15 +0200 Subject: [PATCH] add some test --- .../Controllers/TimeTrackerController.php | 16 ++++- app/Http/Controllers/ToDoController.php | 6 +- app/Http/Requests/ToDoRequest.php | 2 +- resources/js/components/SVG.tsx | 6 ++ resources/js/pages/ToDos/ToDoShow.tsx | 20 ++++++- tests/Feature/TimeTrackerTest.php | 58 +++++++++++++++++-- tests/Feature/ToDoTest.php | 8 +-- 7 files changed, 102 insertions(+), 14 deletions(-) diff --git a/app/Http/Controllers/TimeTrackerController.php b/app/Http/Controllers/TimeTrackerController.php index 1218999..ef5fe25 100644 --- a/app/Http/Controllers/TimeTrackerController.php +++ b/app/Http/Controllers/TimeTrackerController.php @@ -63,12 +63,17 @@ class TimeTrackerController extends Controller { abort_if($timeTracker->toDo->user_id !== $request->user()->id, 401, __('auth.unauthorized')); + $beforeDelay = $timeTracker->end_at->diffInSeconds($timeTracker->start_at); $data = $request->validate([ 'start_at' => ['required', 'date'], 'end_at' => ['nullable', 'date', 'after_or_equal:start_at'], ]); $timeTracker->update($data); + $timeTracker->fresh(); + $afterDelay = $timeTracker->end_at->diffInSeconds($timeTracker->start_at); + + $timeTracker->toDo->increment('duration', $afterDelay - $beforeDelay); return response()->json(new TimeTrackerResource($timeTracker)); } @@ -76,9 +81,16 @@ class TimeTrackerController extends Controller /** * Remove the specified resource from storage. */ - public function destroy(TimeTracker $timeTracker) + public function destroy(Request $request, TimeTracker $timeTracker) { - // + abort_if($timeTracker->toDo->user_id !== $request->user()->id, 401, __('auth.unauthorized')); + + $delay = $timeTracker->end_at->diffInSeconds($timeTracker->start_at); + $timeTracker->toDo->decrement('duration', $delay); + + $timeTracker->delete(); + + return response()->noContent(); } public function userTimeTracker(Request $request) diff --git a/app/Http/Controllers/ToDoController.php b/app/Http/Controllers/ToDoController.php index 9b99157..19f84a5 100644 --- a/app/Http/Controllers/ToDoController.php +++ b/app/Http/Controllers/ToDoController.php @@ -46,7 +46,11 @@ class ToDoController extends Controller { abort_if($todo->user_id !== $request->user()->id, 401, __('auth.unauthorized')); - $todo->update($request->validated()); + $data = $request->validated(); + if (isset($data['checked'])) { + $data['checked'] = $data['checked'] ? now() : null; + } + $todo->update($data); return response()->json(new ToDoResource($todo)); } diff --git a/app/Http/Requests/ToDoRequest.php b/app/Http/Requests/ToDoRequest.php index e2d6769..ad81bf0 100644 --- a/app/Http/Requests/ToDoRequest.php +++ b/app/Http/Requests/ToDoRequest.php @@ -23,7 +23,7 @@ class ToDoRequest extends FormRequest { return [ 'name' => ['string', 'min:3', 'max:255'], - 'description' => ['string', 'max:2000'], + 'description' => ['nullable', 'string', 'max:2000'], 'checked' => ['nullable', 'bool'], ]; } diff --git a/resources/js/components/SVG.tsx b/resources/js/components/SVG.tsx index 26bbbab..0003dd5 100644 --- a/resources/js/components/SVG.tsx +++ b/resources/js/components/SVG.tsx @@ -64,3 +64,9 @@ export const StopSVG: FC> = (props) => SVGSkeleton({ paths: , ...props }) + +export const TrashSVG: FC> = (props) => SVGSkeleton({ + viewBox: "0 0 448 512", + paths: , + ...props +}) diff --git a/resources/js/pages/ToDos/ToDoShow.tsx b/resources/js/pages/ToDos/ToDoShow.tsx index 090010f..ae7b287 100644 --- a/resources/js/pages/ToDos/ToDoShow.tsx +++ b/resources/js/pages/ToDos/ToDoShow.tsx @@ -2,7 +2,7 @@ import React, {FC, useEffect, useState} from "react" import {useParams} from "react-router-dom" import useAxiosTools from "../../hooks/AxiosTools" import {timeTracker, toDo} from "../../utilities/types" -import {EditSVG} from "../../components/SVG" +import {EditSVG, TrashSVG} from "../../components/SVG" import Field, {TextArea} from "../../components/Field" import TimeTrackerEdit from "../../components/TimeTrackers/TimeTrackerEdit" import {Modal} from "../../components/Modals" @@ -70,7 +70,7 @@ export default ToDoShow const ToDoTimeTrackers: FC<{toDo: toDo}> = ({toDo: toDo}) => { - const {setLoading, errorCatch, errorLabel, axiosGet} = useAxiosTools(true) + const {setLoading, errorCatch, errorLabel, axiosGet, axiosDelete} = useAxiosTools(true) const [timeTrackers, setTimeTrackers] = useState([]) const [showTrackers, setShowTrackers] = useState(null) const [reload, setReload] = useState(null) @@ -112,6 +112,17 @@ const ToDoTimeTrackers: FC<{toDo: toDo}> = ({toDo: toDo}) => { return (more ? '+' : '') + timer.durationify() } + const destroyTimeTracker = async (timeTracker: timeTracker) => { + try { + await axiosDelete('/api/time-trackers/' + timeTracker.id) + await fetchTimeTrackers() + } catch (error) { + errorCatch(error) + } finally { + setLoading(false) + } + } + return
Temps passé : {timeSpend()}
{errorLabel()} @@ -134,6 +145,11 @@ const ToDoTimeTrackers: FC<{toDo: toDo}> = ({toDo: toDo}) => { + + + )} diff --git a/tests/Feature/TimeTrackerTest.php b/tests/Feature/TimeTrackerTest.php index d9dc41b..7285663 100644 --- a/tests/Feature/TimeTrackerTest.php +++ b/tests/Feature/TimeTrackerTest.php @@ -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); +}); diff --git a/tests/Feature/ToDoTest.php b/tests/Feature/ToDoTest.php index 7dbf1a4..968695d 100644 --- a/tests/Feature/ToDoTest.php +++ b/tests/Feature/ToDoTest.php @@ -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)];