<?php

require_once 'PHPUnit/Util/Filter.php';

PHPUnit_Util_Filter::addFileToFilter(__FILE__);

require_once 'PHPUnit/TextUI/ResultPrinter.php';

class PHPEdit_ResultPrinter extends PHPUnit_TextUI_ResultPrinter
{
	const STATUS_UNKNOWN = 'Unknown';
	const STATUS_INCOMPLETE = 'Incomplete';
	const STATUS_SKIPPED = 'Skipped';
	const STATUS_FAILED = 'Failed';
	const STATUS_Error = 'Error';
	const STATUS_SUCCEEDED = 'Succeeded';

	private $testResult = '';
	private $message = '';
	private $status = PHPEdit_ResultPrinter::STATUS_UNKNOWN;

	public function addError(PHPUnit_Framework_Test $test, Exception $e, $time)
	{
		$this->status = PHPEdit_ResultPrinter::STATUS_Error;
		$this->message .= htmlspecialchars($e->__toString()) . "\n";
	}

	public function addFailure(PHPUnit_Framework_Test $test, PHPUnit_Framework_AssertionFailedError $e,	$time)
	{
		$this->status = PHPEdit_ResultPrinter::STATUS_FAILED;
		$this->message .= htmlspecialchars($e->toString()) . "\n";
	}

	public function addIncompleteTest(PHPUnit_Framework_Test $test, Exception $e, $time)
	{
		$this->status = PHPEdit_ResultPrinter::STATUS_INCOMPLETE;
		$this->message .= htmlspecialchars($e->toString()) . "\n";
	}

	public function addSkippedTest(PHPUnit_Framework_Test $test, Exception $e, $time)
	{
		$this->status = PHPEdit_ResultPrinter::STATUS_SKIPPED;
		$this->message .= htmlspecialchars($e->toString()) . "\n";
	}

	public function startTest(PHPUnit_Framework_Test $test)
	{
		$this->status = PHPEdit_ResultPrinter::STATUS_UNKNOWN;
		$this->message = '';
	}

	public function endTest(PHPUnit_Framework_Test $test, $time)
	{
		$testName = htmlspecialchars($test->getName());
		$class = new ReflectionClass($test);
		$className = htmlspecialchars($class->getName());
		$filename = htmlspecialchars($class->getFileName());
		$time = htmlspecialchars($time);

		echo utf8_encode("\n<Test name='$testName' class='$className' filename='$filename'");
		if ($time != '')
		{
			if ($this->status == PHPEdit_ResultPrinter::STATUS_UNKNOWN)
			{
				$this->status = PHPEdit_ResultPrinter::STATUS_SUCCEEDED;
			}
			echo " time='$time'";
		}
		echo " status='{$this->status}'>";
		echo utf8_encode($this->message);
		echo "</Test>\n";
	}

	public function startTestSuite(PHPUnit_Framework_TestSuite $suite)
	{
	}

	public function endTestSuite(PHPUnit_Framework_TestSuite $suite)
	{
	}

	public function printResult(PHPUnit_Framework_TestResult $result)
  {
	}
}

?>
