/** * Gathers up all the fiddly details we need for the ClassData object * * @param MockData $obj * @param EntityData $entity * @return MockData */ private function generateMockDataDetails(MockData $obj, EntityData $entity) { $methodWorker = new MethodDataWorker(); $propertyWorker = new PropertyDataWorker(); $targetClass = $entity->getClassNamespace() . '\\' . $entity->getClassName(); $obj->setHasConstructor($entity->getHasConstructor())->addUseStatements($entity->getUseStatements())->setExtends($entity->getExtends())->addImplements($entity->getImplements())->addMethods($methodWorker->generateMethodObjects($entity->getMethods()))->addProperties($propertyWorker->generatePropertyObjects($targetClass, $entity->getProperties(), $obj->getMethods())); return $obj; }
/** * Generates use statements out of namespaces for all classes used by the mocked class * * To help out with generating mocks, we're gathering the namespaces * that are used in typehinting as well as the ones declared at the * top of the file. * * @param MockData $mockData MockData object * @param EntityData $entityData EntityData object * @return string */ protected function generateUseStatements(MockData $mockData, EntityData $entityData) { $classUse = $mockData->getUseStatements(); $mockedClassUse = "use {$entityData->getClassNamespace()}\\{$entityData->getClassName()};"; array_unshift($classUse, $mockedClassUse); $propUse = $this->getUseStatementsFromClassProperties($mockData->getProperties()); $methodUse = $this->getUseStatementsFromClassMethods($mockData->getMethods()); $statements = array_merge($classUse, $propUse, $methodUse); return join(PHP_EOL, array_unique($statements)); }
/** * Creates a ReflectionClass instance of the target class * * TODO: Eventually this needs to be able to handle classes with constructors as well. * * Might need to use Mockery to generate mocked versions of the constructor arguments * so this can continue. * * PHP >= 5.4 has the ReflectionClass::newInstanceWithoutConstructor method * Otherwise ... parse the file directly? * * @param EntityData $classData * @return \ReflectionClass */ private function createReflectionClass(EntityData $classData) { return new \ReflectionClass($classData->getClassNamespace() . '\\' . $classData->getClassName()); }