Skip to content

Commit

Permalink
Make handle method optional
Browse files Browse the repository at this point in the history
See #39
  • Loading branch information
lorisleiva committed May 17, 2020
1 parent b101372 commit 43f1bc7
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
4 changes: 4 additions & 0 deletions src/Action.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,10 @@ protected function handleRun(array $attributes = [])
$this->resolveAuthorization();
$this->resolveValidation();

if (! method_exists($this, 'handle')) {
return;
}

return $this->resolveAndCall($this, 'handle');
}

Expand Down
19 changes: 15 additions & 4 deletions tests/RunsAsObjectsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Lorisleiva\Actions\Tests;

use BadMethodCallException;
use Lorisleiva\Actions\Action;
use Lorisleiva\Actions\Tests\Actions\SimpleCalculator;

class RunsAsObjectsTest extends TestCase
Expand Down Expand Up @@ -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()
{
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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());
}
}
}
}

0 comments on commit 43f1bc7

Please sign in to comment.