Skip to content

Commit

Permalink
Fixes ignored default values in handle method.
Browse files Browse the repository at this point in the history
- 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`
  • Loading branch information
Justin Scott committed Oct 16, 2020
1 parent ef190a2 commit d8fc49d
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 6 deletions.
13 changes: 9 additions & 4 deletions src/Action.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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;
}

/**
Expand Down
15 changes: 15 additions & 0 deletions tests/Actions/SimpleCalculatorWithHandleDefaults.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Lorisleiva\Actions\Tests\Actions;

use Lorisleiva\Actions\Action;

class SimpleCalculatorWithHandleDefaults extends SimpleCalculator
{
protected $getAttributesFromConstructor = true;

public function handle($operation, $left = 50, $right = 100)
{
return parent::handle($operation, $left, $right);
}
}
6 changes: 4 additions & 2 deletions tests/RegistersActionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

class RegistersActionsTest extends TestCase
{
const ACTION_COUNT = 11;

protected function getEnvironmentSetUp($app)
{
$app->instance(ActionManager::class, new class() extends ActionManager {
Expand All @@ -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 */
Expand All @@ -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 */
Expand Down
14 changes: 14 additions & 0 deletions tests/RunsAsObjectsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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()
{
Expand Down

0 comments on commit d8fc49d

Please sign in to comment.