/** * @test */ public function getInstanceUsesClassNameMd5AsCacheKey() { $className = 'TYPO3\\CMS\\Extbase\\Tests\\Unit\\Object\\Container\\Fixtures\\NamespacedClass'; $classNameHash = md5($className); $mockedCache = $this->getMock('TYPO3\\CMS\\Extbase\\Object\\Container\\ClassInfoCache', array('has', 'set', 'get')); $container = $this->getMock('TYPO3\\CMS\\Extbase\\Object\\Container\\Container', array('log', 'getClassInfoCache')); $container->expects($this->any())->method('getClassInfoCache')->will($this->returnValue($mockedCache)); $mockedCache->expects($this->never())->method('has'); $mockedCache->expects($this->once())->method('get')->with($classNameHash)->will($this->returnValue(FALSE)); $mockedCache->expects($this->once())->method('set')->with($classNameHash, $this->anything())->will($this->returnCallback(array($this, 'setClassInfoCacheCallback'))); $container->getInstance($className); $this->assertInstanceOf('TYPO3\\CMS\\Extbase\\Object\\Container\\ClassInfo', $this->cachedClassInfo); $this->assertEquals($className, $this->cachedClassInfo->getClassName()); }
/** * Inject setter-dependencies into $instance * * @param object $instance * @param \TYPO3\CMS\Extbase\Object\Container\ClassInfo $classInfo * @return void */ protected function injectDependencies($instance, \TYPO3\CMS\Extbase\Object\Container\ClassInfo $classInfo) { if (!$classInfo->hasInjectMethods() && !$classInfo->hasInjectProperties()) { return; } foreach ($classInfo->getInjectMethods() as $injectMethodName => $classNameToInject) { $instanceToInject = $this->getInstanceInternal($classNameToInject); if ($classInfo->getIsSingleton() && !$instanceToInject instanceof \TYPO3\CMS\Core\SingletonInterface) { $this->log('The singleton "' . $classInfo->getClassName() . '" needs a prototype in "' . $injectMethodName . '". This is often a bad code smell; often you rather want to inject a singleton.', 1); } if (is_callable([$instance, $injectMethodName])) { $instance->{$injectMethodName}($instanceToInject); } } foreach ($classInfo->getInjectProperties() as $injectPropertyName => $classNameToInject) { $instanceToInject = $this->getInstanceInternal($classNameToInject); if ($classInfo->getIsSingleton() && !$instanceToInject instanceof \TYPO3\CMS\Core\SingletonInterface) { $this->log('The singleton "' . $classInfo->getClassName() . '" needs a prototype in "' . $injectPropertyName . '". This is often a bad code smell; often you rather want to inject a singleton.', 1); } $propertyReflection = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Extbase\Reflection\PropertyReflection::class, $instance, $injectPropertyName); $propertyReflection->setAccessible(true); $propertyReflection->setValue($instance, $instanceToInject); } }