/** * @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()); }
/** * gets array of parameter that can be used to call a constructor * * @param string $className * @param \TYPO3\CMS\Extbase\Object\Container\ClassInfo $classInfo * @param array $givenConstructorArguments * @throws \InvalidArgumentException * @return array */ private function getConstructorArguments($className, \TYPO3\CMS\Extbase\Object\Container\ClassInfo $classInfo, array $givenConstructorArguments) { $parameters = []; $constructorArgumentInformation = $classInfo->getConstructorArguments(); foreach ($constructorArgumentInformation as $index => $argumentInformation) { // Constructor argument given AND argument is a simple type OR instance of argument type if (array_key_exists($index, $givenConstructorArguments) && (!isset($argumentInformation['dependency']) || is_a($givenConstructorArguments[$index], $argumentInformation['dependency']))) { $parameter = $givenConstructorArguments[$index]; } else { if (isset($argumentInformation['dependency']) && !array_key_exists('defaultValue', $argumentInformation)) { $parameter = $this->getInstanceInternal($argumentInformation['dependency']); if ($classInfo->getIsSingleton() && !$parameter instanceof \TYPO3\CMS\Core\SingletonInterface) { $this->log('The singleton "' . $className . '" needs a prototype in the constructor. This is often a bad code smell; often you rather want to inject a singleton.', 1); } } elseif (array_key_exists('defaultValue', $argumentInformation)) { $parameter = $argumentInformation['defaultValue']; } else { throw new \InvalidArgumentException('not a correct info array of constructor dependencies was passed!', 1476107941); } } $parameters[] = $parameter; } return $parameters; }