/**
  * Renders this node.
  *
  * @param string  $target
  * @param string  $title
  * @param string  $charset
  * @param boolean $highlight
  * @param integer $lowUpperBound
  * @param integer $highLowerBound
  * @access public
  */
 public function render($target, $title, $charset = 'ISO-8859-1', $highlight = FALSE, $lowUpperBound = 35, $highLowerBound = 70)
 {
     if ($this->yui) {
         $template = new PHPUnit_Util_Template(PHPUnit_Util_Report::$templatePath . 'file.html');
         $yuiTemplate = new PHPUnit_Util_Template(PHPUnit_Util_Report::$templatePath . 'yui_item.js');
     } else {
         $template = new PHPUnit_Util_Template(PHPUnit_Util_Report::$templatePath . 'file_no_yui.html');
     }
     $i = 1;
     $lines = '';
     $ignore = FALSE;
     foreach ($this->codeLines as $line) {
         if (strpos($line, '@codeCoverageIgnore') !== FALSE) {
             if (strpos($line, '@codeCoverageIgnoreStart') !== FALSE) {
                 $ignore = TRUE;
             } else {
                 if (strpos($line, '@codeCoverageIgnoreEnd') !== FALSE) {
                     $ignore = FALSE;
                 }
             }
         }
         $css = '';
         if (!$ignore && isset($this->executedLines[$i])) {
             $count = '';
             // Array: Line is executable and was executed.
             // count(Array) = Number of tests that hit this line.
             if (is_array($this->executedLines[$i])) {
                 $color = 'lineCov';
                 $numTests = count($this->executedLines[$i]);
                 $count = sprintf('%8d', $numTests);
                 if ($this->yui) {
                     $buffer = '';
                     foreach ($this->executedLines[$i] as $test) {
                         if (!isset($test->__liHtml)) {
                             $test->__liHtml = '';
                             if ($test instanceof PHPUnit_Framework_SelfDescribing) {
                                 $testName = $test->toString();
                                 if ($test instanceof PHPUnit_Framework_TestCase) {
                                     switch ($test->getStatus()) {
                                         case PHPUnit_Runner_BaseTestRunner::STATUS_PASSED:
                                             $testCSS = ' class=\\"testPassed\\"';
                                             break;
                                         case PHPUnit_Runner_BaseTestRunner::STATUS_FAILURE:
                                             $testCSS = ' class=\\"testFailure\\"';
                                             break;
                                         case PHPUnit_Runner_BaseTestRunner::STATUS_ERROR:
                                             $testCSS = ' class=\\"testError\\"';
                                             break;
                                         case PHPUnit_Runner_BaseTestRunner::STATUS_INCOMPLETE:
                                         case PHPUnit_Runner_BaseTestRunner::STATUS_SKIPPED:
                                             $testCSS = ' class=\\"testIncomplete\\"';
                                             break;
                                         default:
                                             $testCSS = '';
                                     }
                                 }
                             }
                             $test->__liHtml .= sprintf('<li%s>%s</li>', $testCSS, $testName);
                         }
                         $buffer .= $test->__liHtml;
                     }
                     if ($numTests > 1) {
                         $header = $numTests . ' tests cover';
                     } else {
                         $header = '1 test covers';
                     }
                     $header .= ' line ' . $i;
                     $yuiTemplate->setVar(array('line' => $i, 'header' => $header, 'tests' => $buffer), FALSE);
                     $this->yuiPanelJS .= $yuiTemplate->render();
                 }
             } else {
                 if ($this->executedLines[$i] == -1) {
                     $color = 'lineNoCov';
                     $count = sprintf('%8d', 0);
                 } else {
                     $color = 'lineDeadCode';
                     $count = '        ';
                 }
             }
             $css = sprintf('<span class="%s">       %s : ', $color, $count);
         }
         $fillup = array_shift($this->codeLinesFillup);
         if ($fillup > 0) {
             $line .= str_repeat(' ', $fillup);
         }
         $lines .= sprintf('<span class="lineNum" id="container%d"><a name="%d"></a><a href="#%d" id="line%d">%8d</a> </span>%s%s%s' . "\n", $i, $i, $i, $i, $i, !empty($css) ? $css : '                : ', !$this->highlight ? htmlspecialchars($line) : $line, !empty($css) ? '</span>' : '');
         $i++;
     }
     $items = '';
     foreach ($this->classes as $className => $classData) {
         $numCalledClasses = $classData['executedLines'] > 0 ? 1 : 0;
         $calledClassesPercent = $numCalledClasses == 1 ? 100 : 0;
         $numCalledMethods = 0;
         $numMethods = count($classData['methods']);
         foreach ($classData['methods'] as $method) {
             if ($method['executedLines'] > 0) {
                 $numCalledMethods++;
             }
         }
         $items .= $this->doRenderItem(array('name' => sprintf('<b><a href="#%d">%s</a></b>', $classData['startLine'], $className), 'numClasses' => 1, 'numCalledClasses' => $numCalledClasses, 'calledClassesPercent' => sprintf('%01.2f', $calledClassesPercent), 'numMethods' => $numMethods, 'numCalledMethods' => $numCalledMethods, 'calledMethodsPercent' => $this->calculatePercent($numCalledMethods, $numMethods), 'numExecutableLines' => $classData['executableLines'], 'numExecutedLines' => $classData['executedLines'], 'executedLinesPercent' => $this->calculatePercent($classData['executedLines'], $classData['executableLines'])), $lowUpperBound, $highLowerBound);
         foreach ($classData['methods'] as $methodName => $methodData) {
             $numCalledMethods = $methodData['executedLines'] > 0 ? 1 : 0;
             $calledMethodsPercent = $numCalledMethods == 1 ? 100 : 0;
             $items .= $this->doRenderItem(array('name' => sprintf('&nbsp;<a href="#%d">%s</a>', $methodData['startLine'], PHPUnit_Util_Class::getMethodSignature(new ReflectionMethod($className, $methodName))), 'numClasses' => '', 'numCalledClasses' => '', 'calledClassesPercent' => '', 'numMethods' => 1, 'numCalledMethods' => $numCalledMethods, 'calledMethodsPercent' => sprintf('%01.2f', $calledMethodsPercent), 'numExecutableLines' => $methodData['executableLines'], 'numExecutedLines' => $methodData['executedLines'], 'executedLinesPercent' => $this->calculatePercent($methodData['executedLines'], $methodData['executableLines'])), $lowUpperBound, $highLowerBound, 'method_item.html');
         }
     }
     $this->setTemplateVars($template, $title, $charset);
     $template->setVar(array('lines' => $lines, 'total_item' => $this->renderTotalItem($lowUpperBound, $highLowerBound, FALSE), 'items' => $items, 'yuiPanelJS' => $this->yuiPanelJS));
     $cleanId = PHPUnit_Util_Filesystem::getSafeFilename($this->getId());
     $template->renderTo($target . $cleanId . '.html');
 }
Beispiel #2
0
 protected function doRenderItem(array $data, $lowUpperBound, $highLowerBound, $template = NULL)
 {
     if ($template === NULL) {
         if ($this instanceof PHPUnit_Util_Report_Node_Directory) {
             $template = 'directory_item.html';
         } else {
             $template = 'file_item.html';
         }
     }
     $itemTemplate = new PHPUnit_Util_Template(PHPUnit_Util_Report::$templatePath . $template);
     if ($data['numClasses'] > 0) {
         list($classesColor, $classesLevel) = $this->getColorLevel($data['testedClassesPercent'], $lowUpperBound, $highLowerBound);
         $classesNumber = $data['numTestedClasses'] . ' / ' . $data['numClasses'];
     } else {
         $classesColor = 'snow';
         $classesLevel = 'None';
         $classesNumber = '&nbsp;';
     }
     if ($data['numMethods'] > 0) {
         list($methodsColor, $methodsLevel) = $this->getColorLevel($data['testedMethodsPercent'], $lowUpperBound, $highLowerBound);
         $methodsNumber = $data['numTestedMethods'] . ' / ' . $data['numMethods'];
     } else {
         $methodsColor = 'snow';
         $methodsLevel = 'None';
         $methodsNumber = '&nbsp;';
     }
     list($linesColor, $linesLevel) = $this->getColorLevel($data['executedLinesPercent'], $lowUpperBound, $highLowerBound);
     if ($data['name'] == '<b><a href="#0">*</a></b>') {
         $functions = TRUE;
     } else {
         $functions = FALSE;
     }
     $itemTemplate->setVar(array('name' => $functions ? 'Functions' : $data['name'], 'itemClass' => isset($data['itemClass']) ? $data['itemClass'] : 'coverItem', 'classes_color' => $classesColor, 'classes_level' => $functions ? 'None' : $classesLevel, 'classes_tested_width' => floor($data['testedClassesPercent']), 'classes_tested_percent' => !$functions && $data['numClasses'] > 0 ? $data['testedClassesPercent'] . '%' : '&nbsp;', 'classes_not_tested_width' => 100 - floor($data['testedClassesPercent']), 'classes_number' => $functions ? '&nbsp;' : $classesNumber, 'methods_color' => $methodsColor, 'methods_level' => $methodsLevel, 'methods_tested_width' => floor($data['testedMethodsPercent']), 'methods_tested_percent' => $data['numMethods'] > 0 ? $data['testedMethodsPercent'] . '%' : '&nbsp;', 'methods_not_tested_width' => 100 - floor($data['testedMethodsPercent']), 'methods_number' => $methodsNumber, 'lines_color' => $linesColor, 'lines_level' => $linesLevel, 'lines_executed_width' => floor($data['executedLinesPercent']), 'lines_executed_percent' => $data['executedLinesPercent'] . '%', 'lines_not_executed_width' => 100 - floor($data['executedLinesPercent']), 'num_executable_lines' => $data['numExecutableLines'], 'num_executed_lines' => $data['numExecutedLines']));
     return $itemTemplate->render();
 }
 /**
  * @param  string  $templateDir
  * @param  string  $className
  * @param  string  $methodName
  * @param  string  $modifier
  * @param  string  $arguments
  * @param  string  $reference
  * @return string
  */
 protected static function generateMockedMethodDefinition($templateDir, $className, $methodName, $modifier = 'public', $arguments = '', $reference = '')
 {
     $template = new PHPUnit_Util_Template($templateDir . 'mocked_method.tpl');
     $template->setVar(array('arguments' => $arguments, 'class_name' => $className, 'method_name' => $methodName, 'modifier' => $modifier, 'reference' => $reference));
     return $template->render();
 }
Beispiel #4
0
 protected function doRenderItem(array $data, $lowUpperBound, $highLowerBound, $template = 'item.html')
 {
     $itemTemplate = new PHPUnit_Util_Template(PHPUnit_Util_Report::$templatePath . $template);
     list($classesColor, $classesLevel) = $this->getColorLevel($data['calledClassesPercent'], $lowUpperBound, $highLowerBound);
     list($methodsColor, $methodsLevel) = $this->getColorLevel($data['calledMethodsPercent'], $lowUpperBound, $highLowerBound);
     list($linesColor, $linesLevel) = $this->getColorLevel($data['executedLinesPercent'], $lowUpperBound, $highLowerBound);
     $itemTemplate->setVar(array('name' => $data['name'], 'itemClass' => isset($data['itemClass']) ? $data['itemClass'] : 'coverItem', 'classes_color' => $classesColor, 'classes_level' => $classesLevel, 'classes_called_width' => floor($data['calledClassesPercent']), 'classes_called_percent' => $data['calledClassesPercent'], 'classes_not_called_width' => 100 - floor($data['calledClassesPercent']), 'num_classes' => $data['numClasses'], 'num_called_classes' => $data['numCalledClasses'], 'methods_color' => $methodsColor, 'methods_level' => $methodsLevel, 'methods_called_width' => floor($data['calledMethodsPercent']), 'methods_called_percent' => $data['calledMethodsPercent'], 'methods_not_called_width' => 100 - floor($data['calledMethodsPercent']), 'num_methods' => $data['numMethods'], 'num_called_methods' => $data['numCalledMethods'], 'lines_color' => $linesColor, 'lines_level' => $linesLevel, 'lines_executed_width' => floor($data['executedLinesPercent']), 'lines_executed_percent' => $data['executedLinesPercent'], 'lines_not_executed_width' => 100 - floor($data['executedLinesPercent']), 'num_executable_lines' => $data['numExecutableLines'], 'num_executed_lines' => $data['numExecutedLines']));
     return $itemTemplate->render();
 }
Beispiel #5
0
 /**
  * Generates the class' source.
  *
  * @return mixed
  */
 public function generate()
 {
     $methods = '';
     foreach ($this->findTestedMethods() as $method) {
         $methodTemplate = new PHPUnit_Util_Template(sprintf('%s%sTemplate%sMethod.tpl', dirname(__FILE__), DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR));
         $methodTemplate->setVar(array('methodName' => $method));
         $methods .= $methodTemplate->render();
     }
     $classTemplate = new PHPUnit_Util_Template(sprintf('%s%sTemplate%sClass.tpl', dirname(__FILE__), DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR));
     $classTemplate->setVar(array('className' => $this->outClassName['fullyQualifiedClassName'], 'methods' => $methods, 'date' => date('Y-m-d'), 'time' => date('H:i:s')));
     return $classTemplate->render();
 }
Beispiel #6
0
 /**
  * Generates the test class' source.
  *
  * @param  boolean $verbose
  * @return mixed
  */
 public function generate($verbose = FALSE)
 {
     $class = new ReflectionClass($this->className);
     $methods = '';
     $incompleteMethods = '';
     foreach ($class->getMethods() as $method) {
         if (!$method->isConstructor() && !$method->isAbstract() && $method->isPublic() && $method->getDeclaringClass()->getName() == $this->className) {
             $assertAnnotationFound = FALSE;
             if (preg_match_all('/@assert(.*)$/Um', $method->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();
                         }
                         if ($assertion == 'exception') {
                             $template = 'TestMethodException';
                         } else {
                             if ($assertion == 'Equals' && strtolower($matches[3]) == 'true') {
                                 $assertion = 'True';
                                 $template = 'TestMethodBool';
                             } else {
                                 if ($assertion == 'NotEquals' && strtolower($matches[3]) == 'true') {
                                     $assertion = 'False';
                                     $template = 'TestMethodBool';
                                 } else {
                                     if ($assertion == 'Equals' && strtolower($matches[3]) == 'false') {
                                         $assertion = 'False';
                                         $template = 'TestMethodBool';
                                     } else {
                                         if ($assertion == 'NotEquals' && strtolower($matches[3]) == 'false') {
                                             $assertion = 'True';
                                             $template = 'TestMethodBool';
                                         } else {
                                             $template = 'TestMethod';
                                         }
                                     }
                                 }
                             }
                         }
                         if ($method->isStatic()) {
                             $template .= 'Static';
                         }
                         $methodTemplate = new PHPUnit_Util_Template(sprintf('%s%sSkeleton%s%s.tpl', dirname(__FILE__), DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR, $template));
                         $origMethodName = $method->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' => $this->className, 'methodName' => $methodName));
                         $methods .= $methodTemplate->render();
                         $assertAnnotationFound = TRUE;
                     }
                 }
             }
             if (!$assertAnnotationFound) {
                 $methodTemplate = new PHPUnit_Util_Template(sprintf('%s%sSkeleton%sIncompleteTestMethod.tpl', dirname(__FILE__), DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR));
                 $methodTemplate->setVar(array('methodName' => ucfirst($method->getName())));
                 $incompleteMethods .= $methodTemplate->render();
             }
         }
     }
     $classTemplate = new PHPUnit_Util_Template(sprintf('%s%sSkeleton%sTestClass.tpl', dirname(__FILE__), DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR));
     if ($this->classSourceFile != '<internal>') {
         $requireClassFile = sprintf("\n\nrequire_once '%s';", $this->classSourceFile);
     } else {
         $requireClassFile = '';
     }
     $classTemplate->setVar(array('className' => $this->className, 'requireClassFile' => $requireClassFile, 'methods' => $methods . $incompleteMethods, 'date' => date('Y-m-d'), 'time' => date('H:i:s')));
     if (!$verbose) {
         return $classTemplate->render();
     } else {
         return array('code' => $classTemplate->render(), 'incomplete' => empty($methods));
     }
 }
Beispiel #7
0
 /**
  * Handler for 'end run' event.
  *
  */
 protected function endRun()
 {
     $scenariosTemplate = new PHPUnit_Util_Template($this->templatePath . 'scenarios.html');
     $scenariosTemplate->setVar(array('scenarios' => $this->scenarios, 'successfulScenarios' => $this->successful, 'failedScenarios' => $this->failed, 'skippedScenarios' => $this->skipped, 'incompleteScenarios' => $this->incomplete));
     $this->write($scenariosTemplate->render());
 }
Beispiel #8
0
 /**
  * @param  array    $items
  * @param  boolean  $includeDetails
  * @return string
  * @access protected
  */
 protected function doRenderItems(array $items, $includeDetails)
 {
     $result = '';
     foreach ($items as $item) {
         $itemTemplate = new PHPUnit_Util_Template(PHPUnit_Util_Report::getTemplatePath() . 'coverage_item.html');
         $details = '';
         if ($includeDetails) {
             foreach ($item->getCoveringTests() as $suite => $tests) {
                 $detailsHeaderTemplate = new PHPUnit_Util_Template(PHPUnit_Util_Report::getTemplatePath() . 'coverage_item_details_header.html');
                 $detailsHeaderTemplate->setVar('link', sprintf('<a href="%s-test.html">%s</a>', PHPUnit_Util_Filesystem::getSafeFilename($suite), $suite));
                 $details .= $detailsHeaderTemplate->render();
                 foreach ($tests as $test => $_test) {
                     $detailsTemplate = new PHPUnit_Util_Template(PHPUnit_Util_Report::getTemplatePath() . 'coverage_item_details.html');
                     if ($_test['object']->getResult() !== PHPUnit_Runner_BaseTestRunner::STATUS_PASSED) {
                         $failure = sprintf('<br /><pre>%s</pre>', htmlspecialchars($_test['object']->getResult()->exceptionMessage()));
                     } else {
                         $failure = '';
                     }
                     $detailsTemplate->setVar(array('item', 'executed_percent', 'executed_lines', 'executable_lines'), array($test . $failure, sprintf('%01.2f', $_test['numLinesExecuted'] / $item->getNumExecutableLines() * 100), $_test['numLinesExecuted'], $item->getNumExecutableLines()));
                     $details .= $detailsTemplate->render();
                 }
             }
         }
         $floorPercent = floor($item->getExecutedPercent());
         if ($floorPercent < self::LOW_UPPER_BOUND) {
             $color = 'scarlet_red';
             $level = 'Lo';
         } else {
             if ($floorPercent >= self::LOW_UPPER_BOUND && $floorPercent < self::HIGH_LOWER_BOUND) {
                 $color = 'butter';
                 $level = 'Med';
             } else {
                 $color = 'chameleon';
                 $level = 'Hi';
             }
         }
         $itemTemplate->setVar(array('link', 'color', 'level', 'executed_width', 'executed_percent', 'not_executed_width', 'executable_lines', 'executed_lines', 'details'), array($item->getLink(FALSE, FALSE), $color, $level, floor($item->getExecutedPercent()), $item->getExecutedPercent(), 100 - floor($item->getExecutedPercent()), $item->getNumExecutableLines(), $item->getNumExecutedLines(), $details));
         $result .= $itemTemplate->render();
     }
     return $result;
 }
Beispiel #9
0
 /**
  * @return string
  * @access protected
  */
 protected function renderItems()
 {
     $result = '';
     foreach ($this->tests as $item) {
         $itemTemplate = new PHPUnit_Util_Template(PHPUnit_Util_Report::getTemplatePath() . 'testsuite_item.html');
         $resultCode = $item->getResult();
         if ($resultCode instanceof PHPUnit_Framework_TestFailure) {
             if ($resultCode->isFailure()) {
                 $testResult = 'Failure';
             } else {
                 if ($resultCode->thrownException() instanceof PHPUnit_Framework_SkippedTest) {
                     $testResult = 'Skipped';
                 } else {
                     if ($resultCode->thrownException() instanceof PHPUnit_Framework_IncompleteTest) {
                         $testResult = 'Incomplete';
                     } else {
                         $testResult = 'Error';
                     }
                 }
             }
         } else {
             if ($resultCode === PHPUnit_Runner_BaseTestRunner::STATUS_PASSED) {
                 $testResult = 'Passed';
             } else {
                 $testResult = 'Error';
             }
         }
         switch ($testResult) {
             case 'Passed':
             case 'Skipped':
                 $color = 'chameleon';
                 break;
             case 'Incomplete':
                 $color = 'butter';
                 break;
             case 'Error':
             case 'Failure':
             default:
                 $color = 'scarlet_red';
         }
         $itemTemplate->setVar(array('color', 'result', 'name'), array($color, $testResult, $item->getName()));
         $result .= $itemTemplate->render();
     }
     return $result;
 }