Skip to content
This repository has been archived by the owner on Jan 29, 2020. It is now read-only.

Ensure that MiddlewarePipes are called as callables if an error is present #95

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Dispatch.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ public function __invoke(
$this->setResponsePrototype($response);
}

// Handle middleware pipes as callables if an $err is present
if ($route->handler instanceof MiddlewarePipe && null !== $err) {
return $this->dispatchCallableMiddleware($route->handler, $next, $request, $response, $err);
}

if ($route->handler instanceof ServerMiddlewareInterface) {
return $this->dispatchInteropMiddleware($route->handler, $next, $request);
}
Expand Down
93 changes: 93 additions & 0 deletions test/DispatchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,99 @@ public function testInvokingWithInteropMiddlewareDispatchesIt()
);
}

/**
* @todo Remove this test for version 2.0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should probably spawn an issue for 2.0 for this, then link the issue instead

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also: why is this not needed in 2.0?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because this class goes away entirely, and that version will only work with http-interop middleware. 😉

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Which means, really, that this @todo can go away, as the top of the classfile already indicates the entire file goes away. Updating...

*/
public function testInvokingWithMiddlewarePipeAndErrorDispatchesNextErrorMiddleware()
{
$error = new RuntimeException('expected');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider using just Exception. Even better, Throwable may be needed

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll rewrite to use a data provider to seed multiple exception/throwable types. Thanks for the feedback!

$request = $this->prophesize(ServerRequestInterface::class)->reveal();
$response = $this->prophesize(ResponseInterface::class)->reveal();
$expected = $this->prophesize(ResponseInterface::class)->reveal();

$next = $this->prophesize(Next::class);
$next->willImplement(DelegateInterface::class);
$next
->__invoke(
$request,
$this->response->reveal(),
$error
)
->willReturn($expected);

$middleware = $this->prophesize(MiddlewarePipe::class);
$middleware
->__invoke(
Argument::type(ServerRequestInterface::class),
Argument::type(ResponseInterface::class),
Argument::type('callable')
)
->shouldNotBeCalled();
$middleware
->process(
Argument::type(ServerRequestInterface::class),
Argument::type(DelegateInterface::class)
)
->shouldNotBeCalled();

$route = new Route('/foo', $middleware->reveal());

$dispatch = new Dispatch();

$this->assertSame(
$expected,
$dispatch($route, $error, $request, $this->response->reveal(), $next->reveal())
);
}

/**
* @todo Remove this test for version 2.0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should probably spawn an issue for 2.0 for this, then link the issue instead

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See note above; I'll just remove these, as there's a note at the top of this file already.

*/
public function testInvokingWithMiddlewarePipeAndNoErrorDispatchesAsInteropMiddleware()
{
$request = $this->prophesize(ServerRequestInterface::class)->reveal();
$response = $this->prophesize(ResponseInterface::class)->reveal();

$next = $this->prophesize(Next::class);
$next->willImplement(DelegateInterface::class);
$next
->__invoke(
Argument::type(ServerRequestInterface::class),
Argument::type(ResponseInterface::class)
)
->shouldNotBeCalled();
$next
->process(Argument::type(ServerRequestInterface::class))
->shouldNotBeCalled();

$middleware = $this->prophesize(MiddlewarePipe::class);
$middleware
->__invoke(
Argument::type(ServerRequestInterface::class),
Argument::type(ResponseInterface::class),
Argument::type('callable')
)
->shouldNotBeCalled();
$middleware
->hasResponsePrototype()
->willReturn(true);
$middleware
->process(
Argument::type(ServerRequestInterface::class),
Argument::type(DelegateInterface::class)
)
->willReturn($response);

$route = new Route('/foo', $middleware->reveal());

$dispatch = new Dispatch();

$this->assertSame(
$response,
$dispatch($route, null, $request, $this->response->reveal(), $next->reveal())
);
}

/**
* @group http-interop
*/
Expand Down