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

View File

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