Skip to content

Commit

Permalink
update accessors
Browse files Browse the repository at this point in the history
  • Loading branch information
easy-system committed Jun 15, 2016
1 parent 7c614d4 commit 1303a8f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 92 deletions.
73 changes: 7 additions & 66 deletions src/Listener/DispatchesControllerListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
namespace Es\Dispatcher\Listener;

use Es\Dispatcher\DispatchEvent;
use Es\Events\EventsInterface;
use Es\Events\EventsTrait;
use Es\Http\ServerInterface;
use Es\Mvc\ControllersInterface;
use Es\Services\ServicesTrait;
use Es\Services\Provider;
use Es\System\SystemEvent;
use Psr\Http\Message\ResponseInterface;
use RuntimeException;
Expand All @@ -23,28 +23,7 @@
*/
class DispatchesControllerListener
{
use ServicesTrait;

/**
* The controllers.
*
* @var \Es\Mvc\ControllersInterface
*/
protected $controllers;

/**
* The events.
*
* @var \Es\Events\EventsInterface
*/
protected $events;

/**
* The server.
*
* @var \Es\Http\ServerInterface
*/
protected $server;
use EventsTrait;

/**
* Sets the controllers.
Expand All @@ -53,7 +32,7 @@ class DispatchesControllerListener
*/
public function setControllers(ControllersInterface $controllers)
{
$this->controllers = $controllers;
Provider::getServices()->set('Controllers', $controllers);
}

/**
Expand All @@ -63,39 +42,7 @@ public function setControllers(ControllersInterface $controllers)
*/
public function getControllers()
{
if (! $this->controllers) {
$services = $this->getServices();
$controllers = $services->get('Controllers');
$this->setControllers($controllers);
}

return $this->controllers;
}

/**
* Sets the events.
*
* @param \Es\Events\EventsInterface $events The events
*/
public function setEvents(EventsInterface $events)
{
$this->events = $events;
}

/**
* Gets the events.
*
* @return \Es\Events\EventsInterface The events
*/
public function getEvents()
{
if (! $this->events) {
$services = $this->getServices();
$events = $services->get('Events');
$this->setEvents($events);
}

return $this->events;
return Provider::getServices()->get('Controllers');
}

/**
Expand All @@ -105,7 +52,7 @@ public function getEvents()
*/
public function setServer(ServerInterface $server)
{
$this->server = $server;
Provider::getServices()->set('Server', $server);
}

/**
Expand All @@ -115,13 +62,7 @@ public function setServer(ServerInterface $server)
*/
public function getServer()
{
if (! $this->server) {
$services = $this->getServices();
$server = $services->get('Server');
$this->setServer($server);
}

return $this->server;
return Provider::getServices()->get('Server');
}

/**
Expand Down
43 changes: 17 additions & 26 deletions test/Listener/DispatchesControllerListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Es\Http\Response;
use Es\Http\Server;
use Es\Http\ServerRequest;
use Es\Services\Provider;
use Es\Services\Services;
use Es\System\SystemEvent;

Expand All @@ -35,53 +36,43 @@ public function testGetControllers()
$services = new Services();
$controllers = new FakeControllers();
$services->set('Controllers', $controllers);

Provider::setServices($services);
$dispatcher = new DispatchesControllerListener();
$dispatcher->setServices($services);
$this->assertSame($controllers, $dispatcher->getControllers());
}

public function testSetControllers()
{
$services = new Services();
Provider::setServices($services);

$controllers = new FakeControllers();
$dispatcher = new DispatchesControllerListener();
$dispatcher->setControllers($controllers);
$this->assertSame($controllers, $dispatcher->getControllers());
}

public function testGetEvents()
{
$events = new Events();
$services = new Services();
$services->set('Events', $events);
$dispatcher = new DispatchesControllerListener();
$dispatcher->setServices($services);
$this->assertSame($events, $dispatcher->getEvents());
}

public function testSetEvents()
{
$events = new Events();
$dispatcher = new DispatchesControllerListener();
$dispatcher->setEvents($events);
$this->assertSame($events, $dispatcher->getEvents());
$this->assertSame($controllers, $services->get('Controllers'));
}

public function testGetServer()
{
$server = new Server();
$services = new Services();
$services->set('Server', $server);

Provider::setServices($services);
$dispatcher = new DispatchesControllerListener();
$dispatcher->setServices($services);
$this->assertSame($server, $dispatcher->getServer());
}

public function testSetServer()
{
$services = new Services();
Provider::setServices($services);

$server = new Server();
$dispatcher = new DispatchesControllerListener();
$dispatcher->setServer($server);
$this->assertSame($server, $dispatcher->getServer());
$this->assertSame($server, $services->get('Server'));
}

public function testOnDispatchRaiseExceptionIfRequestNotHaveTheControllerAttribute()
Expand All @@ -102,7 +93,7 @@ public function testOnDispatchRaiseExceptionIfRequestNotHaveTheControllerAttribu
public function testOnDispatchIfResultIsInstanceOfResponse()
{
$server = new Server();
$request = $this->getMock('Es\Http\ServerRequest');
$request = $this->getMock(ServerRequest::CLASS);
$response = new Response();
$server->setRequest($request);
$server->setResponse($response);
Expand Down Expand Up @@ -163,7 +154,7 @@ public function testOnDispatchIfResultIsNotInstanceOfResponse()
$result = 'Lorem ipsum dolor sit amet';

$server = new Server();
$request = $this->getMock('Es\Http\ServerRequest');
$request = $this->getMock(ServerRequest::CLASS);
$response = new Response();
$server->setRequest($request);
$server->setResponse($response);
Expand All @@ -172,7 +163,7 @@ public function testOnDispatchIfResultIsNotInstanceOfResponse()
$controllers = new FakeControllers();
$controllers->set('FakeController', $controller);

$events = $this->getMock('Es\Events\Events');
$events = $this->getMock(Events::CLASS);

$dispatcher = new DispatchesControllerListener();
$dispatcher->setServer($server);
Expand Down Expand Up @@ -230,7 +221,7 @@ public function testDoDispatch()
$controller = $this->getMock('FakeController', ['fakeAction']);
$event = new DispatchEvent($controller, 'FakeController', 'fake', $params);

$request = $this->getMock('Es\Http\ServerRequest');
$request = $this->getMock(ServerRequest::CLASS);
$response = new Response();
$server = new Server();
$server->setRequest($request);
Expand Down

0 comments on commit 1303a8f

Please sign in to comment.