Skip to content

feat:[LAR-134] Refactor edit and create thread to action and write test #258

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions app/Actions/Forum/CreateThreadAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace App\Actions\Forum;

use App\Events\ThreadWasCreated;
use App\Gamify\Points\ThreadCreated;
use App\Models\Thread;
use Illuminate\Support\Facades\DB;

final class CreateThreadAction
{
public function execute(array $formValues): Thread
{
return DB::transaction(function () use ($formValues) {
$thread = Thread::query()->create($formValues);

app(SubscribeToThreadAction::class)->execute($thread);

givePoint(new ThreadCreated($thread));

event(new ThreadWasCreated($thread));

return $thread;
});

}
}
22 changes: 22 additions & 0 deletions app/Actions/Forum/UpdateThreadAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace App\Actions\Forum;

use App\Models\Thread;
use Illuminate\Support\Facades\DB;

final class UpdateThreadAction
{
public function execute(array $formValues, Thread $thread): Thread
{
return DB::transaction(function () use ($formValues, $thread) {

$thread->update($formValues);

return $thread;
});

}
}
22 changes: 7 additions & 15 deletions app/Livewire/Components/Slideovers/ThreadForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
: app(CreateThreadAction::class)->execute($validated);

givePoint(new ThreadCreated($thread));
event(new ThreadWasCreated($thread));
}
$this->form->model($thread)->saveRelationships();

Notification::make()
->title(
Expand All @@ -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
Expand Down
38 changes: 38 additions & 0 deletions tests/Feature/Actions/Forum/CreateThreadActionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

namespace Tests\Feature\Actions\Forum;

use App\Actions\Forum\CreateThreadAction;
use App\Events\ThreadWasCreated;
use App\Models\Channel;
use App\Models\Thread;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Notification;

beforeEach(function (): void {
$this->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->modelKeys()],
]);

expect($thread)
->toBeInstanceOf(Thread::class)
->and($thread->user_id)
->toBe($this->user->id);

Event::assertDispatched(ThreadWasCreated::class);
});
38 changes: 38 additions & 0 deletions tests/Feature/Actions/Forum/UpdateThreadActionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

namespace Tests\Feature\Actions\Forum;

use App\Actions\Forum\UpdateThreadAction;
use App\Events\ThreadWasCreated;
use App\Models\Channel;
use App\Models\Thread;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Notification;

beforeEach(function (): void {
$this->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);

expect($thread)
->toBeInstanceOf(Thread::class)
->and($thread->title)
->toBe('update thread title');

Event::assertNotDispatched(ThreadWasCreated::class);

});
Loading