Skip to content

Commit 2c23bc0

Browse files
feat:[LAR-134] Refactor edit and create thread to action and write test (#258)
2 parents ebe5194 + df904e0 commit 2c23bc0

File tree

5 files changed

+134
-15
lines changed

5 files changed

+134
-15
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Actions\Forum;
6+
7+
use App\Events\ThreadWasCreated;
8+
use App\Gamify\Points\ThreadCreated;
9+
use App\Models\Thread;
10+
use Illuminate\Support\Facades\DB;
11+
12+
final class CreateThreadAction
13+
{
14+
public function execute(array $formValues): Thread
15+
{
16+
return DB::transaction(function () use ($formValues) {
17+
$thread = Thread::query()->create($formValues);
18+
19+
app(SubscribeToThreadAction::class)->execute($thread);
20+
21+
givePoint(new ThreadCreated($thread));
22+
23+
event(new ThreadWasCreated($thread));
24+
25+
return $thread;
26+
});
27+
28+
}
29+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Actions\Forum;
6+
7+
use App\Models\Thread;
8+
use Illuminate\Support\Facades\DB;
9+
10+
final class UpdateThreadAction
11+
{
12+
public function execute(array $formValues, Thread $thread): Thread
13+
{
14+
return DB::transaction(function () use ($formValues, $thread) {
15+
16+
$thread->update($formValues);
17+
18+
return $thread;
19+
});
20+
21+
}
22+
}

app/Livewire/Components/Slideovers/ThreadForm.php

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@
44

55
namespace App\Livewire\Components\Slideovers;
66

7-
use App\Actions\Forum\SubscribeToThreadAction;
8-
use App\Events\ThreadWasCreated;
7+
use App\Actions\Forum\CreateThreadAction;
8+
use App\Actions\Forum\UpdateThreadAction;
99
use App\Exceptions\UnverifiedUserException;
10-
use App\Gamify\Points\ThreadCreated;
1110
use App\Livewire\Traits\WithAuthenticatedUser;
1211
use App\Models\Thread;
1312
use Filament\Forms;
@@ -122,18 +121,11 @@ public function save(): void
122121

123122
$validated = $this->form->getState();
124123

125-
if ($this->thread?->id) {
126-
$this->thread->update($validated);
127-
$this->form->model($this->thread)->saveRelationships();
128-
} else {
129-
$thread = Thread::query()->create($validated);
130-
$this->form->model($thread)->saveRelationships();
131-
132-
app(SubscribeToThreadAction::class)->execute($thread);
124+
$thread = ($this->thread?->id)
125+
? app(UpdateThreadAction::class)->execute($validated, $this->thread)
126+
: app(CreateThreadAction::class)->execute($validated);
133127

134-
givePoint(new ThreadCreated($thread));
135-
event(new ThreadWasCreated($thread));
136-
}
128+
$this->form->model($thread)->saveRelationships();
137129

138130
Notification::make()
139131
->title(
@@ -144,7 +136,7 @@ public function save(): void
144136
->success()
145137
->send();
146138

147-
$this->redirect(route('forum.show', ['thread' => $thread ?? $this->thread]), navigate: true);
139+
$this->redirect(route('forum.show', ['thread' => $thread]), navigate: true);
148140
}
149141

150142
public function render(): View
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests\Feature\Actions\Forum;
6+
7+
use App\Actions\Forum\CreateThreadAction;
8+
use App\Events\ThreadWasCreated;
9+
use App\Models\Channel;
10+
use App\Models\Thread;
11+
use Illuminate\Support\Facades\Event;
12+
use Illuminate\Support\Facades\Notification;
13+
14+
beforeEach(function (): void {
15+
$this->user = $this->login();
16+
17+
Event::fake();
18+
Notification::fake();
19+
});
20+
21+
it('user can create a thread', function (): void {
22+
$channels = Channel::factory()->count(2)->create();
23+
24+
$thread = app(CreateThreadAction::class)->execute([
25+
'title' => 'thread title',
26+
'slug' => 'thread-title',
27+
'user_id' => $this->user->id,
28+
'body' => 'This is a test action thread for created or updated thread.',
29+
'channels' => [$channels->modelKeys()],
30+
]);
31+
32+
expect($thread)
33+
->toBeInstanceOf(Thread::class)
34+
->and($thread->user_id)
35+
->toBe($this->user->id);
36+
37+
Event::assertDispatched(ThreadWasCreated::class);
38+
});
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests\Feature\Actions\Forum;
6+
7+
use App\Actions\Forum\UpdateThreadAction;
8+
use App\Events\ThreadWasCreated;
9+
use App\Models\Channel;
10+
use App\Models\Thread;
11+
use Illuminate\Support\Facades\Event;
12+
use Illuminate\Support\Facades\Notification;
13+
14+
beforeEach(function (): void {
15+
$this->user = $this->login();
16+
17+
Event::fake();
18+
Notification::fake();
19+
});
20+
21+
it('user can edit a thread', function (): void {
22+
$thread = Thread::factory()->create(['user_id' => $this->user->id]);
23+
$channels = Channel::factory()->count(3)->create();
24+
25+
$thread->channels()->attach($channels->modelKeys());
26+
27+
$thread = app(UpdateThreadAction::class)->execute([
28+
'title' => 'update thread title',
29+
], $thread);
30+
31+
expect($thread)
32+
->toBeInstanceOf(Thread::class)
33+
->and($thread->title)
34+
->toBe('update thread title');
35+
36+
Event::assertNotDispatched(ThreadWasCreated::class);
37+
38+
});

0 commit comments

Comments
 (0)