Skip to content

Commit 3a72f59

Browse files
committed
Tests for actions
1 parent 2887b91 commit 3a72f59

File tree

4 files changed

+45
-3
lines changed

4 files changed

+45
-3
lines changed

src/Actions/Action.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,13 @@ public function payload(): array
9292

9393
public function handleRequest(ActionRequest $request)
9494
{
95-
if (! method_exists($this, 'handle')) {
95+
if (!method_exists($this, 'handle')) {
9696
throw new Exception('Missing handle method from the action.');
9797
}
9898

9999
$response = null;
100100

101-
if (! $request->isForRepositoryRequest()) {
101+
if (!$request->isForRepositoryRequest()) {
102102
$request->collectRepositories($this, static::$chunkCount, function ($models) use ($request, &$response) {
103103
Transaction::run(function () use ($models, $request, &$response) {
104104
$response = $this->handle($request, $models);
@@ -108,7 +108,9 @@ public function handleRequest(ActionRequest $request)
108108
Transaction::run(function () use ($request, &$response) {
109109
$response = $this->handle(
110110
$request,
111-
$request->findModelOrFail()
111+
tap($request->findModelQuery(), function ($query) use ($request) {
112+
static::indexQuery($request, $query);
113+
})->firstOrFail()
112114
);
113115
});
114116
}

tests/Actions/PerformActionsControllerTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Binaryk\LaravelRestify\Tests\Actions;
44

55
use Binaryk\LaravelRestify\Tests\Fixtures\Post\PublishPostAction;
6+
use Binaryk\LaravelRestify\Tests\Fixtures\User\ActivateAction;
67
use Binaryk\LaravelRestify\Tests\IntegrationTest;
78

89
class PerformActionsControllerTest extends IntegrationTest
@@ -50,4 +51,17 @@ public function test_cannot_apply_a_show_action_to_index()
5051
'errors',
5152
]);
5253
}
54+
55+
public function test_show_action_not_need_repositories()
56+
{
57+
$users = $this->mockUsers();
58+
59+
$this->post('/restify-api/users/'.$users->first()->id.'/action?action='.(new ActivateAction)->uriKey())
60+
->assertSuccessful()
61+
->assertJsonStructure([
62+
'data',
63+
]);
64+
65+
$this->assertEquals(1, ActivateAction::$applied[0]->id);
66+
}
5367
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace Binaryk\LaravelRestify\Tests\Fixtures\User;
4+
5+
use Binaryk\LaravelRestify\Actions\Action;
6+
use Binaryk\LaravelRestify\Http\Requests\ActionRequest;
7+
use Illuminate\Http\JsonResponse;
8+
9+
class ActivateAction extends Action
10+
{
11+
public static $applied = [];
12+
13+
public function handle(ActionRequest $request, User $user): JsonResponse
14+
{
15+
static::$applied[] = $user;
16+
17+
return $this->response()->data(['succes' => 'true'])->respond();
18+
}
19+
}

tests/Fixtures/User/UserRepository.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,11 @@ public function fields(RestifyRequest $request)
2222
return [
2323
];
2424
}
25+
26+
public function actions(RestifyRequest $request)
27+
{
28+
return [
29+
ActivateAction::new(),
30+
];
31+
}
2532
}

0 commit comments

Comments
 (0)