CodeView

PHPUnit test harness for Komodo

MyTestFormatter.php

<?php
 
# From http://www.getinstance.com/blog/index.php?id=29

# Modified based on PHPUnit/TextUI/TestRunner.php

class KoUnit_Formatter implements  PHPUnit_Framework_TestListener {
 
    var $owe_started;
    var $did_finished;
    public function __construct() {
        $this->owe_started = true;
        $this->did_finished = true;
    }
 
    # Taken from PHPUnit/Framework/AssertionFailedError.php
    # removed in http://www.phpunit.de/changeset/4809
    # because this wasn't called by any other code in PHPUnit
    # (repeat: it's never a good idea to modify a class
    # public interface in a non-backward-compliant way).
    private function getLocation(Exception $e) {
        foreach ($e->getTrace() as $frame) {
            if (!isset($frame['line'])) {
                break;
            }
 
            $result = array(
              'file' => $frame['file'],
              'line' => $frame['line']
            );
        }
 
        return $result;
    }
 
    private function _emitFaultInfo(Exception $e) {
        print "@fault@:\n";
        print $e->getMessage() . "\n";
        if ($e instanceof PHPUnit_Framework_AssertionFailedError) {
           $location = (method_exists($e, 'getLocation')
                        ? $e->getLocation()
                        : $this->getLocation($e));
            printf("in %s on line %d\n", $location['file'], $location['line']);
        }
        $this->testSucceeded = false;
    }
 
    /**
     * An error occurred.
     *
     * @param  PHPUnit_Framework_Test $test
     * @param  Exception              $e
     * @param  float                  $time
     * @access public
     */
    public function addError(PHPUnit_Framework_Test $test, Exception $e, $time) {
        $this->_emitFaultInfo($e);
        $this->numErrors += 1;
        $this->testResultCode = "E";
    }
 
    /**
     * A failure occurred.
     *
     * @param  PHPUnit_Framework_Test                 $test
     * @param  PHPUnit_Framework_AssertionFailedError $e
     * @param  float                                  $time
     * @access public
     */
    public function addFailure(PHPUnit_Framework_Test $test, PHPUnit_Framework_AssertionFailedError $e, $time)
    {
        $this->_emitFaultInfo($e);
        $this->numFailures += 1;
        $this->testResultCode = "F";
    }
 
    public function addIncompleteTest(  PHPUnit_Framework_Test $test,
                                        Exception $e, $time) {
        $this->numIncomplete += 1;
        $this->testSucceeded = false;
        $this->testResultCode = "I";
    }
 
    public function addSkippedTest(  PHPUnit_Framework_Test $test,
                                        Exception $e, $time) {
        $this->numSkipped += 1;
        $this->testSucceeded = false;
        $this->testResultCode = "S";
    }
 
    public function startTestSuite( PHPUnit_Framework_TestSuite $suite) {
        $suiteName = $suite->getName();
        if (!$suiteName) {
            return;
        }
        $this->owe_started = false;
        $this->did_finished = false;
        $this->numTests = count($suite); // Not sure what this is...
        $this->numCountedTests = 0;
        $this->numErrors = 0;
        $this->numFailures = 0;
        $this->numPassed = 0;
        $this->numSkipped = 0;
        $this->numIncomplete = 0;
        $this->testSuiteTime = 0;
        printf("@suite_started@:\n");
    }
 
    public function endTestSuite( PHPUnit_Framework_TestSuite $suite) {
        if ($this->did_finished) {
            #print("Ignoring extra endTestSuite\n");
            return;
        } else {
            $this->did_finished = true;
        }
        $this->did_started = false;
        $str = sprintf(" N:%d P:%d F:%d E:%d",
                        $this->numCountedTests, // Not $this->numTests
                        $this->numPassed,
                        $this->numFailures,
                        $this->numErrors);
        if ($this->numSkipped > 0) {
            $str .= sprintf(" S:%d", $this->numSkipped);
        }
        if ($this->numIncomplete > 0) {
            $str .= sprintf(" I:%d", $this->numIncomplete);
        }
        printf("@suite_finished@:%s; %f\n", $str, $this->testSuiteTime);
        $this->testSuiteTime = 0;
        $this->owe_started = true;
    }
 
    public function startTest(PHPUnit_Framework_Test $test) {
        if ($this->owe_started) {
            $this->owe_started = false;
            printf("@suite_started@:\n");
        }
        printf("@test_started@: %s\n", $test->getName());
        $this->testSucceeded = true;
        $this->testResultCode = "P";
        $this->numCountedTests += 1;
    }
 
    public function endTest(PHPUnit_Framework_Test $test, $time)
    {
        printf("@test_result@: %s\n", $this->testResultCode);
        if ($this->testSucceeded) {
            $this->numPassed += 1;
        }
        $this->testSuiteTime += $time;
    }
 
}
?>