/** * fill the method with a body(test) * * @param \PhpUnitTestGenerator\Generator\TestMethod $method * @return void */ public function handleTestableMethod(\PhpUnitTestGenerator\Generator\TestMethod $method) { //$method->setStatus(\PhpUnitTestGenerator\Generator\TestMethod::STATUS_FINAL); $doc = $this->getParsedDocComment($method); if ($doc !== null) { if ($doc->hasTag("return")) { $returns = $doc->getTagsByName("return"); $return = $returns[0]; $type = $return->getType(); } } if ($method->getOriginalMethod()->isStatic()) { $tpl = 'TestMethodInternalTypeStatic.tpl.dist'; } else { $tpl = 'TestMethodInternalType.tpl.dist'; } $method->setStatus(\PhpUnitTestGenerator\Generator\TestMethod::STATUS_ADDITIONAL); $method->setContent(\PhpUnitTestGenerator\Resource\Helper::getParsedTemplateByNameAndHash($tpl, array('methodName' => ucfirst($method->getName()), 'origMethodName' => $method->getOriginalMethod()->getName(), 'className' => '\\' . $method->getOriginalMethod()->getDeclaringClass()->getName(), 'type' => $type, 'arguments' => $this->getPropertyDummyStub($method, true)))); }
/** * fill the method with a body(test) * * @param \PhpUnitTestGenerator\Generator\TestMethod $method * @return void */ public function handleTestableMethod(\PhpUnitTestGenerator\Generator\TestMethod $method) { //$method->setStatus(\PhpUnitTestGenerator\Generator\TestMethod::STATUS_FINAL); $doc = $this->getParsedDocComment($method); if ($doc !== null) { if ($doc->hasTag("return")) { $returns = $doc->getTagsByName("return"); $return = $returns[0]; $type = $return->getType(); } } /* $this->assert{assertion}( {expected}, {className}::{origMethodName}({arguments}) ); */ $method->setStatus(\PhpUnitTestGenerator\Generator\TestMethod::STATUS_ADDITIONAL); $method->setContent(\PhpUnitTestGenerator\Resource\Helper::getParsedTemplateByNameAndHash('TestMethodStatic.tpl.dist', array('methodName' => ucfirst($method->getName()), 'origMethodName' => $method->getOriginalMethod()->getName(), 'className' => '\\' . $method->getOriginalMethod()->getDeclaringClass()->getName(), 'type' => $type, 'arguments' => $this->getPropertyDummyStub($method, true), 'assertion' => 'InstanceOf', 'expected' => '"' . str_replace('\\', '\\\\', $type) . '"'))); }
/** * 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(); }
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); } } } }
public function handleTestableMethod(\PhpUnitTestGenerator\Generator\TestMethod $method) { if ($this->isGetterMethod($method)) { $dummyValueType = "string"; foreach ($method->getOriginalMethod()->getDocBlocks()->getTags() as $tag) { /* @var $tag \phpDocumentor\Reflection\DocBlock\Tag\ReturnTag */ if ($tag->getName() == "return") { $dummyValueType = $tag->getType(); break; } } $dummyValue = Generator::getInstance()->getValueByType($dummyValueType); $excepted = Generator::getInstance()->getExceptedValueByType($dummyValueType); $assertion = Generator::getInstance()->getAssertionByType($dummyValueType); $method->setStatus(\PhpUnitTestGenerator\Generator\TestMethod::STATUS_FINAL); $method->setContent(\PhpUnitTestGenerator\Resource\Helper::getParsedTemplateByNameAndHash('TestMethodGetter.tpl.dist', array('methodName' => ucfirst($method->getName()), 'origMethodName' => $method->getOriginalMethod()->getName(), 'className' => $method->getOriginalMethod()->getDeclaringClass()->getName(), 'methodShort' => str_replace('get', '', $method->getOriginalMethod()->getName()), 'dummyValue' => $dummyValue, 'excepted' => $excepted, 'assertion' => $assertion))); } elseif ($this->isSetterMethod($method)) { $dummyValueType = "string"; foreach ($method->getOriginalMethod()->getDocBlocks()->getTags() as $tag) { /* @var $tag \phpDocumentor\Reflection\DocBlock\Tag\ParamTag */ if ($tag->getName() == "param") { $dummyValueType = $tag->getType(); break; } } $dummyValue = Generator::getInstance()->getValueByType($dummyValueType); $excepted = Generator::getInstance()->getExceptedValueByType($dummyValueType); $assertion = Generator::getInstance()->getAssertionByType($dummyValueType); $method->setStatus(\PhpUnitTestGenerator\Generator\TestMethod::STATUS_ADDITIONAL); $method->setContent(\PhpUnitTestGenerator\Resource\Helper::getParsedTemplateByNameAndHash('TestMethodSetter.tpl.dist', array('methodName' => ucfirst($method->getName()), 'origMethodName' => $method->getOriginalMethod()->getName(), 'className' => $method->getOriginalMethod()->getDeclaringClass()->getName(), 'methodShort' => str_replace('set', '', $method->getOriginalMethod()->getName()), 'dummyValue' => $dummyValue, 'excepted' => $excepted, 'assertion' => $assertion))); } }