From d8fc49d916f25001b8b274bf8e0ecd0c91e6d04d Mon Sep 17 00:00:00 2001 From: Justin Scott Date: Fri, 16 Oct 2020 11:52:28 -0500 Subject: [PATCH] Fixes ignored default values in handle method. - Always call `resolveAttributesFromConstructor` in the constructor to account for possible attributes in handle signature. - Treat attributes `handle` and attributes in `getAttributesFromConstructor` separately. - Normalize return value to `$this` in `resolveAttributesFromConstructor` --- src/Action.php | 13 +++++++++---- .../SimpleCalculatorWithHandleDefaults.php | 15 +++++++++++++++ tests/RegistersActionsTest.php | 6 ++++-- tests/RunsAsObjectsTest.php | 14 ++++++++++++++ 4 files changed, 42 insertions(+), 6 deletions(-) create mode 100644 tests/Actions/SimpleCalculatorWithHandleDefaults.php diff --git a/src/Action.php b/src/Action.php index c816122..f636524 100644 --- a/src/Action.php +++ b/src/Action.php @@ -54,9 +54,8 @@ public function __construct() $this->resolveAndCall($this, 'initialized'); } - if (func_num_args() > 0) { - $this->resolveAttributesFromConstructor(...func_get_args()); - } + $args = func_num_args() > 0 ? func_get_args() : []; + $this->resolveAttributesFromConstructor(...$args); } protected function resolveAttributesFromConstructor(...$arguments) @@ -71,12 +70,18 @@ protected function resolveAttributesFromConstructor(...$arguments) if ($attributes === true && method_exists($this, 'handle')) { $reflector = new ReflectionMethod($this, 'handle'); - $attributes = collect($reflector->getParameters())->map->getName()->toArray(); + foreach($reflector->getParameters() as $index => $param) { + $defaultValue = $param->isDefaultValueAvailable() ? $param->getDefaultValue() : null; + $this->set($param->getName(), Arr::get($arguments, $index, $defaultValue)); + } + return $this; } foreach (Arr::wrap($attributes) as $index => $name) { $this->set($name, Arr::get($arguments, $index, null)); } + + return $this; } /** diff --git a/tests/Actions/SimpleCalculatorWithHandleDefaults.php b/tests/Actions/SimpleCalculatorWithHandleDefaults.php new file mode 100644 index 0000000..1c40925 --- /dev/null +++ b/tests/Actions/SimpleCalculatorWithHandleDefaults.php @@ -0,0 +1,15 @@ +instance(ActionManager::class, new class() extends ActionManager { @@ -30,7 +32,7 @@ public function it_can_register_all_actions_within_a_directory() Actions::paths(__DIR__ . '/Actions'); Actions::registerAllPaths(); - $this->assertCount(10, Actions::getRegisteredActions()); + $this->assertCount(static::ACTION_COUNT, Actions::getRegisteredActions()); } /** @test */ @@ -40,7 +42,7 @@ public function it_only_register_actions_once() Actions::registerAllPaths(); Actions::registerAllPaths(); - $this->assertCount(10, Actions::getRegisteredActions()); + $this->assertCount(static::ACTION_COUNT, Actions::getRegisteredActions()); } /** @test */ diff --git a/tests/RunsAsObjectsTest.php b/tests/RunsAsObjectsTest.php index 85c1ce0..7159ee3 100644 --- a/tests/RunsAsObjectsTest.php +++ b/tests/RunsAsObjectsTest.php @@ -5,6 +5,7 @@ use BadMethodCallException; use Lorisleiva\Actions\Action; use Lorisleiva\Actions\Tests\Actions\SimpleCalculator; +use Lorisleiva\Actions\Tests\Actions\SimpleCalculatorWithHandleDefaults; class RunsAsObjectsTest extends TestCase { @@ -90,6 +91,19 @@ public function it_can_fill_multiple_attributes_at_once() $this->assertEquals(5, $action->right); } + /** @test */ + public function it_can_fill_attributes_dynamically_from_handle() + { + $action = (new SimpleCalculatorWithHandleDefaults())->fill([ + 'operation' => 'addition', + ]); + + $this->assertEquals('addition', $action->operation); + $this->assertEquals(50, $action->left); + $this->assertEquals(100, $action->right); + $this->assertEquals(150, $action->run()); + } + /** @test */ public function it_can_run_with_some_additional_data_to_fill() {