Skip to content
This repository has been archived by the owner on Jan 29, 2020. It is now read-only.

[ZF3] [BC-BREAK] Remove ServiceLocatorAwareInterface from AbstractController #4

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 1 addition & 30 deletions src/Controller/AbstractController.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
use Zend\Http\Request as HttpRequest;
use Zend\Mvc\InjectApplicationEventInterface;
use Zend\Mvc\MvcEvent;
use Zend\ServiceManager\ServiceLocatorAwareInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
use Zend\Stdlib\DispatchableInterface as Dispatchable;
use Zend\Stdlib\RequestInterface as Request;
use Zend\Stdlib\ResponseInterface as Response;
Expand Down Expand Up @@ -46,8 +44,7 @@
abstract class AbstractController implements
Dispatchable,
EventManagerAwareInterface,
InjectApplicationEventInterface,
ServiceLocatorAwareInterface
InjectApplicationEventInterface
{
/**
* @var PluginManager
Expand All @@ -74,11 +71,6 @@ abstract class AbstractController implements
*/
protected $events;

/**
* @var ServiceLocatorInterface
*/
protected $serviceLocator;

/**
* @var null|string|string[]
*/
Expand Down Expand Up @@ -230,27 +222,6 @@ public function getEvent()
return $this->event;
}

/**
* Set serviceManager instance
*
* @param ServiceLocatorInterface $serviceLocator
* @return void
*/
public function setServiceLocator(ServiceLocatorInterface $serviceLocator)
{
$this->serviceLocator = $serviceLocator;
}

/**
* Retrieve serviceManager instance
*
* @return ServiceLocatorInterface
*/
public function getServiceLocator()
{
return $this->serviceLocator;
}

/**
* Get plugin manager
*
Expand Down
26 changes: 15 additions & 11 deletions test/ApplicationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ public function setupPathController($addService = true)
$this->application->bootstrap();
}

public function setupActionController()
public function setupActionController($controllerClass = Controller\TestAsset\SampleController::class)
{
$request = $this->serviceManager->get('Request');
$request->setUri('http://example.local/sample');
Expand All @@ -250,16 +250,14 @@ public function setupActionController()
$route = Router\Http\Literal::factory(array(
'route' => '/sample',
'defaults' => array(
'controller' => 'sample',
'controller' => $controllerClass,
'action' => 'test',
),
));
$router->addRoute('sample', $route);

$controllerLoader = $this->serviceManager->get('ControllerLoader');
$controllerLoader->setFactory('sample', function () {
return new Controller\TestAsset\SampleController;
});
$controllerLoader->setInvokableClass($controllerClass, $controllerClass);
$this->application->bootstrap();
}

Expand Down Expand Up @@ -333,15 +331,21 @@ public function testControllerIsDispatchedDuringRun()
*/
public function testDispatchingInjectsLocatorInLocatorAwareControllers()
{
$this->setupActionController();
$controllerClass = Controller\TestAsset\ServiceLocatorAwareController::class;
$this->setupActionController($controllerClass);

$events = $this->application->getEventManager()->getSharedManager();
$storage = new ArrayObject();
$events->attach('ZendTest\Mvc\Controller\TestAsset\SampleController', MvcEvent::EVENT_DISPATCH, function ($e) use ($storage) {
$controller = $e->getTarget();
$storage['locator'] = $controller->getServiceLocator();
return $e->getResponse();
}, 100);
$events->attach(
$controllerClass,
MvcEvent::EVENT_DISPATCH,
function ($e) use ($storage) {
$controller = $e->getTarget();
$storage['locator'] = $controller->getServiceLocator();
return $e->getResponse();
},
100
);

$this->application->run();

Expand Down
3 changes: 1 addition & 2 deletions test/Controller/AbstractControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,7 @@ public function testSetEventManagerWithDefaultIdentifiersIncludesImplementedInte
->with($this->logicalAnd(
$this->contains('Zend\\EventManager\\EventManagerAwareInterface'),
$this->contains('Zend\\Stdlib\\DispatchableInterface'),
$this->contains('Zend\\Mvc\\InjectApplicationEventInterface'),
$this->contains('Zend\\ServiceManager\\ServiceLocatorAwareInterface')
$this->contains('Zend\\Mvc\\InjectApplicationEventInterface')
));

$this->controller->setEventManager($eventManager);
Expand Down
5 changes: 0 additions & 5 deletions test/Controller/ActionControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,11 +161,6 @@ public function testDispatchInjectsEventIntoController()
$this->assertSame($this->event, $event);
}

public function testControllerIsLocatorAware()
{
$this->assertInstanceOf('Zend\ServiceManager\ServiceLocatorAwareInterface', $this->controller);
}

public function testControllerIsEventAware()
{
$this->assertInstanceOf('Zend\Mvc\InjectApplicationEventInterface', $this->controller);
Expand Down
1 change: 0 additions & 1 deletion test/Controller/ControllerManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ public function testInjectControllerDependenciesInjectsExpectedDependencies()
{
$controller = new TestAsset\SampleController();
$this->controllers->injectControllerDependencies($controller, $this->controllers);
$this->assertSame($this->services, $controller->getServiceLocator());
$this->assertSame($this->plugins, $controller->getPluginManager());

// The default AbstractController implementation lazy instantiates an EM
Expand Down
3 changes: 1 addition & 2 deletions test/Controller/Plugin/ForwardTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ public function setUp()

$this->controller = new SampleController();
$this->controller->setEvent($event);
$this->controller->setServiceLocator($services);
$this->controller->setPluginManager($plugins);

$this->plugin = $this->controller->plugin('forward');
Expand Down Expand Up @@ -127,7 +126,7 @@ public function testPluginWithoutControllerLocatorRaisesServiceNotCreatedExcepti

public function testDispatchRaisesDomainExceptionIfDiscoveredControllerIsNotDispatchable()
{
$locator = $this->controller->getServiceLocator();
$locator = $this->plugins->getServiceLocator();
$locator->add('bogus', function () {
return new stdClass;
});
Expand Down
5 changes: 0 additions & 5 deletions test/Controller/RestfulControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -342,11 +342,6 @@ public function testDispatchInjectsEventIntoController()
$this->assertSame($this->event, $event);
}

public function testControllerIsLocatorAware()
{
$this->assertInstanceOf('Zend\ServiceManager\ServiceLocatorAwareInterface', $this->controller);
}

public function testControllerIsEventAware()
{
$this->assertInstanceOf('Zend\Mvc\InjectApplicationEventInterface', $this->controller);
Expand Down
16 changes: 16 additions & 0 deletions test/Controller/TestAsset/ServiceLocatorAwareController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php
/**
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wrong file block

* @author Stefano Torresi (http://stefanotorresi.it)
* @license See the file LICENSE.txt for copying permission.
* ************************************************
*/

namespace ZendTest\Mvc\Controller\TestAsset;

use Zend\ServiceManager\ServiceLocatorAwareInterface;
use Zend\ServiceManager\ServiceLocatorAwareTrait;

class ServiceLocatorAwareController extends SampleController implements ServiceLocatorAwareInterface
{
use ServiceLocatorAwareTrait;
}
1 change: 0 additions & 1 deletion test/Service/ControllerLoaderFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ public function testControllerLoadedCanBeInjectedWithValuesFromPeer()

$controller = $this->loader->get('ZendTest\Dispatchable');
$this->assertInstanceOf('ZendTest\Mvc\Service\TestAsset\Dispatchable', $controller);
$this->assertSame($this->services, $controller->getServiceLocator());
$this->assertSame($this->services->get('EventManager'), $controller->getEventManager());
$this->assertSame($this->services->get('ControllerPluginManager'), $controller->getPluginManager());
}
Expand Down