From 8ff245104b61af10ad35947aac283cb119b63d44 Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Sat, 28 Jul 2007 17:37:32 +0000 Subject: [PATCH] Starting to migrate from SimpleTest to PHPUnit. * docs/UnitTestReport.php: Renamed to SimpleTestReporter * docs/UnitTest/AllTests.php: Changing the path to the test reporter, see above. * docs/PHPUnitTestReporter.php: Added --- docs/PHPUnitTestReporter.php | 546 ++++++++++++++++++ ...tTestReport.php => SimpleTestReporter.php} | 0 docs/UnitTest/AllTests.php | 2 +- 3 files changed, 547 insertions(+), 1 deletion(-) create mode 100644 docs/PHPUnitTestReporter.php rename docs/{UnitTestReport.php => SimpleTestReporter.php} (100%) diff --git a/docs/PHPUnitTestReporter.php b/docs/PHPUnitTestReporter.php new file mode 100644 index 0000000..b713fd5 --- /dev/null +++ b/docs/PHPUnitTestReporter.php @@ -0,0 +1,546 @@ +$result = new PHPUnit2_Framework_TestResult; +* $result->addListener(new ISI_PHPUnit2_Output_HTML_Listener); +* $suite->run($result); +* +* @author Iris Studios, Inc. +* @copyright Copyright ©2002 - [#]year[#], Iris Studios, Inc. +* @version $Revision$ +* +*/ +class ISI_PHPUnit2_Output_HTML_Listener implements PHPUnit2_Framework_TestListener +{ + /** + * A successful test + * @var integer + */ + const TEST_SUCCESS = 1; + + /** + * An incomplete test + * @var integer + */ + const TEST_INCOMPLETE = 2; + + /** + * A failed test + * @var integer + */ + const TEST_FAIL = 3; + + /** + * An array of all the tests run in a given suite + * @var array + */ + protected $tests = array(); + + /** + * An array of arrays of all the errors in a given failed test in a suite + * @var array + */ + protected $errors = array(); + + /** + * An array of all the incomplete test messages for a test in a suite + * @var array + */ + protected $incompletes = array(); + + /** + * Statistics for a given test suite + * @var array + */ + protected $stats = array(); + + /** + * The name of the currently-running test suite + * @var string + */ + private $currentSuite; + + /** + * The name of the currently-running individual test + * @var string + */ + private $currentTest; + + // ################################################################### + /** + * A test errored out + * + * @param object PHPUnit2_Framework_Test + * @param except Thrown execption + */ + public function addError(PHPUnit2_Framework_Test $test, Exception $e) + { + $this->errors[ $this->currentSuite ][ $this->currentTest ][] = new ISI_Private_Exception($e); + } + + // ################################################################### + /** + * A test failed + * + * @param object PHPUnit2_Framework_Test + * @param object PHPUnit2_Framework_AssertionFailedError + */ + public function addFailure(PHPUnit2_Framework_Test $test, PHPUnit2_Framework_AssertionFailedError $e) + { + $this->errors[ $this->currentSuite ][ $this->currentTest ][] = new ISI_Private_Exception($e); + $this->tests[ $this->currentSuite ][ $this->currentTest ] = self::TEST_FAIL; + $this->stats[ $this->currentSuite ]['fail']++; + } + + // ################################################################### + /** + * A test reported being an incomplete implementation + * + * @param object PHPUnit2_Framework_Test + * @param except The thrown exception notice + */ + public function addIncompleteTest(PHPUnit2_Framework_Test $test, Exception $e) + { + $this->incompletes[ $this->currentSuite ][ $this->currentTest ] = new ISI_Private_Exception($e); + $this->tests[ $this->currentSuite ][ $this->currentTest ] = self::TEST_INCOMPLETE; + $this->stats[ $this->currentSuite ]['incomplete']++; + } + + // ################################################################### + /** + * Start test suite + * + * @param object A PHPUnit2_Framework_TestSuite + */ + public function startTestSuite(PHPUnit2_Framework_TestSuite $suite) + { + $this->currentSuite = $suite->getName(); + if ($this->currentSuite == '') + { + return; + } + + $this->tests[ $this->currentSuite ] = array(); + $this->stats[ $this->currentSuite ] = array('total' => 0, 'fail' => 0, 'incomplete' => 0); + } + + // ################################################################### + /** + * Test suite ended + * + * @param object PHPUnit2_Framework_TestSuite + */ + public function endTestSuite(PHPUnit2_Framework_TestSuite $suite) + { + $this->currentSuite = null; + } + + // ################################################################### + /** + * An individual test started + * + * @param object The PHPUnit2_Framework_Test + */ + public function startTest(PHPUnit2_Framework_Test $test) + { + $this->currentTest = $test->getName(); + if ($this->currentTest == '') + { + return; + } + + $this->tests[ $this->currentSuite ][ $this->currentTest ] = self::TEST_SUCCESS; + $this->stats[ $this->currentSuite ]['total']++; + } + + // ################################################################### + /** + * An individual test ended + * + * @param object The PHPUnit2_Framework_Test + */ + public function endTest(PHPUnit2_Framework_Test $test) + { + $this->currentTest = null; + } + + // ################################################################### + /** + * Destructor: create the HTML output + */ + public function __destruct() + { + $stats = array('total' => 0, 'incomplete' => 0, 'fail' => 0, 'success' => 0); + foreach ($this->stats AS $statGroup) + { + foreach ($statGroup AS $statType => $count) + { + $stats["$statType"] += $count; + } + } + $stats['success'] = $stats['total'] - ($stats['incomplete'] + $stats['fail']); + + $output = ' + + + + Unit Test Results + + + + + +

Unit Test Results

+ +
+
The following is a report of how each unit test performed. If any errors or exceptions were thrown, they have been recorded below. Tests have been broken down into their test suite groups. Your overall statistics are here:
+ +
+ +
Total Tests: ' . $stats['total'] . '
+
Number Success: ' . $stats['success'] . '
+
Number Fail: ' . $stats['fail'] . '
+
Number Incomplete: ' . $stats['incomplete'] . '
+ +
+ +
Total Success Rate: ' . round(100 * ($stats['success'] / $stats['total']), 1) . '%
+
+ +
+ +'; + + foreach ($this->tests AS $suite => $tests) + { + $successPercent = round(100 * (($this->stats["$suite"]['total'] - ($this->stats["$suite"]['fail'] + $this->stats["$suite"]['incomplete'])) / $this->stats["$suite"]['total']), 1); + + $output .= '
'; + $output .= "\n\t" . '
'; + $output .= "\n\t\t" . '' . $this->stats["$suite"]['total'] . ' Tests, ' . $successPercent . '% Success' . ''; + $output .= "\n\t\t" . $suite; + $output .= "\n\t" . '
'; + $output .= "\n\t" . '
'; + foreach ($tests AS $name => $statusCode) + { + switch ($statusCode) + { + case self::TEST_SUCCESS: + $cssName = 'Success'; + $cssColor = 'green'; + + $info = ''; + break; + + case self::TEST_INCOMPLETE: + $cssName = 'Incomplete'; + $cssColor = 'blue'; + + $info = $this->formatInfo($this->incompletes["$suite"]["$name"]); + break; + + case self::TEST_FAIL: + $cssName = 'Fail'; + $cssColor = 'red'; + + $info = $this->formatInfo($this->errors["$suite"]["$name"]); + break; + } + + $output .= "\n\t\t" . '
'; + $output .= "\n\t\t\t" . '
' . $name . '
'; + + if ($info) + { + $output .= "\n\t\t\t" . '
'; + $output .= $info; + $output .= "\n\t\t\t" . '
'; + } + + $output .= "\n\t\t" . '
'; + } + $output .= "\n\t" . '
'; + $output .= "\n" . '
'; + $output .= "\n\n" . '
' . "\n\n"; + } + + $output .= ' + +'; + + file_put_contents('./UnitTestReport.html', $output); + } + + // ################################################################### + /** + * Formats an error array into a bulleted list + * + * @param mixed Error/information list + * + * @return string Formatted HTML + */ + private function formatInfo($list) + { + if (!is_array($list)) + { + $list = array($list); + } + + $count = sizeof($list); + foreach ($list AS $exc) + { + $i++; + + if ($exc->getMessage() != '') + { + $output = "\n\t\t\t\t" . '
'; + $output .= "\n\t\t\t\t\t" . '
• ' . htmlspecialchars($exc->getMessage()) . '
'; + } + + $output .= "\n\t\t\t\t\t" . '
in ' . $exc->getPath() . ' at line ' . $exc->getLine() . '
'; + + if ($exc->getMessage() != '') + { + $output .= "\n\t\t\t\t" . '
'; + } + } + + return $output; + } +} + +/** +* Iris Studios Private Exception Handler +* +* This class is just a form of an exception that ignores the stack trace +* because it's too long. +* +* @author Iris Studios, Inc. +* @copyright Copyright ©2002 - [#]year[#], Iris Studios, Inc. +* @version $Revision$ +* +*/ +class ISI_Private_Exception +{ + /** + * Exception context message + * @var string + */ + private $message = ''; + + /** + * File path + * @var string + */ + private $path = ''; + + /** + * Line number of the exception + * @var integer + */ + private $line = 0; + + // ################################################################### + /** + * Constructor: accept exception + * + * @param except A given exception + */ + public function __construct(Exception $e) + { + $this->message = $e->getMessage(); + $this->path = $e->getFile(); + $this->line = $e->getLine(); + } + + // ################################################################### + /** + * Returns the context message + * + * @return string Context message + */ + public function getMessage() + { + return $this->message; + } + + // ################################################################### + /** + * Returns the file path + * + * @return string File path + */ + public function getPath() + { + return $this->path; + } + + // ################################################################### + /** + * Returns the line number + * + * @return integer Line number + */ + public function getLine() + { + return $this->line; + } +} + +/*=====================================================================*\ +|| ################################################################### +|| # $HeadURL$ +|| # $Id$ +|| ################################################################### +\*=====================================================================*/ +?> \ No newline at end of file diff --git a/docs/UnitTestReport.php b/docs/SimpleTestReporter.php similarity index 100% rename from docs/UnitTestReport.php rename to docs/SimpleTestReporter.php diff --git a/docs/UnitTest/AllTests.php b/docs/UnitTest/AllTests.php index 66d7a82..22e5764 100644 --- a/docs/UnitTest/AllTests.php +++ b/docs/UnitTest/AllTests.php @@ -4,7 +4,7 @@ set_include_path(get_include_path() . ':/Server/htdocs'); // load the toolkit require_once('simpletest/unit_tester.php'); -require_once('../UnitTestReport.php'); +require_once('../SimpleTestReporter.php'); $test = new GroupTest('All Tests'); -- 2.43.5