[TYPO3-mvc] Benefit of DI when referencing to a class (and not to an interface)?

Bastian Waidelich bastian at typo3.org
Tue Jun 26 11:22:35 CEST 2012


Roland wrote:

> is there any pro arguments to create objects through dependency
> injection instead of through
> $this->objectManager->create('Tx_Foo_LogFile', 'arg1', 'arg2') when you
> do reference to an concrete class and not an interface?

Yes, it's easier to create unit tests for classes that have less 
dependencies.
With ObjectManager:
$myClass = $this->getAccessibleMock('MyClass', array('dummy'));
$mockBarRepository = $this->getMock('BarRepository');
$mockObjectManager = $this->getMock('ObjectManager');
$mockObjectManager->expects($this->any())->method('create')->with('Tx_Foo_BarRepository')->will($this->returnValue($mockBarRepository));
...

W/o ObjectManager:
$myClass = $this->getAccessibleMock('MyClass', array('dummy'));
$mockBarRepository = $this->getMock('BarRepository');
$myClass->_set('barRepository', $mockBarRepository);

HTH

-- 
Bastian Waidelich
TYPO3 Core Team Member

TYPO3 .... inspiring people to share!
Get involved: typo3.org




More information about the TYPO3-project-typo3v4mvc mailing list