22
33namespace Cone \Root \Tests \Actions ;
44
5+ use Cone \Root \Exceptions \QueryResolutionException ;
6+ use Cone \Root \Exceptions \SaveFormDataException ;
57use Cone \Root \Fields \Text ;
68use Cone \Root \Tests \TestCase ;
79use Cone \Root \Tests \User ;
810
911class ActionTest extends TestCase
1012{
11- protected SendPasswordResetNotification $ action ;
13+ protected SendNotification $ action ;
14+
15+ protected User $ user ;
1216
1317 public function setUp (): void
1418 {
1519 parent ::setUp ();
1620
17- $ this ->action = new SendPasswordResetNotification ;
21+ $ this ->user = User:: factory ()-> create () ;
1822
19- $ this ->action -> withQuery ( fn () => User:: query ()) ;
23+ $ this ->action = new SendNotification ;
2024 }
2125
2226 public function test_an_action_has_key (): void
2327 {
24- $ this ->assertSame ('send-password-reset- notification ' , $ this ->action ->getKey ());
28+ $ this ->assertSame ('send-notification ' , $ this ->action ->getKey ());
2529 }
2630
2731 public function test_an_action_has_name (): void
2832 {
29- $ this ->assertSame ('Send Password Reset Notification ' , $ this ->action ->getName ());
33+ $ this ->assertSame ('Send Notification ' , $ this ->action ->getName ());
3034 }
3135
3236 public function test_an_action_can_be_destructive (): void
@@ -55,20 +59,40 @@ public function test_an_action_can_be_confirmable(): void
5559 $ this ->assertFalse ($ this ->action ->isConfirmable ());
5660 }
5761
62+ public function test_an_action_can_be_standalone (): void
63+ {
64+ $ this ->assertFalse ($ this ->action ->isStandalone ());
65+
66+ $ this ->action ->standalone ();
67+
68+ $ this ->assertTrue ($ this ->action ->isStandalone ());
69+
70+ $ this ->action ->standalone (false );
71+
72+ $ this ->assertFalse ($ this ->action ->isStandalone ());
73+ }
74+
5875 public function test_an_action_registers_routes (): void
5976 {
6077 $ this ->app ['router ' ]->prefix ('users/actions ' )->group (function ($ router ) {
6178 $ this ->action ->registerRoutes ($ this ->app ['request ' ], $ router );
6279 });
6380
64- $ this ->assertSame ('/users/actions/send-password-reset- notification ' , $ this ->action ->getUri ());
81+ $ this ->assertSame ('/users/actions/send-notification ' , $ this ->action ->getUri ());
6582
6683 $ this ->assertArrayHasKey (
6784 trim ($ this ->action ->getUri (), '/ ' ),
6885 $ this ->app ['router ' ]->getRoutes ()->get ('POST ' )
6986 );
7087 }
7188
89+ public function test_an_action_resolves_query (): void
90+ {
91+ $ this ->expectException (QueryResolutionException::class);
92+
93+ $ this ->action ->resolveQuery ($ this ->app ['request ' ]);
94+ }
95+
7296 public function test_an_action_resolves_fields (): void
7397 {
7498 $ this ->action ->withFields (function () {
@@ -88,8 +112,9 @@ public function test_an_action_has_array_representation(): void
88112 'confirmable ' => $ this ->action ->isConfirmable (),
89113 'destructive ' => $ this ->action ->isDestructive (),
90114 'key ' => $ this ->action ->getKey (),
91- 'modalKey ' => 'action-send-password-reset- notification ' ,
115+ 'modalKey ' => 'action-send-notification ' ,
92116 'name ' => $ this ->action ->getName (),
117+ 'standalone ' => false ,
93118 'template ' => 'root::actions.action ' ,
94119 ], $ this ->action ->toArray ());
95120 }
@@ -107,6 +132,11 @@ public function test_an_action_has_form_representation(): void
107132
108133 public function test_an_action_has_response_representation (): void
109134 {
135+ $ this ->action ->withQuery (fn () => User::query ());
136+
137+ $ this ->app ['request ' ]->merge (['models ' => [$ this ->user ->getKey ()]]);
138+ $ this ->app ['request ' ]->setUserResolver (fn () => $ this ->user );
139+
110140 $ response = $ this ->createTestResponse (
111141 $ this ->action ->perform ($ this ->app ['request ' ]),
112142 $ this ->app ['request ' ]
@@ -115,4 +145,14 @@ public function test_an_action_has_response_representation(): void
115145 $ response ->assertRedirect ()
116146 ->assertSessionHas (sprintf ('alerts.action-%s ' , $ this ->action ->getKey ()));
117147 }
148+
149+ public function test_an_action_handles_exceptions_on_perform (): void
150+ {
151+ $ this ->expectException (SaveFormDataException::class);
152+
153+ $ this ->createTestResponse (
154+ $ this ->action ->perform ($ this ->app ['request ' ]),
155+ $ this ->app ['request ' ]
156+ );
157+ }
118158}
0 commit comments