public static function getFilteredStackTrace(Exception $e, $asString = true) { $stackTrace = $asString ? '' : array(); $trace = $e->getPrevious() ? $e->getPrevious()->getTrace() : $e->getTrace(); if ($e instanceof \PHPUnit_Framework_ExceptionWrapper) { $trace = $e->getSerializableTrace(); } foreach ($trace as $step) { if (self::classIsFiltered($step)) { continue; } if (self::fileIsFiltered($step)) { continue; } if (!$asString) { $stackTrace[] = $step; continue; } if (!isset($step['file'])) { continue; } $stackTrace .= $step['file'] . ':' . $step['line'] . "\n"; } return $stackTrace; }
/** * A test ended. * * @param PHPUnit_Framework_Test $test * @param float $time */ public function endTest(PHPUnit_Framework_Test $test, $time) { if (!$test instanceof PHPUnit_Framework_TestCase) { return; } /* @var PHPUnit_Framework_TestCase $test */ $groups = array_filter($test->getGroups(), function ($group) { if ($group == 'small' || $group == 'medium' || $group == 'large') { return false; } return true; }); $node = $this->document->createElement('test'); $node->setAttribute('className', get_class($test)); $node->setAttribute('methodName', $test->getName()); $node->setAttribute('prettifiedClassName', $this->prettifier->prettifyTestClass(get_class($test))); $node->setAttribute('prettifiedMethodName', $this->prettifier->prettifyTestMethod($test->getName())); $node->setAttribute('status', $test->getStatus()); $node->setAttribute('time', $time); $node->setAttribute('size', $test->getSize()); $node->setAttribute('groups', implode(',', $groups)); $inlineAnnotations = PHPUnit_Util_Test::getInlineAnnotations(get_class($test), $test->getName()); if (isset($inlineAnnotations['given']) && isset($inlineAnnotations['when']) && isset($inlineAnnotations['then'])) { $node->setAttribute('given', $inlineAnnotations['given']['value']); $node->setAttribute('givenStartLine', $inlineAnnotations['given']['line']); $node->setAttribute('when', $inlineAnnotations['when']['value']); $node->setAttribute('whenStartLine', $inlineAnnotations['when']['line']); $node->setAttribute('then', $inlineAnnotations['then']['value']); $node->setAttribute('thenStartLine', $inlineAnnotations['then']['line']); } if ($this->exception !== null) { if ($this->exception instanceof PHPUnit_Framework_Exception) { $steps = $this->exception->getSerializableTrace(); } else { $steps = $this->exception->getTrace(); } $class = new ReflectionClass($test); $file = $class->getFileName(); foreach ($steps as $step) { if (isset($step['file']) && $step['file'] == $file) { $node->setAttribute('exceptionLine', $step['line']); break; } } $node->setAttribute('exceptionMessage', $this->exception->getMessage()); } $this->root->appendChild($node); }
/** * Filters stack frames from PHPUnit classes. * * @param Exception $e * @param bool $asString * @return string */ public static function getFilteredStacktrace(Exception $e, $asString = true) { $prefix = false; $script = realpath($GLOBALS['_SERVER']['SCRIPT_NAME']); if (defined('__PHPUNIT_PHAR_ROOT__')) { $prefix = __PHPUNIT_PHAR_ROOT__; } if ($asString === true) { $filteredStacktrace = ''; } else { $filteredStacktrace = array(); } if ($e instanceof PHPUnit_Framework_SyntheticError) { $eTrace = $e->getSyntheticTrace(); $eFile = $e->getSyntheticFile(); $eLine = $e->getSyntheticLine(); } elseif ($e instanceof PHPUnit_Framework_Exception) { $eTrace = $e->getSerializableTrace(); $eFile = $e->getFile(); $eLine = $e->getLine(); } else { if ($e->getPrevious()) { $e = $e->getPrevious(); } $eTrace = $e->getTrace(); $eFile = $e->getFile(); $eLine = $e->getLine(); } if (!self::frameExists($eTrace, $eFile, $eLine)) { array_unshift($eTrace, array('file' => $eFile, 'line' => $eLine)); } $blacklist = new PHPUnit_Util_Blacklist(); foreach ($eTrace as $frame) { if (isset($frame['file']) && is_file($frame['file']) && !$blacklist->isBlacklisted($frame['file']) && ($prefix === false || strpos($frame['file'], $prefix) !== 0) && $frame['file'] !== $script) { if ($asString === true) { $filteredStacktrace .= sprintf("%s:%s\n", $frame['file'], isset($frame['line']) ? $frame['line'] : '?'); } else { $filteredStacktrace[] = $frame; } } } return $filteredStacktrace; }