Skip to content

Commit

Permalink
Merge 01e5a95 into b0e7adb
Browse files Browse the repository at this point in the history
  • Loading branch information
Moln committed Aug 22, 2018
2 parents b0e7adb + 01e5a95 commit 01ed438
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 21 deletions.
4 changes: 4 additions & 0 deletions src/RouteNameContentValidationMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ public function process(
ServerRequestInterface $request,
RequestHandlerInterface $handler
): ResponseInterface {
if ($request->getAttribute(ContentValidationMiddleware::INPUT_FILTER_NAME)) {
return parent::process($request, $handler);
}

// Set expressive route name or slim route name
if ($route = $request->getAttribute('Zend\Expressive\Router\RouteResult')) {
$request = $request->withAttribute(
Expand Down
61 changes: 40 additions & 21 deletions test/RouteNameContentValidationMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,47 +14,66 @@

class RouteNameContentValidationMiddlewareTest extends TestCase
{
public function testExistsInputFilterName()
{
$middleware = new RouteNameContentValidationMiddleware();

$req = $this->prophesize(ServerRequestInterface::class);
$req->getAttribute(ContentValidationMiddleware::INPUT_FILTER_NAME)
->willReturn('test')
->shouldBeCalledTimes(2);

$middleware->process($req->reveal(), $this->getHandler());
}

public function testProcessExpressive()
{
$middleware = new RouteNameContentValidationMiddleware();

$routeResult = $this->prophesize(RouteResult::class);
$routeResult->getMatchedRouteName()->willReturn('test')->shouldBeCalled();
$req2 = $this->prophesize(ServerRequestInterface::class);
$req2->getAttribute(ContentValidationMiddleware::INPUT_FILTER_NAME)->shouldBeCalled();

$req = $this->prophesize(ServerRequestInterface::class);
$req->getAttribute(ContentValidationMiddleware::INPUT_FILTER_NAME)->willReturn(null);
$req->getAttribute('Zend\Expressive\Router\RouteResult')
->willReturn($routeResult->reveal())
->shouldBeCalled();
$req->withAttribute(ContentValidationMiddleware::INPUT_FILTER_NAME, 'test')
->willReturn($req2->reveal())
->shouldBeCalled();

$handler = $this->createMock(RequestHandlerInterface::class);
$handler->method('handle')->willReturn(new Response());
$this->withAttribute($req);

$middleware->process($req->reveal(), $handler);
$middleware->process($req->reveal(), $this->getHandler());
}

public function testProcessSlim()
{
$middleware = new RouteNameContentValidationMiddleware();

$routeResult = $this->prophesize(Route::class);
$routeResult->getName()->willReturn('test')->shouldBeCalled();

$req = $this->prophesize(ServerRequestInterface::class);
$req->getAttribute(ContentValidationMiddleware::INPUT_FILTER_NAME)->willReturn(null);
$req->getAttribute('Zend\Expressive\Router\RouteResult')->willReturn(null)->shouldBeCalled();
$req->getAttribute('route')->willReturn($routeResult);
$this->withAttribute($req);

$middleware->process($req->reveal(), $this->getHandler());
}

private function withAttribute($req, $value = 'test')
{
$req2 = $this->prophesize(ServerRequestInterface::class);
$req2->getAttribute(ContentValidationMiddleware::INPUT_FILTER_NAME)->shouldBeCalled();
$req = $this->createMock(ServerRequestInterface::class);
$req->method('getAttribute')->willReturnCallback(function ($name) {
$routeResult = $this->createMock(Route::class);
$routeResult->method('getName')
->willReturn('test');
if ($name == 'route') {
return $routeResult;
}
});
$req->method('withAttribute')
->willReturn($req2->reveal());

$req->withAttribute(ContentValidationMiddleware::INPUT_FILTER_NAME, $value)
->willReturn($req2)
->shouldBeCalled();
}

private function getHandler(): RequestHandlerInterface
{
$handler = $this->createMock(RequestHandlerInterface::class);
$handler->method('handle')->willReturn(new Response());

$middleware->process($req, $handler);
return $handler;
}
}

0 comments on commit 01ed438

Please sign in to comment.