Server accepts attributes array as first constructor parameter and sets attributes on server request
This commit is contained in:
parent
b06abc0df2
commit
474d8da61c
|
|
@ -15,6 +15,9 @@ use WellRESTed\Transmission\TransmitterInterface;
|
||||||
|
|
||||||
class Server implements DispatchStackInterface
|
class Server implements DispatchStackInterface
|
||||||
{
|
{
|
||||||
|
/** @var array */
|
||||||
|
private $attributes;
|
||||||
|
|
||||||
/** @var DispatcherInterface */
|
/** @var DispatcherInterface */
|
||||||
private $dispatcher;
|
private $dispatcher;
|
||||||
|
|
||||||
|
|
@ -24,15 +27,24 @@ class Server implements DispatchStackInterface
|
||||||
/**
|
/**
|
||||||
* Create a new router.
|
* Create a new router.
|
||||||
*
|
*
|
||||||
|
* @param array $attributes key-value pairs to register as attributes
|
||||||
|
* with the server request.
|
||||||
* @param DispatcherInterface $dispatcher Dispatches middleware. If no
|
* @param DispatcherInterface $dispatcher Dispatches middleware. If no
|
||||||
* object is passed, the Server will create a
|
* object is passed, the Server will create a
|
||||||
* WellRESTed\Dispatching\Dispatcher
|
* WellRESTed\Dispatching\Dispatcher
|
||||||
*/
|
*/
|
||||||
public function __construct(DispatcherInterface $dispatcher = null)
|
public function __construct(array $attributes = null, DispatcherInterface $dispatcher = null)
|
||||||
{
|
{
|
||||||
if ($dispatcher === null) {
|
if ($attributes === null) {
|
||||||
$this->dispatcher = $this->getDispatcher();
|
$attributes = [];
|
||||||
}
|
}
|
||||||
|
$this->attributes = $attributes;
|
||||||
|
|
||||||
|
if ($dispatcher === null) {
|
||||||
|
$dispatcher = $this->getDispatcher();
|
||||||
|
}
|
||||||
|
$this->dispatcher = $dispatcher;
|
||||||
|
|
||||||
$this->stack = [];
|
$this->stack = [];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -104,6 +116,9 @@ class Server implements DispatchStackInterface
|
||||||
if ($request === null) {
|
if ($request === null) {
|
||||||
$request = $this->getRequest();
|
$request = $this->getRequest();
|
||||||
}
|
}
|
||||||
|
foreach ($this->attributes as $name => $value) {
|
||||||
|
$request = $request->withAttribute($name, $value);
|
||||||
|
}
|
||||||
if ($response === null) {
|
if ($response === null) {
|
||||||
$response = $this->getResponse();
|
$response = $this->getResponse();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -21,6 +21,7 @@ class ServerTest extends \PHPUnit_Framework_TestCase
|
||||||
{
|
{
|
||||||
parent::setUp();
|
parent::setUp();
|
||||||
$this->request = $this->prophesize('Psr\Http\Message\ServerRequestInterface');
|
$this->request = $this->prophesize('Psr\Http\Message\ServerRequestInterface');
|
||||||
|
$this->request->withAttribute(Argument::cetera())->willReturn($this->request->reveal());
|
||||||
$this->response = $this->prophesize('Psr\Http\Message\ResponseInterface');
|
$this->response = $this->prophesize('Psr\Http\Message\ResponseInterface');
|
||||||
$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();
|
||||||
|
|
@ -169,4 +170,22 @@ class ServerTest extends \PHPUnit_Framework_TestCase
|
||||||
$next
|
$next
|
||||||
)->shouldHaveBeenCalled();
|
)->shouldHaveBeenCalled();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ------------------------------------------------------------------------
|
||||||
|
// Attributes
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers ::respond
|
||||||
|
*/
|
||||||
|
public function testAddsAttributesToRequest()
|
||||||
|
{
|
||||||
|
$attributes = [
|
||||||
|
"name" => "value"
|
||||||
|
];
|
||||||
|
|
||||||
|
$this->server->__construct($attributes);
|
||||||
|
$this->server->respond();
|
||||||
|
$this->request->withAttribute("name", "value")->shouldHaveBeenCalled();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue