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

Commit eca3efc

Browse files
committed
Test duck-typed ServiceLocatorAwareInterface initializers
Adds tests for both `ServiceManagerConfig` and `ControllerManager`, verifying that each can do duck-typed `ServiceLocatorAwareInterface` injection via the initializers they register.
1 parent b97120e commit eca3efc

File tree

4 files changed

+80
-0
lines changed

4 files changed

+80
-0
lines changed

test/Controller/ControllerManagerTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,26 @@
99

1010
namespace ZendTest\Mvc\Controller;
1111

12+
use PHPUnit_Framework_Error_Deprecated;
1213
use PHPUnit_Framework_TestCase as TestCase;
1314
use ReflectionClass;
1415
use Zend\EventManager\EventManager;
1516
use Zend\EventManager\SharedEventManager;
1617
use Zend\Mvc\Controller\ControllerManager;
1718
use Zend\Mvc\Controller\PluginManager as ControllerPluginManager;
1819
use Zend\ServiceManager\Config;
20+
use Zend\ServiceManager\Factory\InvokableFactory;
1921
use Zend\ServiceManager\ServiceManager;
2022
use Zend\Console\Adapter\Virtual as ConsoleAdapter;
23+
use ZendTest\Mvc\Service\TestAsset\DuckTypedServiceLocatorAwareController;
2124

2225
class ControllerManagerTest extends TestCase
2326
{
2427
public function setUp()
2528
{
29+
// Disable deprecation notices
30+
PHPUnit_Framework_Error_Deprecated::$enabled = false;
31+
2632
$this->sharedEvents = new SharedEventManager;
2733
$this->events = $this->createEventManager($this->sharedEvents);
2834
$this->consoleAdapter = new ConsoleAdapter();
@@ -145,4 +151,13 @@ public function testDoNotUsePeeringServiceManagers()
145151
$this->setExpectedException('Zend\ServiceManager\Exception\ServiceNotFoundException');
146152
$this->controllers->get('EventManager');
147153
}
154+
155+
public function testServiceLocatorAwareInitializerInjectsDuckTypedImplementations()
156+
{
157+
$this->controllers->setFactory(DuckTypedServiceLocatorAwareController::class, InvokableFactory::class);
158+
159+
$controller = $this->controllers->get(DuckTypedServiceLocatorAwareController::class);
160+
$this->assertInstanceOf(DuckTypedServiceLocatorAwareController::class, $controller);
161+
$this->assertSame($this->services, $controller->getServiceLocator());
162+
}
148163
}

test/Service/ServiceManagerConfigTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
namespace ZendTest\Mvc\Service;
1111

12+
use PHPUnit_Framework_Error_Deprecated;
1213
use PHPUnit_Framework_TestCase as TestCase;
1314
use ReflectionClass;
1415
use stdClass;
@@ -37,6 +38,9 @@ class ServiceManagerConfigTest extends TestCase
3738
*/
3839
protected function setUp()
3940
{
41+
// Disable deprecation notices
42+
PHPUnit_Framework_Error_Deprecated::$enabled = false;
43+
4044
$this->config = new ServiceManagerConfig();
4145
$this->services = new ServiceManager();
4246
$this->config->configureServiceManager($this->services);
@@ -212,4 +216,16 @@ public function testEventManagerInitializerCanBeReplaced()
212216

213217
$serviceManager->get('EventManagerAware');
214218
}
219+
220+
public function testServiceLocatorAwareInitializerInjectsDuckTypedImplementations()
221+
{
222+
$serviceManager = new ServiceManager(['factories' => [
223+
TestAsset\DuckTypedServiceLocatorAware::class => InvokableFactory::class,
224+
]]);
225+
(new ServiceManagerConfig())->configureServiceManager($serviceManager);
226+
227+
$instance = $serviceManager->get(TestAsset\DuckTypedServiceLocatorAware::class);
228+
$this->assertInstanceOf(TestAsset\DuckTypedServiceLocatorAware::class, $instance);
229+
$this->assertSame($serviceManager, $instance->getServiceLocator());
230+
}
215231
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
/**
3+
* Zend Framework (http://framework.zend.com/)
4+
*
5+
* @link http://github.com/zendframework/zend-mvc for the canonical source repository
6+
* @copyright Copyright (c) 2005-2016 Zend Technologies USA Inc. (http://www.zend.com)
7+
* @license http://framework.zend.com/license/new-bsd New BSD License
8+
*/
9+
10+
namespace ZendTest\Mvc\Service\TestAsset;
11+
12+
use Zend\ServiceManager\ServiceLocatorInterface;
13+
14+
class DuckTypedServiceLocatorAware
15+
{
16+
private $container;
17+
18+
public function setServiceLocator(ServiceLocatorInterface $container)
19+
{
20+
$this->container = $container;
21+
}
22+
23+
public function getServiceLocator()
24+
{
25+
return $this->container;
26+
}
27+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
/**
3+
* Zend Framework (http://framework.zend.com/)
4+
*
5+
* @link http://github.com/zendframework/zend-mvc for the canonical source repository
6+
* @copyright Copyright (c) 2005-2016 Zend Technologies USA Inc. (http://www.zend.com)
7+
* @license http://framework.zend.com/license/new-bsd New BSD License
8+
*/
9+
10+
namespace ZendTest\Mvc\Service\TestAsset;
11+
12+
use Zend\Stdlib\DispatchableInterface;
13+
use Zend\Stdlib\RequestInterface;
14+
use Zend\Stdlib\ResponseInterface;
15+
16+
class DuckTypedServiceLocatorAwareController extends DuckTypedServiceLocatorAware implements
17+
DispatchableInterface
18+
{
19+
public function dispatch(RequestInterface $request, ResponseInterface $response = null)
20+
{
21+
}
22+
}

0 commit comments

Comments
 (0)