Skip to content

Commit

Permalink
Merged better flow processing delegate with container
Browse files Browse the repository at this point in the history
  • Loading branch information
damianopetrungaro committed Jul 20, 2017
2 parents cd9bae3 + 31b7def commit a719833
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 13 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/vendor/
.idea
composer.phar
composer.phar
.DS_Store
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"name": "moon-php/http-middleware",
"description": "Http-Middleware supporting PSR-15",
"license": "MIT",
"homepage": "https://www.moon-php.com/",
"require": {
"php": ">=7.1",
"psr/http-message": "^1.0",
Expand Down
13 changes: 5 additions & 8 deletions src/Delegate.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,13 @@ public function process(ServerRequestInterface $request): ResponseInterface

if (!$this->container instanceof ContainerInterface || !$this->container->has($middleware)) {
throw new InvalidArgumentException(
sprintf('The middleware is not a valid %s and is not passed in the Container', MiddlewareInterface::class));
}

$middleware = $this->container->get($middleware);
if (!$middleware instanceof MiddlewareInterface) {
throw new InvalidArgumentException(
sprintf('The middleware is not a %s implementation', MiddlewareInterface::class)
sprintf('The middleware is not a valid %s and is not passed in the Container', MiddlewareInterface::class),
$middleware
);
}

return $middleware->process($request, clone $this);
array_unshift($this->middlewares, $this->container->get($middleware));

return $this->process($request);
}
}
29 changes: 29 additions & 0 deletions src/Exception/InvalidArgumentException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,35 @@

namespace Moon\HttpMiddleware\Exception;

use Throwable;

class InvalidArgumentException extends \InvalidArgumentException
{
/**
* @var mixed|null
*/
private $invalidMiddleware;

/**
* InvalidArgumentException constructor.
* @param string $message
* @param mixed|null $invalidMiddleware
* @param int $code
* @param Throwable|null $previous
*/
public function __construct($message = '', $invalidMiddleware = null, $code = 0, Throwable $previous = null)
{
parent::__construct($message, $code, $previous);
$this->invalidMiddleware = $invalidMiddleware;
}

/**
* Return the invalid middleware
*
* @return mixed|null
*/
public function getInvalidMiddleware()
{
return $this->invalidMiddleware;
}
}
44 changes: 40 additions & 4 deletions tests/Unit/DelegateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,36 @@ public function testMiddlewareStackIsTraversed()
$delegate->process($firstRequestProphecy->reveal());
}

public function testMiddlewareStackIsTraversedUsingContainer()
{
$firstRequestProphecy = $this->prophesize(ServerRequestInterface::class);
$secondRequestProphecy = $this->prophesize(ServerRequestInterface::class);
$thirdRequestMock = $this->prophesize(ServerRequestInterface::class)->reveal();

$secondRequestProphecy->getAttribute('total')->shouldBeCalled(1)->willReturn(2);
$secondRequestProphecy->withAttribute('total', 4)->shouldBeCalled(1)->willReturn($thirdRequestMock);

$firstRequestProphecy->getAttribute('total')->shouldBeCalled(1)->willReturn(1);
$firstRequestProphecy->withAttribute('total', 2)->shouldBeCalled(1)->willReturn($secondRequestProphecy->reveal());

$responseMock = $this->createMock(ResponseInterface::class);
$assertion = function (ServerRequestInterface $request) use ($thirdRequestMock, $responseMock) {
$this->assertSame($thirdRequestMock, $request);
return $responseMock;
};

$containerProphecy = $this->prophesize(ContainerInterface::class);
$containerProphecy->has('one')->shouldBeCalled(1)->willReturn(true);
$containerProphecy->has('two')->shouldBeCalled(1)->willReturn(true);
$containerProphecy->get('one')->shouldBeCalled(1)->willReturn(new PlusOneMiddleware());
$containerProphecy->get('two')->shouldBeCalled(1)->willReturn(new PlusTwoMiddleware());
$containerMock = $containerProphecy->reveal();


$delegate = new Delegate(['one', 'two'], $assertion, $containerMock);
$delegate->process($firstRequestProphecy->reveal());
}

public function testMiddlewareStackStop()
{
$requestMock = $this->prophesize(ServerRequestInterface::class)->reveal();
Expand All @@ -63,20 +93,26 @@ public function testMiddlewareStackStop()
$this->assertSame($responseMock, $delegate->process($requestMock));
}

public function testInvalidLazyLoadingMiddlewareFromContainer()
public function testInvalidMiddlewareisPassedToInvalidArgumentException()
{
$this->expectException(InvalidArgumentException::class);

$invalidMiddleware = new \SplStack();
$requestMock = $this->prophesize(ServerRequestInterface::class)->reveal();
$containerProphecy = $this->prophesize(ContainerInterface::class);
$containerProphecy->has('InvalidMiddleware')->shouldBeCalled(1)->willReturn(true);
$containerProphecy->get('InvalidMiddleware')->shouldBeCalled(1)->willReturn(new \SplStack());
$containerProphecy->has(Argument::any())->shouldBeCalled(2)->willReturn(false);
$containerProphecy->get('InvalidMiddleware')->shouldBeCalled(1)->willReturn($invalidMiddleware);
$containerMock = $containerProphecy->reveal();

$delegate = new Delegate(['InvalidMiddleware'], function () {
}, $containerMock);

$delegate->process($requestMock);
try {
$delegate->process($requestMock);
} catch (InvalidArgumentException $e) {
$this->assertSame($e->getInvalidMiddleware(), $invalidMiddleware);
throw $e;
}
}

public function testLazyLoadingMiddlewareFromContainer()
Expand Down

0 comments on commit a719833

Please sign in to comment.