/**
  * checks if provider can finalize(no other generators required) the object
  *
  * @param \PhpUnitTestGenerator\Testable\Object $object
  * @return boolean
  */
 public function canFinalizeTestableObject(\PhpUnitTestGenerator\Testable\Object $object)
 {
     if (false === class_exists("IntelliShop\\Framework\\Entity\\Db2PhpEntityBase")) {
         return false;
     }
     return $object->isChildOfClass("IntelliShop\\Framework\\Entity\\Db2PhpEntityBase");
 }
示例#2
0
 public function logException(\Exception $exception, \PhpUnitTestGenerator\Testable\Object $object)
 {
     $file = Configuration::getInstance()->getTargetDirectory() . DIRECTORY_SEPARATOR . "test-generator-error.log";
     if (!file_exists(Configuration::getInstance()->getTargetDirectory())) {
         mkdir(Configuration::getInstance()->getTargetDirectory());
         chmod(Configuration::getInstance()->getTargetDirectory(), 0755);
     }
     file_put_contents($file, $exception->getMessage() . " in file " . $object->getFilename() . "\n\n", FILE_APPEND);
     //file_put_contents($file, serialize($exception) . "\n\n\n");
 }
示例#3
0
 public function getConstructorArgs()
 {
     try {
         $constructor = $this->object->getConstructor();
         if ($constructor !== null) {
             return Data\Generator::getPropsAsStringByReflectionMethod($constructor);
         }
         return '';
     } catch (\Exception $e) {
         echo "Problem with constructor in class " . $this->getOriginalFullClassName() . "\n";
         echo $e;
         exit;
     }
 }
 /**
  * generate one test from a single testable object and writes it to fs
  *
  * @param \PhpUnitTestGenerator\Testable\Object $object
  * @return \PhpUnitTestGenerator\Generator\Test
  */
 public function generateTestFromTestable(\PhpUnitTestGenerator\Testable\Object $object)
 {
     $test = new Test($this->getFilenameOfTestByOriginalFilename($object->getFilename()), $object);
     $test->setBaseClass($this->configuration->getBaseClass());
     $isTestFinalized = false;
     foreach ($this->getObjectProviders() as $objectProvider) {
         /* @var $objectProvider Provider\Definition\ObjectInterface */
         if (true === $objectProvider->canHandleTestableObject($object)) {
             if (true === $objectProvider->canFinalizeTestableObject($object)) {
                 $objectProvider->handleTestableObject($object, $test);
                 $isTestFinalized = true;
             } elseif ($isTestFinalized === false) {
                 $objectProvider->handleTestableObject($object, $test);
             }
         }
     }
     if ($isTestFinalized === false) {
         foreach ($this->getMethodProviders() as $methodProvider) {
             /* @var $methodProvider Provider\Definition\MethodInterface */
             foreach ($test->getTestedMethods() as $testMethod) {
                 try {
                     if (true === $methodProvider->canHandleTestableMethod($testMethod)) {
                         if (true === $methodProvider->canFinalizeTestableMethod($testMethod)) {
                             $methodProvider->handleTestableMethod($testMethod);
                             $test->addTestedMethod($testMethod);
                             $isTestFinalized = true;
                         } elseif ($isTestFinalized) {
                             $methodProvider->handleTestableMethod($testMethod);
                             $test->addTestedMethod($testMethod);
                         }
                     }
                 } catch (\Exception $e) {
                     Logger::getInstance()->logException($e, $object);
                 }
             }
         }
     }
     /**/
     $test->write();
     return $test;
 }
 /**
  * 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);
             }
         }
     }
 }