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

Commit

Permalink
Throw EmptyPipelineException if passed continuation is invoked more t…
Browse files Browse the repository at this point in the history
…han once
  • Loading branch information
alihmm committed Jun 8, 2019
1 parent e62d1c5 commit 90da95e
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/Next.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

namespace Zend\Stratigility;

use Zend\Stratigility\Middleware\RequestHandlerMiddleware;
use Zend\Stratigility\Exception\EmptyPipelineException;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
Expand Down Expand Up @@ -39,12 +41,15 @@ public function __construct(SplQueue $queue, RequestHandlerInterface $fallbackHa
{
$this->queue = clone $queue;
$this->fallbackHandler = $fallbackHandler;
$this->queue->push(
new RequestHandlerMiddleware($fallbackHandler),
);
}

public function handle(ServerRequestInterface $request) : ResponseInterface
{
if ($this->queue->isEmpty()) {
return $this->fallbackHandler->handle($request);
throw EmptyPipelineException::forClass(__CLASS__);
}

$middleware = $this->queue->dequeue();
Expand Down
23 changes: 23 additions & 0 deletions test/NextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use Zend\Diactoros\ServerRequest as Request;
use Zend\Diactoros\Uri;
use Zend\Stratigility\Next;
use Zend\Stratigility\Exception\EmptyPipelineException;

class NextTest extends TestCase
{
Expand Down Expand Up @@ -214,4 +215,26 @@ public function testMiddlewareReturningResponseShortCircuitsProcess()

$this->assertSame($response, $next->handle($this->request));
}

public function testCallingContinuationMoreThanOnceRaisesException()
{
$this->expectException(EmptyPipelineException::class);
$fallBackHandler = $this->prophesize(RequestHandlerInterface::class);
$fallBackHandler
->handle(Argument::any())
->shouldBeCalledTimes(1);

// Middleware calling $handler->handle() twice. The first call will empty the
// middleware queue while the second call will raise the empty pipline exception.
$middleware = (new class () implements MiddlewareInterface {
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface {
$handler->handle($request);
return $handler->handle($request);
}
});
$this->queue->push($middleware);

$next = new Next($this->queue, $fallBackHandler->reveal());
$next->handle($this->request);
}
}

0 comments on commit 90da95e

Please sign in to comment.