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

Commit

Permalink
Improved unit tests for ServiceManager [zen-33]
Browse files Browse the repository at this point in the history
  • Loading branch information
Ralph Schindler committed May 1, 2012
1 parent 5098fd2 commit ee23691
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 7 deletions.
23 changes: 20 additions & 3 deletions src/Di/DiServiceInitializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Zend\ServiceManager\Di;


use Zend\ServiceManager\InitializerInterface,
Zend\ServiceManager\ServiceLocatorInterface,
Zend\ServiceManager\Exception,
Expand All @@ -11,26 +10,44 @@

class DiServiceInitializer extends Di implements InitializerInterface
{

/**
* @var Di
*/
protected $di = null;

/**
* @var DiInstanceManagerProxy
*/
protected $diInstanceManagerProxy = null;

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

/**
* @param \Zend\Di\Di $di
* @param \Zend\ServiceManager\ServiceLocatorInterface $serviceLocator
* @param null|DiInstanceManagerProxy $diImProxy
*/
public function __construct(Di $di, ServiceLocatorInterface $serviceLocator, DiInstanceManagerProxy $diImProxy = null)
{
$this->di = $di;
$this->serviceLocator = $serviceLocator;
$this->diInstanceManagerProxy = ($diImProxy) ?: new DiInstanceManagerProxy($di->instanceManager(), $serviceLocator);
}

/**
* @param $instance
*/
public function initialize($instance)
{
$instanceManager = $this->di->instanceManager;
$this->di->instanceManager = $this->diInstanceManagerProxy;
try {
$this->di->injectDependencies($instance);
$this->di->instanceManager = $instanceManager;
} catch (Exception $e) {
} catch (\Exception $e) {
$this->di->instanceManager = $instanceManager;
throw $e;
}
Expand Down
49 changes: 46 additions & 3 deletions test/Di/DiAbstractServiceFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,55 @@
namespace ZendTest\ServiceManager\Di;

use Zend\ServiceManager\Di\DiAbstractServiceFactory,
Zend\ServiceManager\Di\DiInstanceManagerProxy;
Zend\ServiceManager\Di\DiInstanceManagerProxy;

class DiAbstractServiceFactoryTest extends \PHPUnit_Framework_TestCase
{

public function testCreateServiceWithName() {}
public function testCanCreateServiceWithName() {}
/**
* @var DiAbstractServiceFactory
*/
protected $diAbstractServiceFactory = null;

/**@#+
* @var \PHPUnit_Framework_MockObject_MockObject
*/
protected $mockDi = null;
protected $mockServiceLocator = null;
/**@#-*/

protected $fooInstance = null;

public function setup()
{
$instanceManager = new \Zend\Di\InstanceManager();
$instanceManager->addSharedInstance($this->fooInstance = new \stdClass(), 'foo');
$this->mockDi = $this->getMock('Zend\Di\Di', array(), array(null, $instanceManager));
$this->mockServiceLocator = $this->getMock('Zend\ServiceManager\ServiceLocatorInterface');
$this->diAbstractServiceFactory = new DiAbstractServiceFactory(
$this->mockDi
);
}


/**
* @covers Zend\ServiceManager\Di\DiAbstractServiceFactory::__construct
*/
public function testConstructor()
{
$instance = new DiAbstractServiceFactory(
$this->getMock('Zend\Di\Di')
);
$this->assertInstanceOf('Zend\ServiceManager\Di\DiAbstractServiceFactory', $instance);
}

/**
* @covers Zend\ServiceManager\Di\DiAbstractServiceFactory::createServiceWithName
* @covers Zend\ServiceManager\Di\DiAbstractServiceFactory::get
*/
public function testCreateServiceWithName()
{
$foo = $this->diAbstractServiceFactory->createServiceWithName($this->mockServiceLocator, 'foo');
$this->assertEquals($this->fooInstance, $foo);
}
}
52 changes: 51 additions & 1 deletion test/Di/DiServiceFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,55 @@

class DiServiceFactoryTest extends \PHPUnit_Framework_TestCase
{
public function testCreateService() {}

protected $diServiceFactory = null;

/**@#+
* @var \PHPUnit_Framework_MockObject_MockObject
*/
protected $mockDi = null;
protected $mockServiceLocator = null;
/**@#-*/

protected $fooInstance = null;

public function setup()
{
$instanceManager = new \Zend\Di\InstanceManager();
$instanceManager->addSharedInstanceWithParameters(
$this->fooInstance = new \stdClass(),
'foo',
array('bar' => 'baz')
);
$this->mockDi = $this->getMock('Zend\Di\Di', array(), array(null, $instanceManager));
$this->mockServiceLocator = $this->getMock('Zend\ServiceManager\ServiceLocatorInterface');
$this->diServiceFactory = new DiServiceFactory(
$this->mockDi,
'foo',
array('bar' => 'baz')
);
}

/**
* @covers Zend\ServiceManager\Di\DiServiceFactory::__construct
*/
public function testConstructor()
{
$instance = new DiServiceFactory(
$this->getMock('Zend\Di\Di'),
'string',
array('foo' => 'bar')
);
$this->assertInstanceOf('Zend\ServiceManager\Di\DiServiceFactory', $instance);
}

/**
* @covers Zend\ServiceManager\Di\DiServiceFactory::createService
* @covers Zend\ServiceManager\Di\DiServiceFactory::get
*/
public function testCreateService()
{
$foo = $this->diServiceFactory->createService($this->mockServiceLocator);
$this->assertEquals($this->fooInstance, $foo);
}
}

0 comments on commit ee23691

Please sign in to comment.