Skip to content

Commit

Permalink
generic actions test
Browse files Browse the repository at this point in the history
  • Loading branch information
iamgergo committed Nov 13, 2024
1 parent 9c8d873 commit 0d947c0
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 12 deletions.
1 change: 1 addition & 0 deletions src/Actions/Action.php
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,7 @@ public function toArray(): array
'key' => $this->getKey(),
'modalKey' => $this->getModalKey(),
'name' => $this->getName(),
'standalone' => $this->isStandalone(),
'template' => $this->template,
];
}
Expand Down
54 changes: 47 additions & 7 deletions tests/Actions/ActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -55,20 +59,40 @@ 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(), '/'),
$this->app['router']->getRoutes()->get('POST')
);
}

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 () {
Expand All @@ -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());
}
Expand All @@ -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']
Expand All @@ -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']
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
//
Expand Down
10 changes: 10 additions & 0 deletions tests/Actions/SendPasswordResetNotificationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Cone\Root\Tests\Actions;

use Cone\Root\Tests\TestCase;

class SendPasswordResetNotificationTest extends TestCase
{
//
}
2 changes: 1 addition & 1 deletion tests/Resources/UserResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Cone\Root\Tests\Resources;

use Cone\Root\Actions\SendPasswordResetNotification;
use Cone\Root\Fields\BelongsToMany;
use Cone\Root\Fields\Email;
use Cone\Root\Fields\HasMany;
Expand All @@ -12,7 +13,6 @@
use Cone\Root\Fields\Select;
use Cone\Root\Fields\Text;
use Cone\Root\Resources\Resource;
use Cone\Root\Tests\Actions\SendPasswordResetNotification;
use Cone\Root\Tests\Team;
use Cone\Root\Tests\User;
use Cone\Root\Tests\Widgets\UsersCount;
Expand Down

0 comments on commit 0d947c0

Please sign in to comment.