Use ServerRequest instance in MethodMap test

This commit is contained in:
PJ Dietz 2016-05-21 08:46:13 -04:00
parent 36bb00dc1a
commit 4eb0b2641e
1 changed files with 37 additions and 84 deletions

View File

@ -3,12 +3,11 @@
namespace WellRESTed\Test\Unit\Routing; namespace WellRESTed\Test\Unit\Routing;
use Prophecy\Argument; use Prophecy\Argument;
use WellRESTed\Message\ServerRequest;
use WellRESTed\Routing\MethodMap; use WellRESTed\Routing\MethodMap;
/** /**
* @coversDefaultClass WellRESTed\Routing\MethodMap * @covers WellRESTed\Routing\MethodMap
* @uses WellRESTed\Routing\MethodMap
* @uses WellRESTed\Dispatching\Dispatcher
* @group routing * @group routing
*/ */
class MethodMapTest extends \PHPUnit_Framework_TestCase class MethodMapTest extends \PHPUnit_Framework_TestCase
@ -21,7 +20,7 @@ class MethodMapTest extends \PHPUnit_Framework_TestCase
public function setUp() public function setUp()
{ {
$this->request = $this->prophesize('Psr\Http\Message\ServerRequestInterface'); $this->request = new ServerRequest();
$this->response = $this->prophesize('Psr\Http\Message\ResponseInterface'); $this->response = $this->prophesize('Psr\Http\Message\ResponseInterface');
$this->response->withStatus(Argument::any())->willReturn($this->response->reveal()); $this->response->withStatus(Argument::any())->willReturn($this->response->reveal());
$this->response->withHeader(Argument::cetera())->willReturn($this->response->reveal()); $this->response->withHeader(Argument::cetera())->willReturn($this->response->reveal());
@ -39,41 +38,30 @@ class MethodMapTest extends \PHPUnit_Framework_TestCase
); );
} }
/**
* @covers ::__construct
*/
public function testCreatesInstance() public function testCreatesInstance()
{ {
$methodMap = new MethodMap($this->dispatcher->reveal()); $methodMap = new MethodMap($this->dispatcher->reveal());
$this->assertNotNull($methodMap); $this->assertNotNull($methodMap);
} }
/**
* @covers ::__invoke
* @covers ::dispatchMiddleware
* @covers ::register
*/
public function testDispatchesMiddlewareWithMatchingMethod() public function testDispatchesMiddlewareWithMatchingMethod()
{ {
$this->request->getMethod()->willReturn("GET"); $this->request = $this->request->withMethod("GET");
$map = new MethodMap($this->dispatcher->reveal()); $map = new MethodMap($this->dispatcher->reveal());
$map->register("GET", $this->middleware->reveal()); $map->register("GET", $this->middleware->reveal());
$map($this->request->reveal(), $this->response->reveal(), $this->next); $map($this->request, $this->response->reveal(), $this->next);
$this->middleware->__invoke( $this->middleware->__invoke(
$this->request->reveal(), $this->request,
$this->response->reveal(), $this->response->reveal(),
$this->next $this->next
)->shouldHaveBeenCalled(); )->shouldHaveBeenCalled();
} }
/**
* @coversNothing
*/
public function testTreatsMethodNamesCaseSensitively() public function testTreatsMethodNamesCaseSensitively()
{ {
$this->request->getMethod()->willReturn("get"); $this->request = $this->request->withMethod("get");
$middlewareUpper = $this->prophesize('WellRESTed\MiddlewareInterface'); $middlewareUpper = $this->prophesize('WellRESTed\MiddlewareInterface');
$middlewareUpper->__invoke(Argument::cetera())->willReturn(); $middlewareUpper->__invoke(Argument::cetera())->willReturn();
@ -84,98 +72,78 @@ class MethodMapTest extends \PHPUnit_Framework_TestCase
$map = new MethodMap($this->dispatcher->reveal()); $map = new MethodMap($this->dispatcher->reveal());
$map->register("GET", $middlewareUpper->reveal()); $map->register("GET", $middlewareUpper->reveal());
$map->register("get", $middlewareLower->reveal()); $map->register("get", $middlewareLower->reveal());
$map($this->request->reveal(), $this->response->reveal(), $this->next); $map($this->request, $this->response->reveal(), $this->next);
$middlewareLower->__invoke( $middlewareLower->__invoke(
$this->request->reveal(), $this->request,
$this->response->reveal(), $this->response->reveal(),
$this->next $this->next
)->shouldHaveBeenCalled(); )->shouldHaveBeenCalled();
} }
/**
* @covers ::__invoke
* @covers ::dispatchMiddleware
* @covers ::register
*/
public function testDispatchesWildcardMiddlewareWithNonMatchingMethod() public function testDispatchesWildcardMiddlewareWithNonMatchingMethod()
{ {
$this->request->getMethod()->willReturn("GET"); $this->request = $this->request->withMethod("GET");
$map = new MethodMap($this->dispatcher->reveal()); $map = new MethodMap($this->dispatcher->reveal());
$map->register("*", $this->middleware->reveal()); $map->register("*", $this->middleware->reveal());
$map($this->request->reveal(), $this->response->reveal(), $this->next); $map($this->request, $this->response->reveal(), $this->next);
$this->middleware->__invoke( $this->middleware->__invoke(
$this->request->reveal(), $this->request,
$this->response->reveal(), $this->response->reveal(),
$this->next $this->next
)->shouldHaveBeenCalled(); )->shouldHaveBeenCalled();
} }
/**
* @covers ::__invoke
*/
public function testDispatchesGetMiddlewareForHeadByDefault() public function testDispatchesGetMiddlewareForHeadByDefault()
{ {
$this->request->getMethod()->willReturn("HEAD"); $this->request = $this->request->withMethod("HEAD");
$map = new MethodMap($this->dispatcher->reveal()); $map = new MethodMap($this->dispatcher->reveal());
$map->register("GET", $this->middleware->reveal()); $map->register("GET", $this->middleware->reveal());
$map($this->request->reveal(), $this->response->reveal(), $this->next); $map($this->request, $this->response->reveal(), $this->next);
$this->middleware->__invoke( $this->middleware->__invoke(
$this->request->reveal(), $this->request,
$this->response->reveal(), $this->response->reveal(),
$this->next $this->next
)->shouldHaveBeenCalled(); )->shouldHaveBeenCalled();
} }
/**
* @covers ::register
*/
public function testRegistersMiddlewareForMultipleMethods() public function testRegistersMiddlewareForMultipleMethods()
{ {
$map = new MethodMap($this->dispatcher->reveal()); $map = new MethodMap($this->dispatcher->reveal());
$map->register("GET,POST", $this->middleware->reveal()); $map->register("GET,POST", $this->middleware->reveal());
$this->request->getMethod()->willReturn("GET"); $this->request = $this->request->withMethod("GET");
$map($this->request->reveal(), $this->response->reveal(), $this->next); $map($this->request, $this->response->reveal(), $this->next);
$this->request->getMethod()->willReturn("POST"); $this->request = $this->request->withMethod("POST");
$map($this->request->reveal(), $this->response->reveal(), $this->next); $map($this->request, $this->response->reveal(), $this->next);
$this->middleware->__invoke( $this->middleware->__invoke(Argument::cetera())->shouldHaveBeenCalledTimes(2);
$this->request->reveal(),
$this->response->reveal(),
$this->next
)->shouldHaveBeenCalledTimes(2);
} }
public function testSettingNullUnregistersMiddleware() public function testSettingNullUnregistersMiddleware()
{ {
$this->request->getMethod()->willReturn("POST"); $this->request = $this->request->withMethod("POST");
$map = new MethodMap($this->dispatcher->reveal()); $map = new MethodMap($this->dispatcher->reveal());
$map->register("POST", $this->middleware->reveal()); $map->register("POST", $this->middleware->reveal());
$map->register("POST", null); $map->register("POST", null);
$map($this->request->reveal(), $this->response->reveal(), $this->next); $map($this->request, $this->response->reveal(), $this->next);
$this->response->withStatus(405)->shouldHaveBeenCalled(); $this->response->withStatus(405)->shouldHaveBeenCalled();
} }
/**
* @covers ::__invoke
* @covers ::addAllowHeader
* @covers ::getAllowedMethods
*/
public function testSetsStatusTo200ForOptions() public function testSetsStatusTo200ForOptions()
{ {
$this->request->getMethod()->willReturn("OPTIONS"); $this->request = $this->request->withMethod("OPTIONS");
$map = new MethodMap($this->dispatcher->reveal()); $map = new MethodMap($this->dispatcher->reveal());
$map->register("GET", $this->middleware->reveal()); $map->register("GET", $this->middleware->reveal());
$map($this->request->reveal(), $this->response->reveal(), $this->next); $map($this->request, $this->response->reveal(), $this->next);
$this->response->withStatus(200)->shouldHaveBeenCalled(); $this->response->withStatus(200)->shouldHaveBeenCalled();
} }
@ -188,30 +156,25 @@ class MethodMapTest extends \PHPUnit_Framework_TestCase
return $response; return $response;
}; };
$this->request->getMethod()->willReturn("OPTIONS"); $this->request = $this->request->withMethod("OPTIONS");
$map = new MethodMap($this->dispatcher->reveal()); $map = new MethodMap($this->dispatcher->reveal());
$map->register("GET", $this->middleware->reveal()); $map->register("GET", $this->middleware->reveal());
$map($this->request->reveal(), $this->response->reveal(), $next); $map($this->request, $this->response->reveal(), $next);
$this->assertFalse($calledNext); $this->assertFalse($calledNext);
} }
/** /** @dataProvider allowedMethodProvider */
* @covers ::__invoke
* @covers ::addAllowHeader
* @covers ::getAllowedMethods
* @dataProvider allowedMethodProvider
*/
public function testSetsAllowHeaderForOptions($methodsDeclared, $methodsAllowed) public function testSetsAllowHeaderForOptions($methodsDeclared, $methodsAllowed)
{ {
$this->request->getMethod()->willReturn("OPTIONS"); $this->request = $this->request->withMethod("OPTIONS");
$map = new MethodMap($this->dispatcher->reveal()); $map = new MethodMap($this->dispatcher->reveal());
foreach ($methodsDeclared as $method) { foreach ($methodsDeclared as $method) {
$map->register($method, $this->middleware->reveal()); $map->register($method, $this->middleware->reveal());
} }
$map($this->request->reveal(), $this->response->reveal(), $this->next); $map($this->request, $this->response->reveal(), $this->next);
$containsAllMethods = function ($headerValue) use ($methodsAllowed) { $containsAllMethods = function ($headerValue) use ($methodsAllowed) {
foreach ($methodsAllowed as $method) { foreach ($methodsAllowed as $method) {
@ -224,19 +187,14 @@ class MethodMapTest extends \PHPUnit_Framework_TestCase
$this->response->withHeader("Allow", Argument::that($containsAllMethods))->shouldHaveBeenCalled(); $this->response->withHeader("Allow", Argument::that($containsAllMethods))->shouldHaveBeenCalled();
} }
/** /** @dataProvider allowedMethodProvider */
* @covers ::__invoke
* @covers ::addAllowHeader
* @covers ::getAllowedMethods
* @dataProvider allowedMethodProvider
*/
public function testSetsStatusTo405ForBadMethod() public function testSetsStatusTo405ForBadMethod()
{ {
$this->request->getMethod()->willReturn("POST"); $this->request = $this->request->withMethod("POST");
$map = new MethodMap($this->dispatcher->reveal()); $map = new MethodMap($this->dispatcher->reveal());
$map->register("GET", $this->middleware->reveal()); $map->register("GET", $this->middleware->reveal());
$map($this->request->reveal(), $this->response->reveal(), $this->next); $map($this->request, $this->response->reveal(), $this->next);
$this->response->withStatus(405)->shouldHaveBeenCalled(); $this->response->withStatus(405)->shouldHaveBeenCalled();
} }
@ -252,29 +210,24 @@ class MethodMapTest extends \PHPUnit_Framework_TestCase
$calledNext = true; $calledNext = true;
return $response; return $response;
}; };
$this->request->getMethod()->willReturn("POST"); $this->request = $this->request->withMethod("POST");
$map = new MethodMap($this->dispatcher->reveal()); $map = new MethodMap($this->dispatcher->reveal());
$map->register("GET", $this->middleware->reveal()); $map->register("GET", $this->middleware->reveal());
$map($this->request->reveal(), $this->response->reveal(), $next); $map($this->request, $this->response->reveal(), $next);
$this->assertFalse($calledNext); $this->assertFalse($calledNext);
} }
/** /** @dataProvider allowedMethodProvider */
* @covers ::__invoke
* @covers ::addAllowHeader
* @covers ::getAllowedMethods
* @dataProvider allowedMethodProvider
*/
public function testSetsAllowHeaderForBadMethod($methodsDeclared, $methodsAllowed) public function testSetsAllowHeaderForBadMethod($methodsDeclared, $methodsAllowed)
{ {
$this->request->getMethod()->willReturn("BAD"); $this->request = $this->request->withMethod("BAD");
$map = new MethodMap($this->dispatcher->reveal()); $map = new MethodMap($this->dispatcher->reveal());
foreach ($methodsDeclared as $method) { foreach ($methodsDeclared as $method) {
$map->register($method, $this->middleware->reveal()); $map->register($method, $this->middleware->reveal());
} }
$map($this->request->reveal(), $this->response->reveal(), $this->next); $map($this->request, $this->response->reveal(), $this->next);
$containsAllMethods = function ($headerValue) use ($methodsAllowed) { $containsAllMethods = function ($headerValue) use ($methodsAllowed) {
foreach ($methodsAllowed as $method) { foreach ($methodsAllowed as $method) {