/** * @param \PhpUnitTestGenerator\Generator\TestMethod $method * @return null|\phpDocumentor\Reflection\DocBlock */ public function getParsedDocComment(\PhpUnitTestGenerator\Generator\TestMethod $method) { if ($method->getOriginalMethod()->getDocComment()) { return new \phpDocumentor\Reflection\DocBlock($method->getOriginalMethod()->getDocComment()); } return null; }
/** * checks if provider can generate for this method * * @param \PhpUnitTestGenerator\Generator\TestMethod $method * @return boolean */ public function canHandleTestableMethod(\PhpUnitTestGenerator\Generator\TestMethod $method) { if ($method->getOriginalMethod()->isStatic()) { $doc = $this->getParsedDocComment($method); if ($doc !== null) { if ($doc->hasTag("return")) { $returns = $doc->getTagsByName("return"); $return = $returns[0]; /* @var $return \phpDocumentor\Reflection\DocBlock\Tag\ReturnTag */ if ($return->getType() != "" && !in_array($return->getType(), self::$types)) { if (class_exists($return->getType()) || interface_exists($return->getType())) { return true; } } } } } return false; }
/** * add test method to test object * * @param \PhpUnitTestGenerator\Generator\TestMethod $testedMethod */ public function addTestedMethod(TestMethod $testedMethod) { if (isset($this->testedMethods[$testedMethod->getName()])) { if (is_array($this->testedMethods[$testedMethod->getName()])) { if ($testedMethod->getStatus() == TestMethod::STATUS_FINAL) { $this->testedMethods[$testedMethod->getName()][] = $testedMethod; } } elseif ($this->testedMethods[$testedMethod->getName()]->getStatus() == TestMethod::STATUS_SKELETON && $testedMethod->getStatus() == TestMethod::STATUS_FINAL) { $this->testedMethods[$testedMethod->getName()] = $testedMethod; } elseif ($this->testedMethods[$testedMethod->getName()]->getStatus() == TestMethod::STATUS_FINAL && $testedMethod->getStatus() == TestMethod::STATUS_FINAL) { //$this->testedMethods[$testedMethod->getName()] = $testedMethod; } else { if ($testedMethod->getStatus() != TestMethod::STATUS_SKELETON) { $e = $this->testedMethods[$testedMethod->getName()]; $this->testedMethods[$testedMethod->getName()] = array($e, $testedMethod); } } } else { $this->testedMethods[$testedMethod->getName()] = $testedMethod; } }
public function handleTestableMethod(\PhpUnitTestGenerator\Generator\TestMethod $method) { $method->setStatus(\PhpUnitTestGenerator\Generator\TestMethod::STATUS_FINAL); $method->setContent(\PhpUnitTestGenerator\Resource\Helper::getParsedTemplateByNameAndHash('TestMethodSingleton.tpl.dist', array('methodName' => ucfirst($method->getName()), 'origMethodName' => $method->getOriginalMethod()->getName(), 'className' => '\\' . $method->getOriginalMethod()->getDeclaringClass()->getName()))); }
/** * 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); } } } }
private function isGetterMethod(\PhpUnitTestGenerator\Generator\TestMethod $method) { return 0 != preg_match('/^(get)[A-Z0-9]/', $method->getOriginalMethod()->getName()) && $method->getOriginalMethod()->getNumberOfParameters() == 0 && false === $method->getOriginalMethod()->isStatic(); //return 0 != preg_match('/^(get|is)[A-Z0-9]/', $method->getOriginalMethod()->getName()) && $method->getOriginalMethod()->getNumberOfParameters() == 0 && false === $method->getOriginalMethod()->isStatic(); }