Skip to content

Commit

Permalink
Properly handles response handling thru middleware calls
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobemerick committed Jun 2, 2017
1 parent 4141838 commit c7c6b7d
Show file tree
Hide file tree
Showing 2 changed files with 141 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public function __invoke(Request $request, Response $response, callable $next)
$request = $request->withAttribute('swagger', $parsedSwagger);

$result = $next($request, $response);
$result = $this->encodeResponse($request, $response);
$result = $this->encodeResponse($request, $result);

$this->log("finished");
return $result;
Expand Down
142 changes: 140 additions & 2 deletions tests/unit/src/RouterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1234,6 +1234,143 @@ public function testInvokationReturnsConsumes()
$this->assertSame($mockResponse, $result);
}

public function testInvokationReturnsResponseFromCallable()
{
$path = [
'/test-path' => [
'get' => [
'description' => 'Some operation',
'responses' => [],
],
],
];

$responses = [
'some response objects',
];

$reflectedRouter = new ReflectionClass(Router::class);
$reflectedSwagger = $reflectedRouter->getProperty('swagger');
$reflectedSwagger->setAccessible(true);

$mockParsedSwagger = $this->createMock(ParsedSwagger::class);
$mockParsedSwagger->expects($this->once())
->method('setApiPath')
->with(key($path));
$mockParsedSwagger->expects($this->once())
->method('setPath')
->with(current($path));
$mockParsedSwagger->expects($this->once())
->method('setOperation')
->with(current($path)['get']);
$mockParsedSwagger->expects($this->once())
->method('setResponses')
->with($responses);

$mockUri = $this->createMock(Uri::class);
$mockRequest = $this->createMock(Request::class);
$mockRequest->method('getUri')
->willReturn($mockUri);
$mockRequest->expects($this->once())
->method('getMethod')
->willReturn('GET');
$mockRequest->expects($this->once())
->method('withAttribute')
->with('swagger', $mockParsedSwagger)
->will($this->returnSelf());

$mockResponse = $this->createMock(Response::class);
$mockCallbackResponse = $this->createMock(Response::class);

$callback = function ($request, $response) use ($mockCallbackResponse) {
return $mockCallbackResponse;
};

$router = $this->getMockBuilder(Router::class)
->disableOriginalConstructor()
->setMethods([
'encodeResponse',
'getConsumes',
'getParameters',
'getParsedSwagger',
'getProduces',
'getResponses',
'getSchemes',
'getSecurity',
'hydrateParameterValues',
'isDocumentationRoute',
'log',
'matchPath',
'resolveRefs',
])
->getMock();
$router->expects($this->once())
->method('encodeResponse')
->with($mockRequest, $mockResponse)
->will($this->returnArgument(1));
$router->expects($this->once())
->method('getConsumes')
->with(current($path)['get'])
->willReturn([]);
$router->expects($this->once())
->method('getParameters')
->with(current($path), current($path)['get'])
->willReturn([]);
$router->expects($this->once())
->method('getParsedSwagger')
->willReturn($mockParsedSwagger);
$router->expects($this->once())
->method('getProduces')
->with(current($path)['get'])
->willReturn([]);
$router->expects($this->once())
->method('getResponses')
->with(current($path)['get'])
->willReturn($responses);
$router->expects($this->once())
->method('getSchemes')
->with(current($path)['get'], $mockRequest)
->willReturn([]);
$router->expects($this->once())
->method('getSecurity')
->with(current($path)['get'])
->willReturn([]);
$router->expects($this->once())
->method('hydrateParameterValues')
->with(
$this->isInstanceOf(ParameterParser::class),
$mockRequest,
[],
key($path)
)
->willReturn([]);
$router->expects($this->once())
->method('isDocumentationRoute')
->with($mockRequest)
->willReturn(false);
$router->expects($this->exactly(3))
->method('log')
->withConsecutive(
[ 'start' ],
[ 'request matched with /test-path' ],
[ 'finished' ]
);
$router->expects($this->once())
->method('matchPath')
->with($mockRequest, key($path))
->willReturn(true);
$router->expects($this->once())
->method('resolveRefs')
->with(current($path))
->will($this->returnArgument(0));

$reflectedSwagger->setValue($router, [ 'paths' => $path ]);

$result = $router($mockRequest, $mockResponse, $callback);

$this->assertSame($mockCallbackResponse, $result);
}

public function testInvokationReturnsResponseFromEncode()
{
$path = [
Expand Down Expand Up @@ -1280,6 +1417,7 @@ public function testInvokationReturnsResponseFromEncode()
->will($this->returnSelf());

$mockResponse = $this->createMock(Response::class);
$mockEncodedResponse = $this->createMock(Response::class);

$callback = function ($request, $response) {
return $response;
Expand All @@ -1306,7 +1444,7 @@ public function testInvokationReturnsResponseFromEncode()
$router->expects($this->once())
->method('encodeResponse')
->with($mockRequest, $mockResponse)
->willReturn($mockResponse);
->willReturn($mockEncodedResponse);
$router->expects($this->once())
->method('getConsumes')
->with(current($path)['get'])
Expand Down Expand Up @@ -1367,7 +1505,7 @@ public function testInvokationReturnsResponseFromEncode()

$result = $router($mockRequest, $mockResponse, $callback);

$this->assertSame($mockResponse, $result);
$this->assertSame($mockEncodedResponse, $result);
}

public function testIsDocumentationRouteFailsIfNotGet()
Expand Down

0 comments on commit c7c6b7d

Please sign in to comment.