MethodMap tests use Dispatcher and MiddlewareMock; rename NextMock

This commit is contained in:
PJ Dietz 2016-05-21 10:16:22 -04:00
parent f9ab311b79
commit 91249d885f
5 changed files with 82 additions and 74 deletions

View File

@ -0,0 +1,32 @@
<?php
namespace WellRESTed\Test;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use WellRESTed\MiddlewareInterface;
class MiddlewareMock implements MiddlewareInterface
{
public $called = false;
public $callCount = 0;
public $request = null;
public $response = null;
public $propagate = true;
public function __invoke(
ServerRequestInterface $request,
ResponseInterface $response,
$next
) {
$this->called = true;
$this->callCount++;
$this->request = $request;
$this->response = $response;
if ($this->propagate) {
return $next($request, $response);
} else {
return $response;
}
}
}

View File

@ -5,7 +5,7 @@ namespace WellRESTed\Test;
use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Message\ServerRequestInterface;
class NextSpy class NextMock
{ {
public $called = false; public $called = false;
public $request = null; public $request = null;
@ -13,11 +13,11 @@ class NextSpy
public function __invoke( public function __invoke(
ServerRequestInterface $request, ServerRequestInterface $request,
ResponseInterface $respone ResponseInterface $response
) { ) {
$this->called = true; $this->called = true;
$this->request = $request; $this->request = $request;
$this->response = $respone; $this->response = $response;
return $respone; return $response;
} }
} }

View File

@ -6,7 +6,7 @@ use WellRESTed\Dispatching\Dispatcher;
use WellRESTed\Dispatching\DispatchStack; use WellRESTed\Dispatching\DispatchStack;
use WellRESTed\Message\Response; use WellRESTed\Message\Response;
use WellRESTed\Message\ServerRequest; use WellRESTed\Message\ServerRequest;
use WellRESTed\Test\NextSpy; use WellRESTed\Test\NextMock;
/** /**
* @covers WellRESTed\Dispatching\DispatchStack * @covers WellRESTed\Dispatching\DispatchStack
@ -23,7 +23,7 @@ class DispatchStackTest extends \PHPUnit_Framework_TestCase
parent::setUp(); parent::setUp();
$this->request = new ServerRequest(); $this->request = new ServerRequest();
$this->response = new Response(); $this->response = new Response();
$this->next = new NextSpy(); $this->next = new NextMock();
} }
public function testDispatchesMiddlewareInOrderAdded() public function testDispatchesMiddlewareInOrderAdded()

View File

@ -9,7 +9,7 @@ use WellRESTed\Dispatching\Dispatcher;
use WellRESTed\Message\Response; use WellRESTed\Message\Response;
use WellRESTed\Message\ServerRequest; use WellRESTed\Message\ServerRequest;
use WellRESTed\MiddlewareInterface; use WellRESTed\MiddlewareInterface;
use WellRESTed\Test\NextSpy; use WellRESTed\Test\NextMock;
/** /**
* @covers WellRESTed\Dispatching\Dispatcher * @covers WellRESTed\Dispatching\Dispatcher
@ -25,7 +25,7 @@ class DispatcherTest extends \PHPUnit_Framework_TestCase
{ {
$this->request = new ServerRequest(); $this->request = new ServerRequest();
$this->response = new Response(); $this->response = new Response();
$this->next = new NextSpy(); $this->next = new NextMock();
} }
public function testDispatchesCallableThatReturnsResponse() public function testDispatchesCallableThatReturnsResponse()

View File

@ -3,10 +3,12 @@
namespace WellRESTed\Test\Unit\Routing; namespace WellRESTed\Test\Unit\Routing;
use Prophecy\Argument; use Prophecy\Argument;
use WellRESTed\Dispatching\Dispatcher;
use WellRESTed\Message\Response; use WellRESTed\Message\Response;
use WellRESTed\Message\ServerRequest; use WellRESTed\Message\ServerRequest;
use WellRESTed\Routing\MethodMap; use WellRESTed\Routing\MethodMap;
use WellRESTed\Test\NextSpy; use WellRESTed\Test\MiddlewareMock;
use WellRESTed\Test\NextMock;
/** /**
* @covers WellRESTed\Routing\MethodMap * @covers WellRESTed\Routing\MethodMap
@ -24,95 +26,69 @@ class MethodMapTest extends \PHPUnit_Framework_TestCase
{ {
$this->request = new ServerRequest(); $this->request = new ServerRequest();
$this->response = new Response(); $this->response = new Response();
$this->next = new NextSpy(); $this->next = new NextMock();
$this->middleware = $this->prophesize('WellRESTed\MiddlewareInterface'); $this->middleware = new MiddlewareMock();
$this->middleware->__invoke(Argument::cetera())->willReturn(); $this->dispatcher = new Dispatcher();
$this->dispatcher = $this->prophesize('WellRESTed\Dispatching\DispatcherInterface');
$this->dispatcher->dispatch(Argument::cetera())->will(
function ($args) {
list($middleware, $request, $response, $next) = $args;
return $middleware($request, $response, $next);
}
);
} }
public function testCreatesInstance() private function getMethodMap() {
{ return new MethodMap($this->dispatcher);
$methodMap = new MethodMap($this->dispatcher->reveal());
$this->assertNotNull($methodMap);
} }
// -------------------------------------------------------------------------
public function testDispatchesMiddlewareWithMatchingMethod() public function testDispatchesMiddlewareWithMatchingMethod()
{ {
$this->request = $this->request->withMethod("GET"); $this->request = $this->request->withMethod("GET");
$map = new MethodMap($this->dispatcher->reveal()); $map = $this->getMethodMap();
$map->register("GET", $this->middleware->reveal()); $map->register("GET", $this->middleware);
$map($this->request, $this->response, $this->next); $map($this->request, $this->response, $this->next);
$this->middleware->__invoke( $this->assertTrue($this->middleware->called);
$this->request,
$this->response,
$this->next
)->shouldHaveBeenCalled();
} }
public function testTreatsMethodNamesCaseSensitively() public function testTreatsMethodNamesCaseSensitively()
{ {
$this->request = $this->request->withMethod("get"); $this->request = $this->request->withMethod("get");
$middlewareUpper = $this->prophesize('WellRESTed\MiddlewareInterface'); $middlewareUpper = new MiddlewareMock();
$middlewareUpper->__invoke(Argument::cetera())->willReturn(); $middlewareLower = new MiddlewareMock();
$middlewareLower = $this->prophesize('WellRESTed\MiddlewareInterface'); $map = $this->getMethodMap();
$middlewareLower->__invoke(Argument::cetera())->willReturn(); $map->register("GET", $middlewareUpper);
$map->register("get", $middlewareLower);
$map = new MethodMap($this->dispatcher->reveal());
$map->register("GET", $middlewareUpper->reveal());
$map->register("get", $middlewareLower->reveal());
$map($this->request, $this->response, $this->next); $map($this->request, $this->response, $this->next);
$middlewareLower->__invoke( $this->assertTrue($middlewareLower->called);
$this->request,
$this->response,
$this->next
)->shouldHaveBeenCalled();
} }
public function testDispatchesWildcardMiddlewareWithNonMatchingMethod() public function testDispatchesWildcardMiddlewareWithNonMatchingMethod()
{ {
$this->request = $this->request->withMethod("GET"); $this->request = $this->request->withMethod("GET");
$map = new MethodMap($this->dispatcher->reveal()); $map = $this->getMethodMap();
$map->register("*", $this->middleware->reveal()); $map->register("*", $this->middleware);
$map($this->request, $this->response, $this->next); $map($this->request, $this->response, $this->next);
$this->middleware->__invoke( $this->assertTrue($this->middleware->called);
$this->request,
$this->response,
$this->next
)->shouldHaveBeenCalled();
} }
public function testDispatchesGetMiddlewareForHeadByDefault() public function testDispatchesGetMiddlewareForHeadByDefault()
{ {
$this->request = $this->request->withMethod("HEAD"); $this->request = $this->request->withMethod("HEAD");
$map = new MethodMap($this->dispatcher->reveal()); $map = $this->getMethodMap();
$map->register("GET", $this->middleware->reveal()); $map->register("GET", $this->middleware);
$map($this->request, $this->response, $this->next); $map($this->request, $this->response, $this->next);
$this->middleware->__invoke( $this->assertTrue($this->middleware->called);
$this->request,
$this->response,
$this->next
)->shouldHaveBeenCalled();
} }
public function testRegistersMiddlewareForMultipleMethods() public function testRegistersMiddlewareForMultipleMethods()
{ {
$map = new MethodMap($this->dispatcher->reveal()); $map = $this->getMethodMap();
$map->register("GET,POST", $this->middleware->reveal()); $map->register("GET,POST", $this->middleware);
$this->request = $this->request->withMethod("GET"); $this->request = $this->request->withMethod("GET");
$map($this->request, $this->response, $this->next); $map($this->request, $this->response, $this->next);
@ -120,15 +96,15 @@ class MethodMapTest extends \PHPUnit_Framework_TestCase
$this->request = $this->request->withMethod("POST"); $this->request = $this->request->withMethod("POST");
$map($this->request, $this->response, $this->next); $map($this->request, $this->response, $this->next);
$this->middleware->__invoke(Argument::cetera())->shouldHaveBeenCalledTimes(2); $this->assertEquals(2, $this->middleware->callCount);
} }
public function testSettingNullUnregistersMiddleware() public function testSettingNullUnregistersMiddleware()
{ {
$this->request = $this->request->withMethod("POST"); $this->request = $this->request->withMethod("POST");
$map = new MethodMap($this->dispatcher->reveal()); $map = $this->getMethodMap();
$map->register("POST", $this->middleware->reveal()); $map->register("POST", $this->middleware);
$map->register("POST", null); $map->register("POST", null);
$response = $map($this->request, $this->response, $this->next); $response = $map($this->request, $this->response, $this->next);
@ -139,8 +115,8 @@ class MethodMapTest extends \PHPUnit_Framework_TestCase
{ {
$this->request = $this->request->withMethod("OPTIONS"); $this->request = $this->request->withMethod("OPTIONS");
$map = new MethodMap($this->dispatcher->reveal()); $map = $this->getMethodMap();
$map->register("GET", $this->middleware->reveal()); $map->register("GET", $this->middleware);
$response = $map($this->request, $this->response, $this->next); $response = $map($this->request, $this->response, $this->next);
$this->assertEquals(200, $response->getStatusCode()); $this->assertEquals(200, $response->getStatusCode());
@ -150,8 +126,8 @@ class MethodMapTest extends \PHPUnit_Framework_TestCase
{ {
$this->request = $this->request->withMethod("OPTIONS"); $this->request = $this->request->withMethod("OPTIONS");
$map = new MethodMap($this->dispatcher->reveal()); $map = $this->getMethodMap();
$map->register("GET", $this->middleware->reveal()); $map->register("GET", $this->middleware);
$map($this->request, $this->response, $this->next); $map($this->request, $this->response, $this->next);
$this->assertFalse($this->next->called); $this->assertFalse($this->next->called);
@ -162,9 +138,9 @@ class MethodMapTest extends \PHPUnit_Framework_TestCase
{ {
$this->request = $this->request->withMethod("OPTIONS"); $this->request = $this->request->withMethod("OPTIONS");
$map = new MethodMap($this->dispatcher->reveal()); $map = $this->getMethodMap();
foreach ($methodsDeclared as $method) { foreach ($methodsDeclared as $method) {
$map->register($method, $this->middleware->reveal()); $map->register($method, $this->middleware);
} }
$response = $map($this->request, $this->response, $this->next); $response = $map($this->request, $this->response, $this->next);
@ -176,8 +152,8 @@ class MethodMapTest extends \PHPUnit_Framework_TestCase
{ {
$this->request = $this->request->withMethod("POST"); $this->request = $this->request->withMethod("POST");
$map = new MethodMap($this->dispatcher->reveal()); $map = $this->getMethodMap();
$map->register("GET", $this->middleware->reveal()); $map->register("GET", $this->middleware);
$response = $map($this->request, $this->response, $this->next); $response = $map($this->request, $this->response, $this->next);
$this->assertEquals(405, $response->getStatusCode()); $this->assertEquals(405, $response->getStatusCode());
@ -191,8 +167,8 @@ class MethodMapTest extends \PHPUnit_Framework_TestCase
{ {
$this->request = $this->request->withMethod("POST"); $this->request = $this->request->withMethod("POST");
$map = new MethodMap($this->dispatcher->reveal()); $map = $this->getMethodMap();
$map->register("GET", $this->middleware->reveal()); $map->register("GET", $this->middleware);
$map($this->request, $this->response, $this->next); $map($this->request, $this->response, $this->next);
$this->assertFalse($this->next->called); $this->assertFalse($this->next->called);
} }
@ -202,9 +178,9 @@ class MethodMapTest extends \PHPUnit_Framework_TestCase
{ {
$this->request = $this->request->withMethod("BAD"); $this->request = $this->request->withMethod("BAD");
$map = new MethodMap($this->dispatcher->reveal()); $map = $this->getMethodMap();
foreach ($methodsDeclared as $method) { foreach ($methodsDeclared as $method) {
$map->register($method, $this->middleware->reveal()); $map->register($method, $this->middleware);
} }
$response = $map($this->request, $this->response, $this->next); $response = $map($this->request, $this->response, $this->next);