Skip to content

Commit 31b7def

Browse files
Simpler process resolution using PSR container
1 parent 9a9cdb6 commit 31b7def

File tree

3 files changed

+74
-12
lines changed

3 files changed

+74
-12
lines changed

src/Delegate.php

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,16 +60,13 @@ public function process(ServerRequestInterface $request): ResponseInterface
6060

6161
if (!$this->container instanceof ContainerInterface || !$this->container->has($middleware)) {
6262
throw new InvalidArgumentException(
63-
sprintf('The middleware is not a valid %s and is not passed in the Container', MiddlewareInterface::class));
64-
}
65-
66-
$middleware = $this->container->get($middleware);
67-
if (!$middleware instanceof MiddlewareInterface) {
68-
throw new InvalidArgumentException(
69-
sprintf('The middleware is not a %s implementation', MiddlewareInterface::class)
63+
sprintf('The middleware is not a valid %s and is not passed in the Container', MiddlewareInterface::class),
64+
$middleware
7065
);
7166
}
7267

73-
return $middleware->process($request, clone $this);
68+
array_unshift($this->middlewares, $this->container->get($middleware));
69+
70+
return $this->process($request);
7471
}
7572
}

src/Exception/InvalidArgumentException.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,35 @@
44

55
namespace Moon\HttpMiddleware\Exception;
66

7+
use Throwable;
8+
79
class InvalidArgumentException extends \InvalidArgumentException
810
{
11+
/**
12+
* @var mixed|null
13+
*/
14+
private $invalidMiddleware;
15+
16+
/**
17+
* InvalidArgumentException constructor.
18+
* @param string $message
19+
* @param mixed|null $invalidMiddleware
20+
* @param int $code
21+
* @param Throwable|null $previous
22+
*/
23+
public function __construct($message = '', $invalidMiddleware = null, $code = 0, Throwable $previous = null)
24+
{
25+
parent::__construct($message, $code, $previous);
26+
$this->invalidMiddleware = $invalidMiddleware;
27+
}
28+
29+
/**
30+
* Return the invalid middleware
31+
*
32+
* @return mixed|null
33+
*/
34+
public function getInvalidMiddleware()
35+
{
36+
return $this->invalidMiddleware;
37+
}
938
}

tests/Unit/DelegateTest.php

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,36 @@ public function testMiddlewareStackIsTraversed()
5252
$delegate->process($firstRequestProphecy->reveal());
5353
}
5454

55+
public function testMiddlewareStackIsTraversedUsingContainer()
56+
{
57+
$firstRequestProphecy = $this->prophesize(ServerRequestInterface::class);
58+
$secondRequestProphecy = $this->prophesize(ServerRequestInterface::class);
59+
$thirdRequestMock = $this->prophesize(ServerRequestInterface::class)->reveal();
60+
61+
$secondRequestProphecy->getAttribute('total')->shouldBeCalled(1)->willReturn(2);
62+
$secondRequestProphecy->withAttribute('total', 4)->shouldBeCalled(1)->willReturn($thirdRequestMock);
63+
64+
$firstRequestProphecy->getAttribute('total')->shouldBeCalled(1)->willReturn(1);
65+
$firstRequestProphecy->withAttribute('total', 2)->shouldBeCalled(1)->willReturn($secondRequestProphecy->reveal());
66+
67+
$responseMock = $this->createMock(ResponseInterface::class);
68+
$assertion = function (ServerRequestInterface $request) use ($thirdRequestMock, $responseMock) {
69+
$this->assertSame($thirdRequestMock, $request);
70+
return $responseMock;
71+
};
72+
73+
$containerProphecy = $this->prophesize(ContainerInterface::class);
74+
$containerProphecy->has('one')->shouldBeCalled(1)->willReturn(true);
75+
$containerProphecy->has('two')->shouldBeCalled(1)->willReturn(true);
76+
$containerProphecy->get('one')->shouldBeCalled(1)->willReturn(new PlusOneMiddleware());
77+
$containerProphecy->get('two')->shouldBeCalled(1)->willReturn(new PlusTwoMiddleware());
78+
$containerMock = $containerProphecy->reveal();
79+
80+
81+
$delegate = new Delegate(['one', 'two'], $assertion, $containerMock);
82+
$delegate->process($firstRequestProphecy->reveal());
83+
}
84+
5585
public function testMiddlewareStackStop()
5686
{
5787
$requestMock = $this->prophesize(ServerRequestInterface::class)->reveal();
@@ -63,20 +93,26 @@ public function testMiddlewareStackStop()
6393
$this->assertSame($responseMock, $delegate->process($requestMock));
6494
}
6595

66-
public function testInvalidLazyLoadingMiddlewareFromContainer()
96+
public function testInvalidMiddlewareisPassedToInvalidArgumentException()
6797
{
6898
$this->expectException(InvalidArgumentException::class);
69-
99+
$invalidMiddleware = new \SplStack();
70100
$requestMock = $this->prophesize(ServerRequestInterface::class)->reveal();
71101
$containerProphecy = $this->prophesize(ContainerInterface::class);
72102
$containerProphecy->has('InvalidMiddleware')->shouldBeCalled(1)->willReturn(true);
73-
$containerProphecy->get('InvalidMiddleware')->shouldBeCalled(1)->willReturn(new \SplStack());
103+
$containerProphecy->has(Argument::any())->shouldBeCalled(2)->willReturn(false);
104+
$containerProphecy->get('InvalidMiddleware')->shouldBeCalled(1)->willReturn($invalidMiddleware);
74105
$containerMock = $containerProphecy->reveal();
75106

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

79-
$delegate->process($requestMock);
110+
try {
111+
$delegate->process($requestMock);
112+
} catch (InvalidArgumentException $e) {
113+
$this->assertSame($e->getInvalidMiddleware(), $invalidMiddleware);
114+
throw $e;
115+
}
80116
}
81117

82118
public function testLazyLoadingMiddlewareFromContainer()

0 commit comments

Comments
 (0)