Skip to content

Commit 0d947c0

Browse files
committed
generic actions test
1 parent 9c8d873 commit 0d947c0

File tree

5 files changed

+60
-12
lines changed

5 files changed

+60
-12
lines changed

src/Actions/Action.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,7 @@ public function toArray(): array
298298
'key' => $this->getKey(),
299299
'modalKey' => $this->getModalKey(),
300300
'name' => $this->getName(),
301+
'standalone' => $this->isStandalone(),
301302
'template' => $this->template,
302303
];
303304
}

tests/Actions/ActionTest.php

Lines changed: 47 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,35 @@
22

33
namespace Cone\Root\Tests\Actions;
44

5+
use Cone\Root\Exceptions\QueryResolutionException;
6+
use Cone\Root\Exceptions\SaveFormDataException;
57
use Cone\Root\Fields\Text;
68
use Cone\Root\Tests\TestCase;
79
use Cone\Root\Tests\User;
810

911
class 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
}

tests/Actions/SendPasswordResetNotification.php renamed to tests/Actions/SendNotification.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,8 @@
66
use Illuminate\Database\Eloquent\Collection;
77
use Illuminate\Http\Request;
88

9-
class SendPasswordResetNotification extends Action
9+
class SendNotification extends Action
1010
{
11-
/**
12-
* Handle the action.
13-
*/
1411
public function handle(Request $request, Collection $models): void
1512
{
1613
//
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Cone\Root\Tests\Actions;
4+
5+
use Cone\Root\Tests\TestCase;
6+
7+
class SendPasswordResetNotificationTest extends TestCase
8+
{
9+
//
10+
}

tests/Resources/UserResource.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Cone\Root\Tests\Resources;
44

5+
use Cone\Root\Actions\SendPasswordResetNotification;
56
use Cone\Root\Fields\BelongsToMany;
67
use Cone\Root\Fields\Email;
78
use Cone\Root\Fields\HasMany;
@@ -12,7 +13,6 @@
1213
use Cone\Root\Fields\Select;
1314
use Cone\Root\Fields\Text;
1415
use Cone\Root\Resources\Resource;
15-
use Cone\Root\Tests\Actions\SendPasswordResetNotification;
1616
use Cone\Root\Tests\Team;
1717
use Cone\Root\Tests\User;
1818
use Cone\Root\Tests\Widgets\UsersCount;

0 commit comments

Comments
 (0)