Refactor Server test
This commit is contained in:
parent
4fb7bf6050
commit
e0b5c836db
|
|
@ -3,15 +3,16 @@
|
||||||
namespace WellRESTed\Test\Unit\Server;
|
namespace WellRESTed\Test\Unit\Server;
|
||||||
|
|
||||||
use Prophecy\Argument;
|
use Prophecy\Argument;
|
||||||
|
use WellRESTed\Message\Response;
|
||||||
|
use WellRESTed\Message\ServerRequest;
|
||||||
use WellRESTed\Server;
|
use WellRESTed\Server;
|
||||||
|
use WellRESTed\Test\NextMock;
|
||||||
|
|
||||||
/**
|
/** @covers WellRESTed\Server */
|
||||||
* @coversDefaultClass WellRESTed\Server
|
|
||||||
* @uses WellRESTed\Server
|
|
||||||
*/
|
|
||||||
class ServerTest extends \PHPUnit_Framework_TestCase
|
class ServerTest extends \PHPUnit_Framework_TestCase
|
||||||
{
|
{
|
||||||
private $dispatcher;
|
private $dispatcher;
|
||||||
|
private $next;
|
||||||
private $request;
|
private $request;
|
||||||
private $response;
|
private $response;
|
||||||
private $transmitter;
|
private $transmitter;
|
||||||
|
|
@ -20,9 +21,10 @@ class ServerTest extends \PHPUnit_Framework_TestCase
|
||||||
public function setUp()
|
public function setUp()
|
||||||
{
|
{
|
||||||
parent::setUp();
|
parent::setUp();
|
||||||
$this->request = $this->prophesize('Psr\Http\Message\ServerRequestInterface');
|
$this->request = new ServerRequest();
|
||||||
$this->request->withAttribute(Argument::cetera())->willReturn($this->request->reveal());
|
$this->response = new Response();
|
||||||
$this->response = $this->prophesize('Psr\Http\Message\ResponseInterface');
|
$this->next = new NextMock();
|
||||||
|
|
||||||
$this->transmitter = $this->prophesize('WellRESTed\Transmission\TransmitterInterface');
|
$this->transmitter = $this->prophesize('WellRESTed\Transmission\TransmitterInterface');
|
||||||
$this->transmitter->transmit(Argument::cetera())->willReturn();
|
$this->transmitter->transmit(Argument::cetera())->willReturn();
|
||||||
$this->dispatcher = $this->prophesize('WellRESTed\Dispatching\DispatcherInterface');
|
$this->dispatcher = $this->prophesize('WellRESTed\Dispatching\DispatcherInterface');
|
||||||
|
|
@ -42,149 +44,107 @@ class ServerTest extends \PHPUnit_Framework_TestCase
|
||||||
->will($this->returnValue($this->dispatcher->reveal()));
|
->will($this->returnValue($this->dispatcher->reveal()));
|
||||||
$this->server->expects($this->any())
|
$this->server->expects($this->any())
|
||||||
->method("getRequest")
|
->method("getRequest")
|
||||||
->will($this->returnValue($this->request->reveal()));
|
->will($this->returnValue($this->request));
|
||||||
$this->server->expects($this->any())
|
$this->server->expects($this->any())
|
||||||
->method("getResponse")
|
->method("getResponse")
|
||||||
->will($this->returnValue($this->response->reveal()));
|
->will($this->returnValue($this->response));
|
||||||
$this->server->expects($this->any())
|
$this->server->expects($this->any())
|
||||||
->method("getTransmitter")
|
->method("getTransmitter")
|
||||||
->will($this->returnValue($this->transmitter->reveal()));
|
->will($this->returnValue($this->transmitter->reveal()));
|
||||||
$this->server->__construct();
|
$this->server->__construct();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @covers ::__construct
|
|
||||||
* @covers ::getDefaultDispatcher
|
|
||||||
* @uses WellRESTed\Dispatching\Dispatcher
|
|
||||||
*/
|
|
||||||
public function testCreatesInstances()
|
public function testCreatesInstances()
|
||||||
{
|
{
|
||||||
$server = new Server();
|
$server = new Server();
|
||||||
$this->assertNotNull($server);
|
$this->assertNotNull($server);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @covers ::add
|
|
||||||
*/
|
|
||||||
public function testAddIsFluid()
|
public function testAddIsFluid()
|
||||||
{
|
{
|
||||||
$server = new Server();
|
$server = new Server();
|
||||||
$this->assertSame($server, $server->add("middleware"));
|
$this->assertSame($server, $server->add("middleware"));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @covers ::getDispatcher
|
|
||||||
*/
|
|
||||||
public function testReturnsDispatcher()
|
public function testReturnsDispatcher()
|
||||||
{
|
{
|
||||||
$this->assertSame($this->dispatcher->reveal(), $this->server->getDispatcher());
|
$this->assertSame($this->dispatcher->reveal(), $this->server->getDispatcher());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @covers ::add
|
|
||||||
* @covers ::dispatch
|
|
||||||
*/
|
|
||||||
public function testDispatchesMiddlewareStack()
|
public function testDispatchesMiddlewareStack()
|
||||||
{
|
{
|
||||||
$next = function ($request, $response) {
|
|
||||||
return $response;
|
|
||||||
};
|
|
||||||
|
|
||||||
$this->server->add("first");
|
$this->server->add("first");
|
||||||
$this->server->add("second");
|
$this->server->add("second");
|
||||||
$this->server->add("third");
|
$this->server->add("third");
|
||||||
|
|
||||||
$this->server->dispatch($this->request->reveal(), $this->response->reveal(), $next);
|
$this->server->dispatch($this->request, $this->response, $this->next);
|
||||||
|
|
||||||
$this->dispatcher->dispatch(
|
$this->dispatcher->dispatch(
|
||||||
["first", "second", "third"],
|
["first", "second", "third"],
|
||||||
$this->request->reveal(),
|
$this->request,
|
||||||
$this->response->reveal(),
|
$this->response,
|
||||||
$next
|
$this->next
|
||||||
)->shouldHaveBeenCalled();
|
)->shouldHaveBeenCalled();
|
||||||
}
|
}
|
||||||
|
|
||||||
// ------------------------------------------------------------------------
|
// ------------------------------------------------------------------------
|
||||||
// Respond
|
// Respond
|
||||||
|
|
||||||
/**
|
|
||||||
* @covers ::respond
|
|
||||||
*/
|
|
||||||
public function testRespondDispatchesRequest()
|
public function testRespondDispatchesRequest()
|
||||||
{
|
{
|
||||||
$this->server->respond();
|
$this->server->respond();
|
||||||
$this->dispatcher->dispatch(
|
$this->dispatcher->dispatch(
|
||||||
Argument::any(),
|
Argument::any(),
|
||||||
$this->request->reveal(),
|
$this->request,
|
||||||
Argument::any(),
|
Argument::any(),
|
||||||
Argument::any()
|
Argument::any()
|
||||||
)->shouldHaveBeenCalled();
|
)->shouldHaveBeenCalled();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @covers ::respond
|
|
||||||
*/
|
|
||||||
public function testRespondDispatchesResponse()
|
public function testRespondDispatchesResponse()
|
||||||
{
|
{
|
||||||
$this->server->respond();
|
$this->server->respond();
|
||||||
$this->dispatcher->dispatch(
|
$this->dispatcher->dispatch(
|
||||||
Argument::any(),
|
Argument::any(),
|
||||||
Argument::any(),
|
Argument::any(),
|
||||||
$this->response->reveal(),
|
$this->response,
|
||||||
Argument::any()
|
Argument::any()
|
||||||
)->shouldHaveBeenCalled();
|
)->shouldHaveBeenCalled();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @covers ::respond
|
|
||||||
*/
|
|
||||||
public function testRespondSendsResponseToResponder()
|
public function testRespondSendsResponseToResponder()
|
||||||
{
|
{
|
||||||
$this->server->respond();
|
$this->server->respond();
|
||||||
$this->transmitter->transmit(
|
$this->transmitter->transmit(
|
||||||
$this->request->reveal(),
|
$this->request,
|
||||||
$this->response->reveal()
|
$this->response
|
||||||
)->shouldHaveBeenCalled();
|
)->shouldHaveBeenCalled();
|
||||||
}
|
}
|
||||||
|
|
||||||
// ------------------------------------------------------------------------
|
// ------------------------------------------------------------------------
|
||||||
// Router
|
// Router
|
||||||
|
|
||||||
/**
|
|
||||||
* @covers ::createRouter
|
|
||||||
* @uses WellRESTed\Routing\Router
|
|
||||||
* @uses WellRESTed\Routing\MethodMap
|
|
||||||
* @uses WellRESTed\Routing\Route\RouteFactory
|
|
||||||
* @uses WellRESTed\Routing\Route\Route
|
|
||||||
* @uses WellRESTed\Routing\Route\StaticRoute
|
|
||||||
*/
|
|
||||||
public function testCreatesRouterWithDispatcher()
|
public function testCreatesRouterWithDispatcher()
|
||||||
{
|
{
|
||||||
$this->request->getMethod()->willReturn("GET");
|
$this->request = $this->request
|
||||||
$this->request->getRequestTarget()->willReturn("/");
|
->withMethod("GET")
|
||||||
|
->withRequestTarget("/");
|
||||||
$next = function ($request, $response) {
|
|
||||||
return $response;
|
|
||||||
};
|
|
||||||
|
|
||||||
$router = $this->server->createRouter();
|
$router = $this->server->createRouter();
|
||||||
$router->register("GET", "/", "middleware");
|
$router->register("GET", "/", "middleware");
|
||||||
$router($this->request->reveal(), $this->response->reveal(), $next);
|
$router($this->request, $this->response, $this->next);
|
||||||
|
|
||||||
$this->dispatcher->dispatch(
|
$this->dispatcher->dispatch(
|
||||||
"middleware",
|
"middleware",
|
||||||
$this->request->reveal(),
|
$this->request,
|
||||||
$this->response->reveal(),
|
$this->response,
|
||||||
$next
|
$this->next
|
||||||
)->shouldHaveBeenCalled();
|
)->shouldHaveBeenCalled();
|
||||||
}
|
}
|
||||||
|
|
||||||
// ------------------------------------------------------------------------
|
// ------------------------------------------------------------------------
|
||||||
// Attributes
|
// Attributes
|
||||||
|
|
||||||
/**
|
|
||||||
* @covers ::respond
|
|
||||||
*/
|
|
||||||
public function testAddsAttributesToRequest()
|
public function testAddsAttributesToRequest()
|
||||||
{
|
{
|
||||||
$attributes = [
|
$attributes = [
|
||||||
|
|
@ -193,6 +153,16 @@ class ServerTest extends \PHPUnit_Framework_TestCase
|
||||||
|
|
||||||
$this->server->__construct($attributes);
|
$this->server->__construct($attributes);
|
||||||
$this->server->respond();
|
$this->server->respond();
|
||||||
$this->request->withAttribute("name", "value")->shouldHaveBeenCalled();
|
|
||||||
|
$isRequestWithExpectedAttribute = function ($request) {
|
||||||
|
return $request->getAttribute("name") === "value";
|
||||||
|
};
|
||||||
|
|
||||||
|
$this->dispatcher->dispatch(
|
||||||
|
Argument::any(),
|
||||||
|
Argument::that($isRequestWithExpectedAttribute),
|
||||||
|
Argument::any(),
|
||||||
|
Argument::any()
|
||||||
|
)->shouldHaveBeenCalled();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue