Skip to content

Commit

Permalink
action test
Browse files Browse the repository at this point in the history
  • Loading branch information
iamgergo committed Jan 25, 2024
1 parent f14eb70 commit 52e6707
Show file tree
Hide file tree
Showing 3 changed files with 153 additions and 2 deletions.
17 changes: 15 additions & 2 deletions src/Actions/Action.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Cone\Root\Actions;

use Cone\Root\Exceptions\QueryResolutionException;
use Cone\Root\Fields\Field;
use Cone\Root\Http\Controllers\ActionController;
use Cone\Root\Http\Middleware\Authorize;
Expand Down Expand Up @@ -103,6 +104,18 @@ public function setQuery(Builder $query): static
return $this;
}

/**
* Get the Eloquent query.
*/
public function getQuery(): Builder
{
if (is_null($this->query)) {
throw new QueryResolutionException();
}

return $this->query;
}

/**
* Handle the callback for the field resolution.
*/
Expand Down Expand Up @@ -155,11 +168,11 @@ public function isConfirmable(): bool
*/
public function perform(Request $request): Response
{
$this->validateFormRequest($request, $this->query->getModel());
$this->validateFormRequest($request, $this->getQuery()->getModel());

$this->handle(
$request,
$request->boolean('all') ? $this->query->get() : $this->query->findMany($request->input('models', []))
$request->boolean('all') ? $this->getQuery()->get() : $this->getQuery()->findMany($request->input('models', []))
);

return Redirect::back()->with(
Expand Down
120 changes: 120 additions & 0 deletions tests/Actions/ActionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
<?php

namespace Cone\Root\Tests\Actions;

use Cone\Root\Fields\Text;
use Cone\Root\Tests\SendPasswordResetNotification;
use Cone\Root\Tests\TestCase;
use Cone\Root\Tests\User;

class ActionTest extends TestCase
{
protected SendPasswordResetNotification $action;

public function setUp(): void
{
parent::setUp();

$this->action = new SendPasswordResetNotification();

$this->action->setQuery(User::query());
}

public function test_an_action_has_key(): void
{
$this->assertSame('send-password-reset-notification', $this->action->getKey());
}

public function test_an_action_has_name(): void
{
$this->assertSame('Send Password Reset Notification', $this->action->getName());
}

public function test_an_action_can_be_destructive(): void
{
$this->assertFalse($this->action->isDestructive());

$this->action->destructive();

$this->assertTrue($this->action->isDestructive());

$this->action->destructive(false);

$this->assertFalse($this->action->isDestructive());
}

public function test_an_action_can_be_confirmable(): void
{
$this->assertFalse($this->action->isConfirmable());

$this->action->confirmable();

$this->assertTrue($this->action->isConfirmable());

$this->action->confirmable(false);

$this->assertFalse($this->action->isConfirmable());
}

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->assertArrayHasKey(
trim($this->action->getUri(), '/'),
$this->app['router']->getRoutes()->get('POST')
);
}

public function test_an_action_resolves_fields(): void
{
$this->action->withFields(function () {
return [
Text::make(__('Name')),
];
});

$this->assertTrue(
$this->action->resolveFields($this->app['request'])->isNotEmpty()
);
}

public function test_an_action_has_array_representation(): void
{
$this->assertSame([
'confirmable' => $this->action->isConfirmable(),
'destructive' => $this->action->isDestructive(),
'key' => $this->action->getKey(),
'modalKey' => 'action-send-password-reset-notification',
'name' => $this->action->getName(),
'template' => 'root::actions.action',
'url' => $this->action->getUri(),
], $this->action->toArray());
}

public function test_an_action_has_form_representation(): void
{
$model = new User();

$fields = $this->action
->resolveFields($this->app['request'])
->mapToInputs($this->app['request'], $model);

$this->assertSame(array_merge($this->action->toArray(), [
'open' => false,
'fields' => [],
]), $this->action->toForm($this->app['request'], $model));
}

public function test_an_action_has_response_representation(): void
{
$response = $this->createTestResponse($this->action->perform($this->app['request']));

$response->assertRedirect()
->assertSessionHas(sprintf('alerts.action-%s', $this->action->getKey()));
}
}
18 changes: 18 additions & 0 deletions tests/SendPasswordResetNotification.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Cone\Root\Tests;

use Cone\Root\Actions\Action;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Http\Request;

class SendPasswordResetNotification extends Action
{
/**
* Handle the action.
*/
public function handle(Request $request, Collection $models): void
{
//
}
}

0 comments on commit 52e6707

Please sign in to comment.