Server accepts attributes array as first constructor parameter and sets attributes on server request

This commit is contained in:
PJ Dietz 2015-05-17 16:30:40 -04:00
parent b06abc0df2
commit 474d8da61c
2 changed files with 37 additions and 3 deletions

View File

@ -15,6 +15,9 @@ use WellRESTed\Transmission\TransmitterInterface;
class Server implements DispatchStackInterface
{
/** @var array */
private $attributes;
/** @var DispatcherInterface */
private $dispatcher;
@ -24,15 +27,24 @@ class Server implements DispatchStackInterface
/**
* 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
* object is passed, the Server will create a
* WellRESTed\Dispatching\Dispatcher
*/
public function __construct(DispatcherInterface $dispatcher = null)
public function __construct(array $attributes = null, DispatcherInterface $dispatcher = null)
{
if ($dispatcher === null) {
$this->dispatcher = $this->getDispatcher();
if ($attributes === null) {
$attributes = [];
}
$this->attributes = $attributes;
if ($dispatcher === null) {
$dispatcher = $this->getDispatcher();
}
$this->dispatcher = $dispatcher;
$this->stack = [];
}
@ -104,6 +116,9 @@ class Server implements DispatchStackInterface
if ($request === null) {
$request = $this->getRequest();
}
foreach ($this->attributes as $name => $value) {
$request = $request->withAttribute($name, $value);
}
if ($response === null) {
$response = $this->getResponse();
}

View File

@ -21,6 +21,7 @@ class ServerTest extends \PHPUnit_Framework_TestCase
{
parent::setUp();
$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->transmitter = $this->prophesize('WellRESTed\Transmission\TransmitterInterface');
$this->transmitter->transmit(Argument::cetera())->willReturn();
@ -169,4 +170,22 @@ class ServerTest extends \PHPUnit_Framework_TestCase
$next
)->shouldHaveBeenCalled();
}
// ------------------------------------------------------------------------
// Attributes
/**
* @covers ::respond
*/
public function testAddsAttributesToRequest()
{
$attributes = [
"name" => "value"
];
$this->server->__construct($attributes);
$this->server->respond();
$this->request->withAttribute("name", "value")->shouldHaveBeenCalled();
}
}