Esempio n. 1
0
 /**
  * renders the test object to real PHP code
  *
  * @return string
  */
 public function render()
 {
     $nsMappings = Configuration::getInstance()->getNamespaceMappings();
     if ($this->getNamespace() !== null && $this->getNamespace() != "") {
         $nsName = null;
         if (isset($nsMappings[$this->getNamespace()])) {
             $nsName = $nsMappings[$this->getNamespace()];
         } else {
             $nsOriginal = null;
             $nsTester = null;
             foreach ($nsMappings as $nsOrig => $nsTest) {
                 if (0 === strpos($this->getNamespace(), $nsOrig)) {
                     $nsOriginal = $nsOrig;
                     $nsTester = $nsTest;
                     break;
                 }
             }
             if ($nsOriginal !== null && $nsTester !== null) {
                 $nsName = str_replace($nsOriginal, $nsTester, $this->getNamespace());
             }
         }
         if ($nsName !== null) {
             $ns = "\nnamespace " . $nsName . ";";
             $ns .= "\nuse " . $this->getNamespace() . ";";
         } else {
             $ns = "\nnamespace " . $this->getNamespace() . ";";
         }
     } else {
         $ns = "";
     }
     $methods = "";
     foreach ($this->getWriteableTestedMethods() as $testedMethodContent) {
         $methods .= $testedMethodContent . "\n";
     }
     $tpl = new \Text_Template(\PhpUnitTestGenerator\Resource\Helper::getTemplateFileByName($this->getTestClassTemplate()));
     $tpl->setVar(array('namespace' => $ns, 'testClassName' => $this->getClassname(), 'className' => '\\' . $this->getOriginalFullClassName(), 'baseTestClass' => $this->getBaseClass(), 'methods' => $methods, 'constructorArgs' => $this->getConstructorArgs()));
     return $tpl->render();
 }
 /**
  * fill the test class with tests
  *
  * @param \PhpUnitTestGenerator\Testable\Object $object
  * @param \PhpUnitTestGenerator\Generator\Test $test
  */
 public function handleTestableObject(\PhpUnitTestGenerator\Testable\Object $object, \PhpUnitTestGenerator\Generator\Test $test)
 {
     foreach ($object->getClassMethods() as $classMethod) {
         if (!$classMethod->isConstructor() && !$classMethod->isAbstract() && $classMethod->isPublic()) {
             $assertAnnotationFound = false;
             if (preg_match_all('/@assert(.*)$/Um', $classMethod->getDocComment(), $annotations)) {
                 foreach ($annotations[1] as $annotation) {
                     if (preg_match('/\\((.*)\\)\\s+([^\\s]*)\\s+(.*)/', $annotation, $matches)) {
                         switch ($matches[2]) {
                             case '==':
                                 $assertion = 'Equals';
                                 break;
                             case '!=':
                                 $assertion = 'NotEquals';
                                 break;
                             case '===':
                                 $assertion = 'Same';
                                 break;
                             case '!==':
                                 $assertion = 'NotSame';
                                 break;
                             case '>':
                                 $assertion = 'GreaterThan';
                                 break;
                             case '>=':
                                 $assertion = 'GreaterThanOrEqual';
                                 break;
                             case '<':
                                 $assertion = 'LessThan';
                                 break;
                             case '<=':
                                 $assertion = 'LessThanOrEqual';
                                 break;
                             case 'throws':
                                 $assertion = 'exception';
                                 break;
                             default:
                                 throw new \RuntimeException(sprintf('Token "%s" could not be parsed in @assert annotation.', $matches[2]));
                         }
                         if ($assertion == 'exception') {
                             $template = 'TestMethodException';
                         } elseif ($assertion == 'Equals' && strtolower($matches[3]) == 'true') {
                             $assertion = 'True';
                             $template = 'TestMethodBool';
                         } elseif ($assertion == 'NotEquals' && strtolower($matches[3]) == 'true') {
                             $assertion = 'False';
                             $template = 'TestMethodBool';
                         } elseif ($assertion == 'Equals' && strtolower($matches[3]) == 'false') {
                             $assertion = 'False';
                             $template = 'TestMethodBool';
                         } elseif ($assertion == 'NotEquals' && strtolower($matches[3]) == 'false') {
                             $assertion = 'True';
                             $template = 'TestMethodBool';
                         } else {
                             $template = 'TestMethod';
                         }
                         if ($classMethod->isStatic()) {
                             $template .= 'Static';
                         }
                         $templateFile = \PhpUnitTestGenerator\Resource\Helper::getTemplateFileByName($template . ".tpl.dist");
                         $methodTemplate = new \Text_Template($templateFile);
                         $origMethodName = $classMethod->getName();
                         $methodName = ucfirst($origMethodName);
                         if (isset($this->methodNameCounter[$methodName])) {
                             $this->methodNameCounter[$methodName]++;
                         } else {
                             $this->methodNameCounter[$methodName] = 1;
                         }
                         if ($this->methodNameCounter[$methodName] > 1) {
                             $methodName .= $this->methodNameCounter[$methodName];
                         }
                         $methodTemplate->setVar(array('annotation' => trim($annotation), 'arguments' => $matches[1], 'assertion' => isset($assertion) ? $assertion : '', 'expected' => $matches[3], 'origMethodName' => $origMethodName, 'className' => '\\' . $object->getName(), 'methodName' => $methodName));
                         $testMethod = \PhpUnitTestGenerator\Generator\TestMethod::createFromReflectionMethod($classMethod);
                         $testMethod->setStatus(\PhpUnitTestGenerator\Generator\TestMethod::STATUS_FINAL);
                         $testMethod->setContent($methodTemplate->render());
                         $test->addTestedMethod($testMethod);
                         $assertAnnotationFound = true;
                     }
                 }
             }
             if (!$assertAnnotationFound) {
                 //ignore internal classes
                 if ($classMethod->getDeclaringClass()->isInternal()) {
                     continue;
                 }
                 $templateFile = \PhpUnitTestGenerator\Resource\Helper::getTemplateFileByName("IncompleteTestMethod.tpl.dist");
                 $methodTemplate = new \Text_Template($templateFile);
                 $methodTemplate->setVar(array('className' => '\\' . $object->getName(), 'methodName' => ucfirst($classMethod->getName()), 'origMethodName' => $classMethod->getName()));
                 //$incompleteMethods .= $methodTemplate->render();
                 $testMethod = \PhpUnitTestGenerator\Generator\TestMethod::createFromReflectionMethod($classMethod);
                 $testMethod->setStatus(\PhpUnitTestGenerator\Generator\TestMethod::STATUS_SKELETON);
                 $testMethod->setContent($methodTemplate->render());
                 $test->addTestedMethod($testMethod);
             }
         }
     }
 }