Refactor dispatch tests

This commit is contained in:
PJ Dietz 2016-05-20 19:17:20 -04:00
parent f48b3c5fd1
commit d3e924485c
4 changed files with 58 additions and 94 deletions

View File

@ -2,4 +2,5 @@
error_reporting(E_ALL); error_reporting(E_ALL);
require_once __DIR__ . "/../vendor/autoload.php"; $loader = require __DIR__ . '/../vendor/autoload.php';
$loader->addPsr4('WellRESTed\\Test\\', __DIR__ . '/src');

23
test/src/NextSpy.php Normal file
View File

@ -0,0 +1,23 @@
<?php
namespace WellRESTed\Test;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
class NextSpy
{
public $called = false;
public $request = null;
public $response = null;
public function __invoke(
ServerRequestInterface $request,
ResponseInterface $respone
) {
$this->called = true;
$this->request = $request;
$this->response = $respone;
return $respone;
}
}

View File

@ -2,12 +2,14 @@
namespace WellRESTed\Test\Unit\Dispatching; namespace WellRESTed\Test\Unit\Dispatching;
use Prophecy\Argument; use WellRESTed\Dispatching\Dispatcher;
use WellRESTed\Dispatching\DispatchStack; use WellRESTed\Dispatching\DispatchStack;
use WellRESTed\Message\Response;
use WellRESTed\Message\ServerRequest;
use WellRESTed\Test\NextSpy;
/** /**
* @coversDefaultClass WellRESTed\Dispatching\DispatchStack * @covers WellRESTed\Dispatching\DispatchStack
* @uses WellRESTed\Dispatching\DispatchStack
* @group dispatching * @group dispatching
*/ */
class DispatchStackTest extends \PHPUnit_Framework_TestCase class DispatchStackTest extends \PHPUnit_Framework_TestCase
@ -15,50 +17,20 @@ class DispatchStackTest extends \PHPUnit_Framework_TestCase
private $request; private $request;
private $response; private $response;
private $next; private $next;
private $dispatcher;
public function setUp() public function setUp()
{ {
parent::setUp(); parent::setUp();
$this->request = $this->prophesize('Psr\Http\Message\ServerRequestInterface'); $this->request = new ServerRequest();
$this->response = $this->prophesize('Psr\Http\Message\ResponseInterface'); $this->response = new Response();
$this->next = function ($request, $response) { $this->next = new NextSpy();
return $response;
};
$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 testDispatchesMiddlewareInOrderAdded()
* @covers ::__construct
*/
public function testCreatesInstance()
{ {
$stack = new DispatchStack($this->dispatcher->reveal()); // Each middleware will add its "name" to this array.
$this->assertNotNull($stack);
}
/**
* @covers ::add
*/
public function testAddIsFluid()
{
$stack = new DispatchStack($this->dispatcher->reveal());
$this->assertSame($stack, $stack->add("middleware1"));
}
/**
* @covers ::__invoke
*/
public function testDispachesMiddlewareInOrderAdded()
{
// Each middelware will add its "name" to this array.
$callOrder = []; $callOrder = [];
$stack = new DispatchStack(new Dispatcher());
$stack = new DispatchStack($this->dispatcher->reveal());
$stack->add(function ($request, $response, $next) use (&$callOrder) { $stack->add(function ($request, $response, $next) use (&$callOrder) {
$callOrder[] = "first"; $callOrder[] = "first";
return $next($request, $response); return $next($request, $response);
@ -71,61 +43,34 @@ class DispatchStackTest extends \PHPUnit_Framework_TestCase
$callOrder[] = "third"; $callOrder[] = "third";
return $next($request, $response); return $next($request, $response);
}); });
$stack($this->request->reveal(), $this->response->reveal(), $this->next); $stack($this->request, $this->response, $this->next);
$this->assertEquals(["first", "second", "third"], $callOrder); $this->assertEquals(["first", "second", "third"], $callOrder);
} }
/**
* @covers ::__invoke
*/
public function testCallsNextAfterDispatchingEmptyStack() public function testCallsNextAfterDispatchingEmptyStack()
{ {
$nextCalled = false; $stack = new DispatchStack(new Dispatcher());
$next = function ($request, $response) use (&$nextCalled) { $stack($this->request, $this->response, $this->next);
$nextCalled = true; $this->assertTrue($this->next->called);
return $response;
};
$stack = new DispatchStack($this->dispatcher->reveal());
$stack($this->request->reveal(), $this->response->reveal(), $next);
$this->assertTrue($nextCalled);
} }
/**
* @covers ::__invoke
*/
public function testCallsNextAfterDispatchingStack() public function testCallsNextAfterDispatchingStack()
{ {
$nextCalled = false;
$next = function ($request, $response) use (&$nextCalled) {
$nextCalled = true;
return $response;
};
$middleware = function ($request, $response, $next) use (&$callOrder) { $middleware = function ($request, $response, $next) use (&$callOrder) {
return $next($request, $response); return $next($request, $response);
}; };
$stack = new DispatchStack($this->dispatcher->reveal()); $stack = new DispatchStack(new Dispatcher());
$stack->add($middleware); $stack->add($middleware);
$stack->add($middleware); $stack->add($middleware);
$stack->add($middleware); $stack->add($middleware);
$stack($this->request->reveal(), $this->response->reveal(), $next); $stack($this->request, $this->response, $this->next);
$this->assertTrue($nextCalled); $this->assertTrue($this->next->called);
} }
/**
* @covers ::__invoke
*/
public function testDoesNotCallNextWhenStackStopsEarly() public function testDoesNotCallNextWhenStackStopsEarly()
{ {
$nextCalled = false;
$next = function ($request, $response) use (&$nextCalled) {
$nextCalled = true;
return $response;
};
$middlewareGo = function ($request, $response, $next) use (&$callOrder) { $middlewareGo = function ($request, $response, $next) use (&$callOrder) {
return $next($request, $response); return $next($request, $response);
}; };
@ -133,12 +78,12 @@ class DispatchStackTest extends \PHPUnit_Framework_TestCase
return $response; return $response;
}; };
$stack = new DispatchStack($this->dispatcher->reveal()); $stack = new DispatchStack(new Dispatcher());
$stack->add($middlewareGo); $stack->add($middlewareGo);
$stack->add($middlewareStop); $stack->add($middlewareStop);
$stack->add($middlewareStop); $stack->add($middlewareStop);
$stack($this->request->reveal(), $this->response->reveal(), $next); $stack($this->request, $this->response, $this->next);
$this->assertFalse($nextCalled); $this->assertFalse($this->next->called);
} }
} }

View File

@ -6,7 +6,10 @@ use Prophecy\Argument;
use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Message\ServerRequestInterface;
use WellRESTed\Dispatching\Dispatcher; use WellRESTed\Dispatching\Dispatcher;
use WellRESTed\Message\Response;
use WellRESTed\Message\ServerRequest;
use WellRESTed\MiddlewareInterface; use WellRESTed\MiddlewareInterface;
use WellRESTed\Test\NextSpy;
/** /**
* @covers WellRESTed\Dispatching\Dispatcher * @covers WellRESTed\Dispatching\Dispatcher
@ -20,17 +23,9 @@ class DispatcherTest 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 = new Response();
$this->response->withStatus(Argument::any())->will( $this->next = new NextSpy();
function ($args) {
$this->getStatusCode()->willReturn($args[0]);
return $this;
}
);
$this->next = function ($request, $response) {
return $response;
};
} }
public function testDispatchesCallableThatReturnsResponse() public function testDispatchesCallableThatReturnsResponse()
@ -40,7 +35,7 @@ class DispatcherTest extends \PHPUnit_Framework_TestCase
}; };
$dispatcher = new Dispatcher(); $dispatcher = new Dispatcher();
$response = $dispatcher->dispatch($middleware, $this->request->reveal(), $this->response->reveal(), $this->next); $response = $dispatcher->dispatch($middleware, $this->request, $this->response, $this->next);
$this->assertEquals(200, $response->getStatusCode()); $this->assertEquals(200, $response->getStatusCode());
} }
@ -51,7 +46,7 @@ class DispatcherTest extends \PHPUnit_Framework_TestCase
}; };
$dispatcher = new Dispatcher(); $dispatcher = new Dispatcher();
$response = $dispatcher->dispatch($middleware, $this->request->reveal(), $this->response->reveal(), $this->next); $response = $dispatcher->dispatch($middleware, $this->request, $this->response, $this->next);
$this->assertEquals(200, $response->getStatusCode()); $this->assertEquals(200, $response->getStatusCode());
} }
@ -60,7 +55,7 @@ class DispatcherTest extends \PHPUnit_Framework_TestCase
$middleware = __NAMESPACE__ . '\DispatcherTest_Middleware'; $middleware = __NAMESPACE__ . '\DispatcherTest_Middleware';
$dispatcher = new Dispatcher(); $dispatcher = new Dispatcher();
$response = $dispatcher->dispatch($middleware, $this->request->reveal(), $this->response->reveal(), $this->next); $response = $dispatcher->dispatch($middleware, $this->request, $this->response, $this->next);
$this->assertEquals(200, $response->getStatusCode()); $this->assertEquals(200, $response->getStatusCode());
} }
@ -69,7 +64,7 @@ class DispatcherTest extends \PHPUnit_Framework_TestCase
$middleware = new DispatcherTest_Middleware(); $middleware = new DispatcherTest_Middleware();
$dispatcher = new Dispatcher(); $dispatcher = new Dispatcher();
$response = $dispatcher->dispatch($middleware, $this->request->reveal(), $this->response->reveal(), $this->next); $response = $dispatcher->dispatch($middleware, $this->request, $this->response, $this->next);
$this->assertEquals(200, $response->getStatusCode()); $this->assertEquals(200, $response->getStatusCode());
} }
@ -81,7 +76,7 @@ class DispatcherTest extends \PHPUnit_Framework_TestCase
$middleware = new DispatcherTest_Middleware(); $middleware = new DispatcherTest_Middleware();
$dispatcher = new Dispatcher(); $dispatcher = new Dispatcher();
$response = $dispatcher->dispatch([$middleware], $this->request->reveal(), $this->response->reveal(), $this->next); $response = $dispatcher->dispatch([$middleware], $this->request, $this->response, $this->next);
$this->assertEquals(200, $response->getStatusCode()); $this->assertEquals(200, $response->getStatusCode());
} }
@ -93,7 +88,7 @@ class DispatcherTest extends \PHPUnit_Framework_TestCase
$middleware = null; $middleware = null;
$dispatcher = new Dispatcher(); $dispatcher = new Dispatcher();
$dispatcher->dispatch($middleware, $this->request->reveal(), $this->response->reveal(), $this->next); $dispatcher->dispatch($middleware, $this->request, $this->response, $this->next);
} }
} }