From 43f1bc73f96dd87cc647afafe8e1fa9fb11f3de9 Mon Sep 17 00:00:00 2001 From: Loris Leiva Date: Sun, 17 May 2020 22:33:15 +0100 Subject: [PATCH] Make handle method optional See #39 --- src/Action.php | 4 ++++ tests/RunsAsObjectsTest.php | 19 +++++++++++++++---- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/Action.php b/src/Action.php index b0ca2bd..5d10a62 100644 --- a/src/Action.php +++ b/src/Action.php @@ -137,6 +137,10 @@ protected function handleRun(array $attributes = []) $this->resolveAuthorization(); $this->resolveValidation(); + if (! method_exists($this, 'handle')) { + return; + } + return $this->resolveAndCall($this, 'handle'); } diff --git a/tests/RunsAsObjectsTest.php b/tests/RunsAsObjectsTest.php index fbc3914..85c1ce0 100644 --- a/tests/RunsAsObjectsTest.php +++ b/tests/RunsAsObjectsTest.php @@ -3,6 +3,7 @@ namespace Lorisleiva\Actions\Tests; use BadMethodCallException; +use Lorisleiva\Actions\Action; use Lorisleiva\Actions\Tests\Actions\SimpleCalculator; class RunsAsObjectsTest extends TestCase @@ -195,16 +196,26 @@ public function it_returns_a_subset_of_the_attributes_of_an_action() ]); $this->assertEquals( - ['operation' => 'addition'], + ['operation' => 'addition'], $action->only('operation') ); $this->assertEquals( - ['right' => 5], + ['right' => 5], $action->except('operation', 'left') ); } + /** @test */ + public function actions_can_have_no_handle_method() + { + // An empty action... + $action = new class() extends Action {}; + + // ...returns null without throwing any exceptions. + $this->assertNull($action->run()); + } + /** @test */ public function it_can_override_the_way_we_get_attribute_from_the_constructor() { @@ -240,7 +251,7 @@ public function it_can_override_constructor_attributes_by_reflecting_the_propert $action = new class('addition', 3, 5) extends SimpleCalculator { protected $getAttributesFromConstructor = true; - public function handle($operation, $left, $right) + public function handle($operation, $left, $right) { return parent::handle($operation, $left, $right); } @@ -281,4 +292,4 @@ public function it_throws_an_exception_when_calling_missing_methods() $this->assertEquals('Method Lorisleiva\Actions\Tests\Actions\SimpleCalculator::missingStaticMethod does not exist.', $e->getMessage()); } } -} \ No newline at end of file +}