Beispiel #1
0
 /**
  * @return RobotLoader
  */
 public function createRobotLoader()
 {
     if (!($cacheDir = $this->getCacheDirectory())) {
         throw new InvalidStateException("Set path to temporary directory using setTempDirectory().");
     }
     $loader = new RobotLoader();
     $loader->setCacheStorage(new FileStorage($cacheDir));
     $loader->autoRebuild = !$this->parameters['productionMode'];
     return $loader;
 }
 /**
  *
  * @param InputInterface $input
  * @param OutputInterface $output
  */
 public function generateClassCase(InputInterface $input, OutputInterface $output)
 {
     $src = $this->getSrc();
     $dest = $this->getDest();
     $configNeon = $this->getNeonData($dest);
     $config = Arrays::get($configNeon, 'tests', array());
     $configServices = Arrays::get($config, 'services', array());
     $configData = Arrays::get($config, 'case', array());
     $configCount = count($configData);
     if ($configCount == 0) {
         return;
     }
     $this->writeNotice($output, "Generating Class Specific Tests: " . $configCount);
     $robotLoader = new RobotLoader();
     $robotLoader->autoRebuild = TRUE;
     $robotLoader->setCacheStorage(new FileStorage($this->getCacheDirectory()));
     $robotLoader->addDirectory($dest);
     $robotLoader->rebuild();
     $robotLoader->register();
     $autoloaderClasses = $robotLoader->getIndexedClasses();
     $classReflections = array();
     foreach ($autoloaderClasses as $class) {
         $classReflections[] = $class;
     }
     foreach ($configServices as $configName => $configClass) {
         $classPrototype = Arrays::get($configClass, 'class');
         $classType = $classPrototype . "Test";
         $classFilename = Arrays::get($autoloaderClasses, $classType, NULL);
         $classGeneratorProperties = array();
         $classGeneratorMethods = array();
         $classGeneratorConst = array();
         $classGeneratorImplements = array();
         $classDependencies = array();
         if (NULL === $classFilename) {
             $classPrototypeReflection = ReflectionClassType::from($classPrototype);
             $classParent = "\\" . $classPrototypeReflection->getName() . "Base";
             $classPrototypeName = $classPrototypeReflection->getName();
             $classPrototypeGenerator = PhpGeneratorClassType::from($classPrototypeReflection);
             $className = $classPrototypeGenerator->getName() . "Test";
             $classReflection = $classPrototypeReflection;
             $classGenerator = new PhpGeneratorClassType($classReflection);
             $classGenerator->setName($className);
             $classGenerator->setExtends($classParent);
             $classGenerator->setImplements($classGeneratorImplements);
             $classGenerator->setProperties($classGeneratorProperties);
             $classGenerator->setMethods($classGeneratorMethods);
             $classGenerator->setConsts($classGeneratorConst);
             $classGenerator->addProperty("className", "\\" . $configName);
             $classFilename = dirname(str_replace($this->getRootDir(), $this->getDest(), $classReflection->getFileName())) . DIRECTORY_SEPARATOR . $classGenerator->getName() . ".php";
         } else {
             $classReflection = ReflectionClassType::from($classType);
             $classGenerator = PhpGeneratorClassType::from($classReflection);
             $classGeneratorConst = $classGenerator->getConsts();
             $classGeneratorProperties = $classGenerator->getProperties();
             $classGeneratorImplements = $classGenerator->getImplements();
             $classGeneratorMethods = $classGenerator->getMethods();
         }
         if ('interface' == $classGenerator->getType() || $classGenerator->isAbstract()) {
             continue;
         }
         $classGeneratorAnnotation = $classReflection->getAnnotations();
         $classAnnotationsSwitch = Arrays::get($classGeneratorAnnotation, 'no-test', FALSE);
         if (FALSE !== $classAnnotationsSwitch) {
             continue;
         }
         try {
             $instanceProvider = $classReflection->getMethod("getInstance");
         } catch (\Exception $e) {
             $instanceProvider = $classGenerator->addMethod("getInstance");
             $instanceProvider->setBody('return parent::getInstance();');
             $instanceProvider->setDocuments(array('return' => "@return \\" . $classPrototype));
         }
         $classDependency = "";
         foreach ($classDependencies as $dependency => $dependencyClass) {
             $classDependency .= "\nuse " . $dependency;
             if ('' == $dependencyClass) {
                 $classDependency .= " as " . $dependencyClass;
             }
             $classDependency .= ";";
         }
         $classCode = sprintf("<?php\nnamespace %s;\n%s\n%s", $classReflection->getNamespaceName(), $classDependency, $classGenerator);
         $this->saveClass($classCode, new \SplFileInfo($classFilename));
     }
 }