Skip to content

Commit

Permalink
close #102
Browse files Browse the repository at this point in the history
  • Loading branch information
rodber committed Dec 20, 2022
1 parent 07d4eb2 commit cfea771
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 55 deletions.
13 changes: 7 additions & 6 deletions src/Action/Action.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ final public function __construct()
$this->setUpAfter();
}

final public function getResponse(mixed ...$namedArguments): ResponseInterface
final public function getResponse(mixed ...$argument): ResponseInterface
{
$this->assertContainer();
$arguments = $this->getArguments(...$namedArguments)->toArray();
$arguments = $this->getArguments(...$argument)->toArray();
$data = $this->run(...$arguments);
if (! is_array($data)) {
throw new TypeError(
Expand All @@ -74,11 +74,11 @@ final public function getResponse(mixed ...$namedArguments): ResponseInterface
return $this->getTypedResponse(...$data);
}

final protected function getTypedResponse(mixed ...$namedArguments): ResponseInterface
final protected function getTypedResponse(mixed ...$arguments): ResponseInterface
{
$arguments = new Arguments($this->responseParameters(), ...$namedArguments);
new Arguments($this->responseParameters(), ...$arguments);

return new Response(...$arguments->toArray());
return new Response(...$arguments);
}

final protected function assertContainer(): void
Expand Down Expand Up @@ -138,7 +138,8 @@ final protected function getParameters(): ParametersInterface
$collection[$pos][$reflectionParameter->getName()] = $parameter;
}

return (new Parameters())->withAddedRequired(...$collection[1])
return (new Parameters())
->withAddedRequired(...$collection[1])
->withAddedOptional(...$collection[0]);
}

Expand Down
13 changes: 7 additions & 6 deletions src/Action/Interfaces/ActionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ public function getContainerParameters(): ParametersInterface;
*/
public function containerParameters(): ParametersInterface;

/**
* Return an instance with the specified `$container`.
*
* This method MUST retain the state of the current instance, and return
* an instance that contains the specified `$container`.
*/
public function withContainer(ContainerInterface $container): static;

/**
Expand All @@ -62,11 +68,6 @@ public function container(): ContainerInterface;
*/
public function getResponseParameters(): ParametersInterface;

/**
* Method called when running the action.
*/
// public function run({...}): ResponseInterface;

/**
* Provides access to the parameters.
*/
Expand All @@ -83,5 +84,5 @@ public function responseParameters(): ParametersInterface;
* This method will provide a response instance with data provided by
* executing the `run` method against `$namedArguments`.
*/
public function getResponse(mixed ...$namedArguments): ResponseInterface;
public function getResponse(mixed ...$argument): ResponseInterface;
}
16 changes: 8 additions & 8 deletions src/Parameter/Arguments.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,17 @@ final class Arguments implements ArgumentsInterface

public function __construct(
private ParametersInterface $parameters,
mixed ...$namedArguments
mixed ...$argument
) {
$storeArguments = [];
foreach (array_keys($namedArguments) as $name) {
foreach (array_keys($argument) as $name) {
$name = strval($name);
if (! $this->parameters()->has($name)) {
unset($namedArguments[$name]);
unset($argument[$name]);

continue;
}
$storeArguments[$name] = $namedArguments[$name];
$storeArguments[$name] = $argument[$name];
}
$this->arguments = $storeArguments;
$this->assertRequired();
Expand Down Expand Up @@ -82,13 +82,13 @@ public function toArray(): array
* @throws TypeError
* @throws InvalidArgumentException
*/
public function withPut(mixed ...$nameValue): ArgumentsInterface
public function withPut(mixed ...$value): ArgumentsInterface
{
$new = clone $this;
foreach ($nameValue as $name => $value) {
foreach ($value as $name => $item) {
$name = strval($name);
$new->assertType($name, $value);
$new->arguments[$name] = $value;
$new->assertType($name, $item);
$new->arguments[$name] = $item;
}

return $new;
Expand Down
4 changes: 2 additions & 2 deletions src/Parameter/Interfaces/ArgumentsInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ interface ArgumentsInterface extends ToArrayInterface
* @throws OutOfBoundsException
* @throws InvalidArgumentException
*/
public function __construct(ParametersInterface $parameters, mixed ...$namedArguments);
public function __construct(ParametersInterface $parameters, mixed ...$argument);

/**
* Provides access to the parameters instance.
Expand All @@ -54,7 +54,7 @@ public function toArray(): array;
* @throws InvalidArgumentException
* @throws OutOfBoundsException If `$name` is not a known parameter.
*/
public function withPut(mixed ...$nameValue): self;
public function withPut(mixed ...$value): self;

/**
* Indicates whether the instance has an argument for the parameter `$name`.
Expand Down
25 changes: 17 additions & 8 deletions tests/Action/ActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use Chevere\Tests\Action\_resources\src\ActionTestAction;
use Chevere\Tests\Action\_resources\src\ActionTestContainer;
use Chevere\Tests\Action\_resources\src\ActionTestController;
use Chevere\Tests\Action\_resources\src\ActionTestGetResponseMerge;
use Chevere\Tests\Action\_resources\src\ActionTestInvalidRunParameter;
use Chevere\Tests\Action\_resources\src\ActionTestInvalidRunReturn;
use Chevere\Tests\Action\_resources\src\ActionTestMissingRun;
Expand All @@ -46,13 +47,13 @@ public function testConstruct(): void
$action->run();
}

public function testActionMissingRun(): void
public function testMissingRunMethod(): void
{
$this->expectException(LogicException::class);
new ActionTestMissingRun();
}

public function testActionParams(): void
public function testRunParams(): void
{
$defaults = [
'intDefault' => 1,
Expand Down Expand Up @@ -90,7 +91,7 @@ public function testActionParams(): void
);
}

public function testActionParamsAttributes(): void
public function testParamsAttributes(): void
{
$action = new ActionTestParameterAttributes();
$this->assertSame('An int', $action->parameters()->get('int')->description());
Expand All @@ -100,7 +101,7 @@ public function testActionParamsAttributes(): void
$this->assertSame('/^[a-z]$/', $parameter->regex()->__toString());
}

public function testActionContainer(): void
public function testContainer(): void
{
$container = new Container();
$containerWith = $container->withPut(id: 123, name: 'wea');
Expand All @@ -114,15 +115,15 @@ public function testActionContainer(): void
$action->withContainer($container);
}

public function testActionContainerMissingParameterException(): void
public function testContainerMissingParameterException(): void
{
$action = new ActionTestContainer();
$this->expectExceptionMessage('[id, name]');
$this->expectException(InvalidArgumentException::class);
$action->getResponse();
}

public function testActionRunWithArguments(): void
public function testRunWithArguments(): void
{
$parameter = 'name';
$value = 'PeoplesHernandez';
Expand All @@ -140,7 +141,7 @@ public function testActionRunWithArguments(): void
$this->assertSame($expected, $response->data());
}

public function testActionInvalidRunReturn(): void
public function testInvalidRunReturn(): void
{
$action = new ActionTestInvalidRunReturn();
$data = $action->run();
Expand All @@ -150,7 +151,7 @@ public function testActionInvalidRunReturn(): void
$action->getResponse();
}

public function testActionInvalidRunParameter(): void
public function testInvalidRunParameter(): void
{
$this->expectException(TypeError::class);
$this->expectExceptionMessage('$mixed');
Expand All @@ -163,4 +164,12 @@ public function testSetupBeforeAndAfter(): void
$this->assertSame(1, $action->before());
$this->assertSame(2, $action->after());
}

public function testResponseMerge(): void
{
$action = new ActionTestGetResponseMerge();
$typedResponse = $action->getResponse();
$runResponse = $action->run();
$this->assertSame($runResponse, $typedResponse->data());
}
}
25 changes: 0 additions & 25 deletions tests/Action/_resources/src/ActionRunnerTestControllerRunFail.php

This file was deleted.

38 changes: 38 additions & 0 deletions tests/Action/_resources/src/ActionTestGetResponseMerge.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

/*
* This file is part of Chevere.
*
* (c) Rodolfo Berrios <rodolfo@chevere.org>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Chevere\Tests\Action\_resources\src;

use Chevere\Action\Action;
use function Chevere\Parameter\integerParameter;
use Chevere\Parameter\Interfaces\ParametersInterface;
use Chevere\Parameter\Parameters;

final class ActionTestGetResponseMerge extends Action
{
public function getResponseParameters(): ParametersInterface
{
return new Parameters(
id: integerParameter(),
);
}

public function run(): array
{
return [
'id' => 1,
'name' => 'name',
'extra' => 'extra',
];
}
}

0 comments on commit cfea771

Please sign in to comment.