File tree Expand file tree Collapse file tree 6 files changed +98
-57
lines changed
Livewire/Components/Slideovers
tests/Feature/Actions/Forum Expand file tree Collapse file tree 6 files changed +98
-57
lines changed Load Diff This file was deleted.
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change 4
4
5
5
namespace App \Livewire \Components \Slideovers ;
6
6
7
- use App \Actions \Forum \CreateOrUpdateThreadAction ;
7
+ use App \Actions \Forum \CreateThreadAction ;
8
+ use App \Actions \Forum \UpdateThreadAction ;
8
9
use App \Exceptions \UnverifiedUserException ;
9
10
use App \Livewire \Traits \WithAuthenticatedUser ;
10
11
use App \Models \Thread ;
@@ -120,10 +121,9 @@ public function save(): void
120
121
121
122
$ validated = $ this ->form ->getState ();
122
123
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 );
127
127
128
128
$ this ->form ->model ($ thread )->saveRelationships ();
129
129
Original file line number Diff line number Diff line change 4
4
5
5
namespace Tests \Feature \Actions \Forum ;
6
6
7
- use App \Actions \Forum \CreateOrUpdateThreadAction ;
7
+ use App \Actions \Forum \CreateThreadAction ;
8
8
use App \Events \ThreadWasCreated ;
9
9
use App \Models \Channel ;
10
10
use App \Models \Thread ;
21
21
it ('user can create a thread ' , function (): void {
22
22
$ channels = Channel::factory ()->count (2 )->create ();
23
23
24
- $ thread = app (createOrUpdateThreadAction ::class)->execute ([
24
+ $ thread = app (CreateThreadAction ::class)->execute ([
25
25
'title ' => 'thread title ' ,
26
26
'slug ' => 'thread-title ' ,
27
27
'user_id ' => $ this ->user ->id ,
36
36
37
37
Event::assertDispatched (ThreadWasCreated::class);
38
38
});
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
- });
Original file line number Diff line number Diff line change
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
+ });
You can’t perform that action at this time.
0 commit comments