From a1466ee81a820e79ad5390e3841f7d0af21d9408 Mon Sep 17 00:00:00 2001 From: Eduard Lupacescu Date: Sun, 27 Dec 2020 09:20:31 +0200 Subject: [PATCH 1/2] Handle action handle when query chunk is zero. --- src/Actions/Action.php | 2 +- src/Http/Requests/ActionRequest.php | 24 +++++++---- .../Actions/PerformActionsControllerTest.php | 41 +++++++++++++++++-- 3 files changed, 55 insertions(+), 12 deletions(-) diff --git a/src/Actions/Action.php b/src/Actions/Action.php index c6abdc3e7..06ad30b12 100644 --- a/src/Actions/Action.php +++ b/src/Actions/Action.php @@ -44,7 +44,7 @@ abstract class Action extends RestController implements JsonSerializable public static function indexQuery(RestifyRequest $request, $query) { - return $query; + // } /** diff --git a/src/Http/Requests/ActionRequest.php b/src/Http/Requests/ActionRequest.php index 6148eeb0e..15b9406c7 100644 --- a/src/Http/Requests/ActionRequest.php +++ b/src/Http/Requests/ActionRequest.php @@ -5,6 +5,7 @@ use Binaryk\LaravelRestify\Actions\Action; use Binaryk\LaravelRestify\Services\Search\RepositorySearchService; use Closure; +use Illuminate\Database\Eloquent\Builder; use Illuminate\Support\Collection; class ActionRequest extends RestifyRequest @@ -32,17 +33,26 @@ protected function actionExists() }); } + public function builder(Action $action, int $size): Builder + { + return tap(RepositorySearchService::instance()->search($this, $this->repository()), function ($query) use ($action) { + $action::indexQuery($this, $query); + }) + ->when($this->input('repositories') !== 'all', function ($query) { + $query->whereKey($this->input('repositories', [])); + }) + ->latest($this->model()->getKeyName()); + } + public function collectRepositories(Action $action, $count, Closure $callback) { $output = []; - tap(RepositorySearchService::instance()->search($this, $this->repository()), function ($query) use ($action) { - $action::indexQuery($this, $query); - }) - ->when($this->input('repositories') !== 'all', function ($query) { - $query->whereKey($this->input('repositories', [])); - })->latest($this->model()->getKeyName()) - ->chunk($count, function ($chunk) use ($callback, &$output) { + if (($query = $this->builder($action, $count))->count() === 0) { + $output[] = $callback(Collection::make([])); + } + + $query->chunk($count, function ($chunk) use ($callback, &$output) { $output[] = $callback(Collection::make($chunk)); }); diff --git a/tests/Actions/PerformActionsControllerTest.php b/tests/Actions/PerformActionsControllerTest.php index 119238748..bac451462 100644 --- a/tests/Actions/PerformActionsControllerTest.php +++ b/tests/Actions/PerformActionsControllerTest.php @@ -2,10 +2,16 @@ namespace Binaryk\LaravelRestify\Tests\Actions; +use Binaryk\LaravelRestify\Actions\Action; +use Binaryk\LaravelRestify\Http\Requests\ActionRequest; +use Binaryk\LaravelRestify\Tests\Fixtures\Post\Post; +use Binaryk\LaravelRestify\Tests\Fixtures\Post\PostRepository; use Binaryk\LaravelRestify\Tests\Fixtures\Post\PublishPostAction; use Binaryk\LaravelRestify\Tests\Fixtures\User\ActivateAction; use Binaryk\LaravelRestify\Tests\Fixtures\User\DisableProfileAction; use Binaryk\LaravelRestify\Tests\IntegrationTest; +use Illuminate\Http\Request; +use Illuminate\Support\Collection; class PerformActionsControllerTest extends IntegrationTest { @@ -19,7 +25,7 @@ public function test_could_perform_action_for_multiple_repositories() { $post = $this->mockPosts(1, 2); - $this->post('posts/action?action='.(new PublishPostAction())->uriKey(), [ + $this->post('posts/action?action=' . (new PublishPostAction())->uriKey(), [ 'repositories' => [ $post->first()->id, $post->last()->id, @@ -35,6 +41,33 @@ public function test_could_perform_action_for_multiple_repositories() $this->assertEquals(1, PublishPostAction::$applied[0][1]->id); } + public function test_could_perform_action_using_all() + { + $this->assertDatabaseCount('posts', 0); + + PostRepository::partialMock() + ->shouldReceive('actions') + ->andReturn([ + new class extends Action { + public static $uriKey = 'publish'; + + public function handle(Request $request, Collection $collection) + { + return response()->json([ + 'fromHandle' => $collection->count(), + ]); + } + } + ]); + + $this->post('posts/action?action=publish', [ + 'repositories' => 'all' + ])->assertOk()->assertJsonFragment([ + 'fromHandle' => 0, + ]); + + } + public function test_cannot_apply_a_show_action_to_index() { $post = $this->mockPosts(1, 2); @@ -42,7 +75,7 @@ public function test_cannot_apply_a_show_action_to_index() $_SERVER['actions.posts.invalidate'] = true; $_SERVER['actions.posts.publish.onlyOnShow'] = true; - $this->post('posts/action?action='.(new PublishPostAction())->uriKey(), [ + $this->post('posts/action?action=' . (new PublishPostAction())->uriKey(), [ 'repositories' => [ $post->first()->id, $post->last()->id, @@ -58,7 +91,7 @@ public function test_show_action_not_need_repositories() { $users = $this->mockUsers(); - $this->post('users/'.$users->first()->id.'/action?action='.(new ActivateAction)->uriKey()) + $this->post('users/' . $users->first()->id . '/action?action=' . (new ActivateAction)->uriKey()) ->assertSuccessful() ->assertJsonStructure([ 'data', @@ -69,7 +102,7 @@ public function test_show_action_not_need_repositories() public function test_could_perform_standalone_action() { - $this->post('users/action?action='.(new DisableProfileAction())->uriKey()) + $this->post('users/action?action=' . (new DisableProfileAction())->uriKey()) ->assertSuccessful() ->assertJsonStructure([ 'data', From 2bf1b34497152fe97326b44f8dd45476fc6ac0f2 Mon Sep 17 00:00:00 2001 From: Lupacescu Eduard Date: Sun, 27 Dec 2020 09:20:53 +0200 Subject: [PATCH 2/2] Apply fixes from StyleCI (#340) --- src/Actions/Action.php | 2 +- tests/Actions/PerformActionsControllerTest.php | 15 ++++++--------- 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/src/Actions/Action.php b/src/Actions/Action.php index 06ad30b12..d3c30f554 100644 --- a/src/Actions/Action.php +++ b/src/Actions/Action.php @@ -44,7 +44,7 @@ abstract class Action extends RestController implements JsonSerializable public static function indexQuery(RestifyRequest $request, $query) { - // + // } /** diff --git a/tests/Actions/PerformActionsControllerTest.php b/tests/Actions/PerformActionsControllerTest.php index bac451462..13080461e 100644 --- a/tests/Actions/PerformActionsControllerTest.php +++ b/tests/Actions/PerformActionsControllerTest.php @@ -3,8 +3,6 @@ namespace Binaryk\LaravelRestify\Tests\Actions; use Binaryk\LaravelRestify\Actions\Action; -use Binaryk\LaravelRestify\Http\Requests\ActionRequest; -use Binaryk\LaravelRestify\Tests\Fixtures\Post\Post; use Binaryk\LaravelRestify\Tests\Fixtures\Post\PostRepository; use Binaryk\LaravelRestify\Tests\Fixtures\Post\PublishPostAction; use Binaryk\LaravelRestify\Tests\Fixtures\User\ActivateAction; @@ -25,7 +23,7 @@ public function test_could_perform_action_for_multiple_repositories() { $post = $this->mockPosts(1, 2); - $this->post('posts/action?action=' . (new PublishPostAction())->uriKey(), [ + $this->post('posts/action?action='.(new PublishPostAction())->uriKey(), [ 'repositories' => [ $post->first()->id, $post->last()->id, @@ -57,15 +55,14 @@ public function handle(Request $request, Collection $collection) 'fromHandle' => $collection->count(), ]); } - } + }, ]); $this->post('posts/action?action=publish', [ - 'repositories' => 'all' + 'repositories' => 'all', ])->assertOk()->assertJsonFragment([ 'fromHandle' => 0, ]); - } public function test_cannot_apply_a_show_action_to_index() @@ -75,7 +72,7 @@ public function test_cannot_apply_a_show_action_to_index() $_SERVER['actions.posts.invalidate'] = true; $_SERVER['actions.posts.publish.onlyOnShow'] = true; - $this->post('posts/action?action=' . (new PublishPostAction())->uriKey(), [ + $this->post('posts/action?action='.(new PublishPostAction())->uriKey(), [ 'repositories' => [ $post->first()->id, $post->last()->id, @@ -91,7 +88,7 @@ public function test_show_action_not_need_repositories() { $users = $this->mockUsers(); - $this->post('users/' . $users->first()->id . '/action?action=' . (new ActivateAction)->uriKey()) + $this->post('users/'.$users->first()->id.'/action?action='.(new ActivateAction)->uriKey()) ->assertSuccessful() ->assertJsonStructure([ 'data', @@ -102,7 +99,7 @@ public function test_show_action_not_need_repositories() public function test_could_perform_standalone_action() { - $this->post('users/action?action=' . (new DisableProfileAction())->uriKey()) + $this->post('users/action?action='.(new DisableProfileAction())->uriKey()) ->assertSuccessful() ->assertJsonStructure([ 'data',