Skip to content

Instantly share code, notes, and snippets.

@ngyuki
Last active December 14, 2015 09:39
Show Gist options
  • Save ngyuki/5066179 to your computer and use it in GitHub Desktop.
Save ngyuki/5066179 to your computer and use it in GitHub Desktop.
<?php
namespace Stagehand\TestRunner\Util;
require_once 'PHPUnit/Framework/Assert/Functions.php';
spl_autoload_register(function ($name) {
$fn = __DIR__ . DIRECTORY_SEPARATOR . strtr($name, '_', '/') . '.php';
if (file_exists($fn))
{
return include $fn;
}
return false;
});
class FailureTrace
{
/**
* @param array $testClassSuperTypes
* @param \Exception $e
* @param \ReflectionClass $class
* @return array
*/
public static function findFileAndLineOfFailureOrError(array $testClassSuperTypes, \Exception $e, \ReflectionClass $class)
{
if (in_array($class->getName(), $testClassSuperTypes)) return;
if ($e->getFile() == $class->getFileName()) {
return array($e->getFile(), $e->getLine());
}
foreach ($e->getTrace() as $trace) {
if (array_key_exists('file', $trace) && $trace['file'] == $class->getFileName()) {
return array($trace['file'], $trace['line']);
}
}
//*
foreach ($class->getTraits() as $trait) {
$ret = self::findFileAndLineOfFailureOrError($testClassSuperTypes, $e, $trait);
if ($ret) {
return $ret;
}
}
/**/
return self::findFileAndLineOfFailureOrError($testClassSuperTypes, $e, $class->getParentClass());
}
/**
* @param array $backtrace
* @return string
*/
public static function buildFailureTrace(array $backtrace)
{
$failureTrace = '';
for ($i = 0, $count = count($backtrace); $i < $count; ++$i) {
if (!array_key_exists('file', $backtrace[$i])) {
continue;
}
$failureTrace .=
$backtrace[$i]['file'] .
':' .
(array_key_exists('line', $backtrace[$i]) ? $backtrace[$i]['line']
: '?') .
PHP_EOL;
}
return $failureTrace;
}
}
<?php
class TraitSampleTest extends PHPUnit_Framework_TestCase
{
use TraitSampleTrait;
protected function getOne()
{
return 2;
}
}
<?php
trait TraitSampleTrait
{
abstract protected function getOne();
function testOne()
{
assertEquals(1, $this->getOne());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment