Skip to content

Commit bd15e39

Browse files
feat:[lar-137] refactor to create Created and Updated action Thread with feature test
1 parent 8fe19d6 commit bd15e39

File tree

6 files changed

+98
-57
lines changed

6 files changed

+98
-57
lines changed

app/Actions/Forum/CreateOrUpdateThreadAction.php

Lines changed: 0 additions & 31 deletions
This file was deleted.
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: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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, int $threadId): Thread
13+
{
14+
return DB::transaction(function () use ($formValues, $threadId) {
15+
16+
$thread = Thread::query()->findOrFail($threadId);
17+
18+
$thread->update($formValues);
19+
20+
return $thread;
21+
});
22+
23+
}
24+
}

app/Livewire/Components/Slideovers/ThreadForm.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44

55
namespace App\Livewire\Components\Slideovers;
66

7-
use App\Actions\Forum\CreateOrUpdateThreadAction;
7+
use App\Actions\Forum\CreateThreadAction;
8+
use App\Actions\Forum\UpdateThreadAction;
89
use App\Exceptions\UnverifiedUserException;
910
use App\Livewire\Traits\WithAuthenticatedUser;
1011
use App\Models\Thread;
@@ -120,10 +121,9 @@ public function save(): void
120121

121122
$validated = $this->form->getState();
122123

123-
$thread = app(CreateOrUpdateThreadAction::class)->execute(
124-
formValues: $validated,
125-
threadId: $this->thread?->id
126-
);
124+
$thread = ($this->thread?->id)
125+
? app(UpdateThreadAction::class)->execute($validated, $this->thread->id)
126+
: app(CreateThreadAction::class)->execute($validated);
127127

128128
$this->form->model($thread)->saveRelationships();
129129

tests/Feature/Actions/Forum/CreateOrUpdateThreadActionTest.php renamed to tests/Feature/Actions/Forum/CreateThreadActionTest.php

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace Tests\Feature\Actions\Forum;
66

7-
use App\Actions\Forum\CreateOrUpdateThreadAction;
7+
use App\Actions\Forum\CreateThreadAction;
88
use App\Events\ThreadWasCreated;
99
use App\Models\Channel;
1010
use App\Models\Thread;
@@ -21,7 +21,7 @@
2121
it('user can create a thread', function (): void {
2222
$channels = Channel::factory()->count(2)->create();
2323

24-
$thread = app(createOrUpdateThreadAction::class)->execute([
24+
$thread = app(CreateThreadAction::class)->execute([
2525
'title' => 'thread title',
2626
'slug' => 'thread-title',
2727
'user_id' => $this->user->id,
@@ -36,22 +36,3 @@
3636

3737
Event::assertDispatched(ThreadWasCreated::class);
3838
});
39-
40-
it('user can edit a thread', function (): void {
41-
$thread = Thread::factory()->create(['user_id' => $this->user->id]);
42-
$channels = Channel::factory()->count(3)->create();
43-
44-
$thread->channels()->attach($channels->modelKeys());
45-
46-
$thread = app(createOrUpdateThreadAction::class)->execute([
47-
'title' => 'update thread title',
48-
], $thread->id);
49-
50-
expect($thread)
51-
->toBeInstanceOf(Thread::class)
52-
->and($thread->title)
53-
->toBe('update thread title');
54-
55-
Event::assertNotDispatched(ThreadWasCreated::class);
56-
57-
});
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->id);
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)