Last active
          December 14, 2015 09:39 
        
      - 
      
- 
        Save ngyuki/5066179 to your computer and use it in GitHub Desktop. 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | <?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; | |
| } | |
| } | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | <?php | |
| class TraitSampleTest extends PHPUnit_Framework_TestCase | |
| { | |
| use TraitSampleTrait; | |
| protected function getOne() | |
| { | |
| return 2; | |
| } | |
| } | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | <?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