diff --git a/src/Actions/Action.php b/src/Actions/Action.php index 11e01088..8ef5de5e 100644 --- a/src/Actions/Action.php +++ b/src/Actions/Action.php @@ -298,6 +298,7 @@ public function toArray(): array 'key' => $this->getKey(), 'modalKey' => $this->getModalKey(), 'name' => $this->getName(), + 'standalone' => $this->isStandalone(), 'template' => $this->template, ]; } diff --git a/tests/Actions/ActionTest.php b/tests/Actions/ActionTest.php index 84bdf07c..227167cb 100644 --- a/tests/Actions/ActionTest.php +++ b/tests/Actions/ActionTest.php @@ -2,31 +2,35 @@ namespace Cone\Root\Tests\Actions; +use Cone\Root\Exceptions\QueryResolutionException; +use Cone\Root\Exceptions\SaveFormDataException; use Cone\Root\Fields\Text; use Cone\Root\Tests\TestCase; use Cone\Root\Tests\User; class ActionTest extends TestCase { - protected SendPasswordResetNotification $action; + protected SendNotification $action; + + protected User $user; public function setUp(): void { parent::setUp(); - $this->action = new SendPasswordResetNotification; + $this->user = User::factory()->create(); - $this->action->withQuery(fn () => User::query()); + $this->action = new SendNotification; } public function test_an_action_has_key(): void { - $this->assertSame('send-password-reset-notification', $this->action->getKey()); + $this->assertSame('send-notification', $this->action->getKey()); } public function test_an_action_has_name(): void { - $this->assertSame('Send Password Reset Notification', $this->action->getName()); + $this->assertSame('Send Notification', $this->action->getName()); } public function test_an_action_can_be_destructive(): void @@ -55,13 +59,26 @@ public function test_an_action_can_be_confirmable(): void $this->assertFalse($this->action->isConfirmable()); } + public function test_an_action_can_be_standalone(): void + { + $this->assertFalse($this->action->isStandalone()); + + $this->action->standalone(); + + $this->assertTrue($this->action->isStandalone()); + + $this->action->standalone(false); + + $this->assertFalse($this->action->isStandalone()); + } + public function test_an_action_registers_routes(): void { $this->app['router']->prefix('users/actions')->group(function ($router) { $this->action->registerRoutes($this->app['request'], $router); }); - $this->assertSame('/users/actions/send-password-reset-notification', $this->action->getUri()); + $this->assertSame('/users/actions/send-notification', $this->action->getUri()); $this->assertArrayHasKey( trim($this->action->getUri(), '/'), @@ -69,6 +86,13 @@ public function test_an_action_registers_routes(): void ); } + public function test_an_action_resolves_query(): void + { + $this->expectException(QueryResolutionException::class); + + $this->action->resolveQuery($this->app['request']); + } + public function test_an_action_resolves_fields(): void { $this->action->withFields(function () { @@ -88,8 +112,9 @@ public function test_an_action_has_array_representation(): void 'confirmable' => $this->action->isConfirmable(), 'destructive' => $this->action->isDestructive(), 'key' => $this->action->getKey(), - 'modalKey' => 'action-send-password-reset-notification', + 'modalKey' => 'action-send-notification', 'name' => $this->action->getName(), + 'standalone' => false, 'template' => 'root::actions.action', ], $this->action->toArray()); } @@ -107,6 +132,11 @@ public function test_an_action_has_form_representation(): void public function test_an_action_has_response_representation(): void { + $this->action->withQuery(fn () => User::query()); + + $this->app['request']->merge(['models' => [$this->user->getKey()]]); + $this->app['request']->setUserResolver(fn () => $this->user); + $response = $this->createTestResponse( $this->action->perform($this->app['request']), $this->app['request'] @@ -115,4 +145,14 @@ public function test_an_action_has_response_representation(): void $response->assertRedirect() ->assertSessionHas(sprintf('alerts.action-%s', $this->action->getKey())); } + + public function test_an_action_handles_exceptions_on_perform(): void + { + $this->expectException(SaveFormDataException::class); + + $this->createTestResponse( + $this->action->perform($this->app['request']), + $this->app['request'] + ); + } } diff --git a/tests/Actions/SendPasswordResetNotification.php b/tests/Actions/SendNotification.php similarity index 72% rename from tests/Actions/SendPasswordResetNotification.php rename to tests/Actions/SendNotification.php index 9dcfbb9b..51948d6c 100644 --- a/tests/Actions/SendPasswordResetNotification.php +++ b/tests/Actions/SendNotification.php @@ -6,11 +6,8 @@ use Illuminate\Database\Eloquent\Collection; use Illuminate\Http\Request; -class SendPasswordResetNotification extends Action +class SendNotification extends Action { - /** - * Handle the action. - */ public function handle(Request $request, Collection $models): void { // diff --git a/tests/Actions/SendPasswordResetNotificationTest.php b/tests/Actions/SendPasswordResetNotificationTest.php new file mode 100644 index 00000000..cd912062 --- /dev/null +++ b/tests/Actions/SendPasswordResetNotificationTest.php @@ -0,0 +1,10 @@ +