From 531b2990d68ae8c54e6259391cf62dc7c8338971 Mon Sep 17 00:00:00 2001 From: Chri$ Date: Thu, 19 Dec 2024 06:10:54 +0100 Subject: [PATCH 1/2] feat:[LAR-134] Refactor edit and create thread to action and write test --- app/Actions/Forum/CreateThreadAction.php | 29 ++++++++++++++ app/Actions/Forum/UpdateThreadAction.php | 24 ++++++++++++ .../Components/Slideovers/ThreadForm.php | 22 ++++------- .../Actions/Forum/CreateThreadActionTest.php | 38 +++++++++++++++++++ .../Actions/Forum/UpdateThreadActionTest.php | 38 +++++++++++++++++++ 5 files changed, 136 insertions(+), 15 deletions(-) create mode 100644 app/Actions/Forum/CreateThreadAction.php create mode 100644 app/Actions/Forum/UpdateThreadAction.php create mode 100644 tests/Feature/Actions/Forum/CreateThreadActionTest.php create mode 100644 tests/Feature/Actions/Forum/UpdateThreadActionTest.php diff --git a/app/Actions/Forum/CreateThreadAction.php b/app/Actions/Forum/CreateThreadAction.php new file mode 100644 index 00000000..45f97686 --- /dev/null +++ b/app/Actions/Forum/CreateThreadAction.php @@ -0,0 +1,29 @@ +create($formValues); + + app(SubscribeToThreadAction::class)->execute($thread); + + givePoint(new ThreadCreated($thread)); + + event(new ThreadWasCreated($thread)); + + return $thread; + }); + + } +} diff --git a/app/Actions/Forum/UpdateThreadAction.php b/app/Actions/Forum/UpdateThreadAction.php new file mode 100644 index 00000000..25e0046f --- /dev/null +++ b/app/Actions/Forum/UpdateThreadAction.php @@ -0,0 +1,24 @@ +findOrFail($threadId); + + $thread->update($formValues); + + return $thread; + }); + + } +} diff --git a/app/Livewire/Components/Slideovers/ThreadForm.php b/app/Livewire/Components/Slideovers/ThreadForm.php index 9b1073bb..e7d38545 100644 --- a/app/Livewire/Components/Slideovers/ThreadForm.php +++ b/app/Livewire/Components/Slideovers/ThreadForm.php @@ -4,10 +4,9 @@ namespace App\Livewire\Components\Slideovers; -use App\Actions\Forum\SubscribeToThreadAction; -use App\Events\ThreadWasCreated; +use App\Actions\Forum\CreateThreadAction; +use App\Actions\Forum\UpdateThreadAction; use App\Exceptions\UnverifiedUserException; -use App\Gamify\Points\ThreadCreated; use App\Livewire\Traits\WithAuthenticatedUser; use App\Models\Thread; use Filament\Forms; @@ -122,18 +121,11 @@ public function save(): void $validated = $this->form->getState(); - if ($this->thread?->id) { - $this->thread->update($validated); - $this->form->model($this->thread)->saveRelationships(); - } else { - $thread = Thread::query()->create($validated); - $this->form->model($thread)->saveRelationships(); - - app(SubscribeToThreadAction::class)->execute($thread); + $thread = ($this->thread?->id) + ? app(UpdateThreadAction::class)->execute($validated, $this->thread->id) + : app(CreateThreadAction::class)->execute($validated); - givePoint(new ThreadCreated($thread)); - event(new ThreadWasCreated($thread)); - } + $this->form->model($thread)->saveRelationships(); Notification::make() ->title( @@ -144,7 +136,7 @@ public function save(): void ->success() ->send(); - $this->redirect(route('forum.show', ['thread' => $thread ?? $this->thread]), navigate: true); + $this->redirect(route('forum.show', ['thread' => $thread]), navigate: true); } public function render(): View diff --git a/tests/Feature/Actions/Forum/CreateThreadActionTest.php b/tests/Feature/Actions/Forum/CreateThreadActionTest.php new file mode 100644 index 00000000..0729c460 --- /dev/null +++ b/tests/Feature/Actions/Forum/CreateThreadActionTest.php @@ -0,0 +1,38 @@ +user = $this->login(); + + Event::fake(); + Notification::fake(); +}); + +it('user can create a thread', function (): void { + $channels = Channel::factory()->count(2)->create(); + + $thread = app(CreateThreadAction::class)->execute([ + 'title' => 'thread title', + 'slug' => 'thread-title', + 'user_id' => $this->user->id, + 'body' => 'This is a test action thread for created or updated thread.', + 'channels' => [$channels->first(), $channels->last()], + ]); + + expect($thread) + ->toBeInstanceOf(Thread::class) + ->and($thread->user_id) + ->toBe($this->user->id); + + Event::assertDispatched(ThreadWasCreated::class); +}); diff --git a/tests/Feature/Actions/Forum/UpdateThreadActionTest.php b/tests/Feature/Actions/Forum/UpdateThreadActionTest.php new file mode 100644 index 00000000..92baf9f3 --- /dev/null +++ b/tests/Feature/Actions/Forum/UpdateThreadActionTest.php @@ -0,0 +1,38 @@ +user = $this->login(); + + Event::fake(); + Notification::fake(); +}); + +it('user can edit a thread', function (): void { + $thread = Thread::factory()->create(['user_id' => $this->user->id]); + $channels = Channel::factory()->count(3)->create(); + + $thread->channels()->attach($channels->modelKeys()); + + $thread = app(UpdateThreadAction::class)->execute([ + 'title' => 'update thread title', + ], $thread->id); + + expect($thread) + ->toBeInstanceOf(Thread::class) + ->and($thread->title) + ->toBe('update thread title'); + + Event::assertNotDispatched(ThreadWasCreated::class); + +}); From df904e004d14a79689409b840050bc9b678f71e3 Mon Sep 17 00:00:00 2001 From: Chri$ Date: Thu, 19 Dec 2024 18:01:17 +0100 Subject: [PATCH 2/2] feat:[LAR-134] Make create and Edit Thread in Action --- app/Actions/Forum/UpdateThreadAction.php | 6 ++---- app/Livewire/Components/Slideovers/ThreadForm.php | 2 +- tests/Feature/Actions/Forum/CreateThreadActionTest.php | 2 +- tests/Feature/Actions/Forum/UpdateThreadActionTest.php | 2 +- 4 files changed, 5 insertions(+), 7 deletions(-) diff --git a/app/Actions/Forum/UpdateThreadAction.php b/app/Actions/Forum/UpdateThreadAction.php index 25e0046f..6068380e 100644 --- a/app/Actions/Forum/UpdateThreadAction.php +++ b/app/Actions/Forum/UpdateThreadAction.php @@ -9,11 +9,9 @@ final class UpdateThreadAction { - public function execute(array $formValues, int $threadId): Thread + public function execute(array $formValues, Thread $thread): Thread { - return DB::transaction(function () use ($formValues, $threadId) { - - $thread = Thread::query()->findOrFail($threadId); + return DB::transaction(function () use ($formValues, $thread) { $thread->update($formValues); diff --git a/app/Livewire/Components/Slideovers/ThreadForm.php b/app/Livewire/Components/Slideovers/ThreadForm.php index e7d38545..94429dab 100644 --- a/app/Livewire/Components/Slideovers/ThreadForm.php +++ b/app/Livewire/Components/Slideovers/ThreadForm.php @@ -122,7 +122,7 @@ public function save(): void $validated = $this->form->getState(); $thread = ($this->thread?->id) - ? app(UpdateThreadAction::class)->execute($validated, $this->thread->id) + ? app(UpdateThreadAction::class)->execute($validated, $this->thread) : app(CreateThreadAction::class)->execute($validated); $this->form->model($thread)->saveRelationships(); diff --git a/tests/Feature/Actions/Forum/CreateThreadActionTest.php b/tests/Feature/Actions/Forum/CreateThreadActionTest.php index 0729c460..73b09207 100644 --- a/tests/Feature/Actions/Forum/CreateThreadActionTest.php +++ b/tests/Feature/Actions/Forum/CreateThreadActionTest.php @@ -26,7 +26,7 @@ 'slug' => 'thread-title', 'user_id' => $this->user->id, 'body' => 'This is a test action thread for created or updated thread.', - 'channels' => [$channels->first(), $channels->last()], + 'channels' => [$channels->modelKeys()], ]); expect($thread) diff --git a/tests/Feature/Actions/Forum/UpdateThreadActionTest.php b/tests/Feature/Actions/Forum/UpdateThreadActionTest.php index 92baf9f3..94dbeaa4 100644 --- a/tests/Feature/Actions/Forum/UpdateThreadActionTest.php +++ b/tests/Feature/Actions/Forum/UpdateThreadActionTest.php @@ -26,7 +26,7 @@ $thread = app(UpdateThreadAction::class)->execute([ 'title' => 'update thread title', - ], $thread->id); + ], $thread); expect($thread) ->toBeInstanceOf(Thread::class)