Browse files

Import PHPUnit 1.x releases.

  • Loading branch information...
0 parents commit bc4c4775f43faac93a74599b6bf93f29185e1527 @sebastianbergmann committed Jun 29, 2006
83 PHPUnit.php
@@ -0,0 +1,83 @@
+<?php
+//
+// +------------------------------------------------------------------------+
+// | PEAR :: PHPUnit |
+// +------------------------------------------------------------------------+
+// | Copyright (c) 2002-2003 Sebastian Bergmann <sb@sebastian-bergmann.de>. |
+// +------------------------------------------------------------------------+
+// | This source file is subject to version 3.00 of the PHP License, |
+// | that is available at http://www.php.net/license/3_0.txt. |
+// | If you did not receive a copy of the PHP license and are unable to |
+// | obtain it through the world-wide-web, please send a note to |
+// | license@php.net so we can mail you a copy immediately. |
+// +------------------------------------------------------------------------+
+//
+// $Id$
+//
+
+require_once 'PHPUnit/TestCase.php';
+require_once 'PHPUnit/TestResult.php';
+require_once 'PHPUnit/TestSuite.php';
+
+/**
+ * PHPUnit runs a TestSuite and returns a TestResult object.
+ *
+ * Here is an example:
+ *
+ * <code>
+ * <?php
+ * require_once 'PHPUnit.php';
+ *
+ * class MathTest extends PHPUnit_TestCase {
+ * var $fValue1;
+ * var $fValue2;
+ *
+ * function MathTest($name) {
+ * $this->PHPUnit_TestCase($name);
+ * }
+ *
+ * function setUp() {
+ * $this->fValue1 = 2;
+ * $this->fValue2 = 3;
+ * }
+ *
+ * function testAdd() {
+ * $this->assertTrue($this->fValue1 + $this->fValue2 == 5);
+ * }
+ * }
+ *
+ * $suite = new PHPUnit_TestSuite();
+ * $suite->addTest(new MathTest('testAdd'));
+ *
+ * $result = PHPUnit::run($suite);
+ * print $result->toHTML();
+ * ?>
+ * </code>
+ *
+ * Alternatively, you can pass a class name to the PHPUnit_TestSuite()
+ * constructor and let it automatically add all methods of that class
+ * that start with 'test' to the suite:
+ *
+ * <code>
+ * <?php
+ * $suite = new PHPUnit_TestSuite('MathTest');
+ * $result = PHPUnit::run($suite);
+ * print $result->toHTML();
+ * ?>
+ * </code>
+ *
+ * @author Sebastian Bergmann <sb@sebastian-bergmann.de>
+ * @copyright Copyright &copy; 2002-2004 Sebastian Bergmann <sb@sebastian-bergmann.de>
+ * @license http://www.php.net/license/3_0.txt The PHP License, Version 3.0
+ * @category PHP
+ * @package PHPUnit
+ */
+class PHPUnit {
+ function &run(&$suite) {
+ $result = new PHPUnit_TestResult();
+ $suite->run($result);
+
+ return $result;
+ }
+}
+?>
297 PHPUnit/Assert.php
@@ -0,0 +1,297 @@
+<?php
+//
+// +------------------------------------------------------------------------+
+// | PEAR :: PHPUnit |
+// +------------------------------------------------------------------------+
+// | Copyright (c) 2002-2003 Sebastian Bergmann <sb@sebastian-bergmann.de>. |
+// +------------------------------------------------------------------------+
+// | This source file is subject to version 3.00 of the PHP License, |
+// | that is available at http://www.php.net/license/3_0.txt. |
+// | If you did not receive a copy of the PHP license and are unable to |
+// | obtain it through the world-wide-web, please send a note to |
+// | license@php.net so we can mail you a copy immediately. |
+// +------------------------------------------------------------------------+
+//
+// $Id$
+//
+
+/**
+ * A set of assert methods.
+ *
+ * @author Sebastian Bergmann <sb@sebastian-bergmann.de>
+ * @copyright Copyright &copy; 2002-2004 Sebastian Bergmann <sb@sebastian-bergmann.de>
+ * @license http://www.php.net/license/3_0.txt The PHP License, Version 3.0
+ * @category PHP
+ * @package PHPUnit
+ */
+class PHPUnit_Assert {
+ /**
+ * @var boolean
+ * @access private
+ */
+ var $_looselyTyped = false;
+
+ /**
+ * Asserts that two variables are equal.
+ *
+ * @param mixed
+ * @param mixed
+ * @param string
+ * @param mixed
+ * @access public
+ */
+ function assertEquals($expected, $actual, $message = '', $delta = 0) {
+ if ((is_array($actual) && is_array($expected)) ||
+ (is_object($actual) && is_object($expected))) {
+ if (is_array($actual) && is_array($expected)) {
+ ksort($actual);
+ ksort($expected);
+ }
+
+ if ($this->_looselyTyped) {
+ $actual = $this->_convertToString($actual);
+ $expected = $this->_convertToString($expected);
+ }
+
+ $actual = serialize($actual);
+ $expected = serialize($expected);
+
+ $message = sprintf(
+ '%sexpected %s, actual %s',
+
+ !empty($message) ? $message . ' ' : '',
+ $expected,
+ $actual
+ );
+
+ if ($actual !== $expected) {
+ return $this->fail($message);
+ }
+ }
+
+ elseif (is_numeric($actual) && is_numeric($expected)) {
+ $message = sprintf(
+ '%sexpected %s%s, actual %s',
+
+ !empty($message) ? $message . ' ' : '',
+ $expected,
+ ($delta != 0) ? ('+/- ' . $delta) : '',
+ $actual
+ );
+
+ if (!($actual >= ($expected - $delta) && $actual <= ($expected + $delta))) {
+ return $this->fail($message);
+ }
+ }
+
+ else {
+ $message = sprintf(
+ '%sexpected %s, actual %s',
+
+ !empty($message) ? $message . ' ' : '',
+ $expected,
+ $actual
+ );
+
+ if ($actual !== $expected) {
+ return $this->fail($message);
+ }
+ }
+ }
+
+ /**
+ * Asserts that an object isn't null.
+ *
+ * @param object
+ * @param string
+ * @access public
+ */
+ function assertNotNull($object, $message = '') {
+ $message = sprintf(
+ '%sexpected NOT NULL, actual NULL',
+
+ !empty($message) ? $message . ' ' : ''
+ );
+
+ if ($object === null) {
+ return $this->fail($message);
+ }
+ }
+
+ /**
+ * Asserts that an object is null.
+ *
+ * @param object
+ * @param string
+ * @access public
+ */
+ function assertNull($object, $message = '') {
+ $message = sprintf(
+ '%sexpected NULL, actual NOT NULL',
+
+ !empty($message) ? $message . ' ' : ''
+ );
+
+ if ($object !== null) {
+ return $this->fail($message);
+ }
+ }
+
+ /**
+ * Asserts that two objects refer to the same object.
+ * This requires the Zend Engine 2 (to work properly).
+ *
+ * @param object
+ * @param object
+ * @param string
+ * @access public
+ */
+ function assertSame($expected, $actual, $message = '') {
+ $message = sprintf(
+ '%sexpected two variables to refer to the same object',
+
+ !empty($message) ? $message . ' ' : ''
+ );
+
+ if ($actual !== $expected) {
+ return $this->fail($message);
+ }
+ }
+
+ /**
+ * Asserts that two objects refer not to the same object.
+ * This requires the Zend Engine 2 (to work properly).
+ *
+ * @param object
+ * @param object
+ * @param string
+ * @access public
+ */
+ function assertNotSame($expected, $actual, $message = '') {
+ $message = sprintf(
+ '%sexpected two variables to refer to different objects',
+
+ !empty($message) ? $message . ' ' : ''
+ );
+
+ if ($actual === $expected) {
+ return $this->fail($message);
+ }
+ }
+
+ /**
+ * Asserts that a condition is true.
+ *
+ * @param boolean
+ * @param string
+ * @access public
+ */
+ function assertTrue($condition, $message = '') {
+ $message = sprintf(
+ '%sexpected true, actual false',
+
+ !empty($message) ? $message . ' ' : ''
+ );
+
+ if (!$condition) {
+ return $this->fail($message);
+ }
+ }
+
+ /**
+ * Asserts that a condition is false.
+ *
+ * @param boolean
+ * @param string
+ * @access public
+ */
+ function assertFalse($condition, $message = '') {
+ $message = sprintf(
+ '%sexpected false, actual true',
+
+ !empty($message) ? $message . ' ' : ''
+ );
+
+ if ($condition) {
+ return $this->fail($message);
+ }
+ }
+
+ /**
+ * Asserts that a string matches a given
+ * regular expression.
+ *
+ * @param string
+ * @param string
+ * @param string
+ * @access public
+ * @author Sébastien Hordeaux <marms@marms.com>
+ */
+ function assertRegExp($expected, $actual, $message = '') {
+ $message = sprintf(
+ '%sexpected %s, actual %s',
+
+ !empty($message) ? $message . ' ' : '',
+ $expected,
+ $actual
+ );
+
+ if (!preg_match($expected, $actual)) {
+ return $this->fail($message);
+ }
+ }
+
+ /**
+ * Asserts that a variable is of a given type.
+ *
+ * @param string $expected
+ * @param mixed $actual
+ * @param optional string $message
+ * @access public
+ */
+ function assertType($expected, $actual, $message = '') {
+ return $this->assertEquals(
+ $expected,
+ gettype($actual),
+ $message
+ );
+ }
+
+ /**
+ * Converts a value to a string.
+ *
+ * @param mixed $value
+ * @access private
+ */
+ function _convertToString($value) {
+ foreach ($value as $k => $v) {
+ if (is_array($v)) {
+ $value[$k] = $this->_convertToString($value[$k]);
+ } else {
+ settype($value[$k], 'string');
+ }
+ }
+
+ return $value;
+ }
+
+ /**
+ * @param boolean $looselyTyped
+ * @access public
+ */
+ function setLooselyTyped($looselyTyped) {
+ if (is_bool($looselyTyped)) {
+ $this->_looselyTyped = $looselyTyped;
+ }
+ }
+
+ /**
+ * Fails a test with the given message.
+ *
+ * @param string
+ * @access protected
+ * @abstract
+ */
+ function fail($message = '') { /* abstract */ }
+}
+?>
205 PHPUnit/GUI/HTML.php
@@ -0,0 +1,205 @@
+<?php
+//
+// +------------------------------------------------------------------------+
+// | PEAR :: PHPUnit |
+// +------------------------------------------------------------------------+
+// | Copyright (c) 2002-2003 Sebastian Bergmann <sb@sebastian-bergmann.de>. |
+// +------------------------------------------------------------------------+
+// | This source file is subject to version 3.00 of the PHP License, |
+// | that is available at http://www.php.net/license/3_0.txt. |
+// | If you did not receive a copy of the PHP license and are unable to |
+// | obtain it through the world-wide-web, please send a note to |
+// | license@php.net so we can mail you a copy immediately. |
+// +------------------------------------------------------------------------+
+//
+// $Id$
+//
+
+/**
+ * This class provides a HTML GUI.
+ *
+ * @author Wolfram Kriesing <wolfram@kriesing.de>
+ * @license http://www.php.net/license/3_0.txt The PHP License, Version 3.0
+ * @category PHP
+ * @package PHPUnit
+ * @subpackage GUI
+ */
+class PHPUnit_GUI_HTML
+{
+
+ var $_suites = array();
+
+ /**
+ * the current implementation of PHPUnit is designed
+ * this way that adding a suite to another suite only
+ * grabs all the tests and adds them to the suite, so you
+ * have no chance to find out which test goes with which suite
+ * therefore you can simply pass an array of suites to this constructor here
+ *
+ * @param array The suites to be tested. If not given, then you might
+ * be using the SetupDecorator, which detects them automatically
+ * when calling getSuitesFromDir()
+ *
+ */
+ function PHPUnit_GUI_HTML($suites=array())
+ {
+ if (!is_array($suites)) {
+ $this->_suites = array($suites);
+ } else {
+ $this->_suites = $suites;
+ }
+ }
+
+ /**
+ * Add suites to the GUI
+ *
+ * @param object this should be an instance of PHPUnit_TestSuite
+ */
+ function addSuites($suites)
+ {
+ $this->_suites = array_merge($this->_suites,$suites);
+ }
+
+ /**
+ * this prints the HTML code straight out
+ *
+ */
+ function show()
+ {
+ $showPassed=false;
+ $submitted = @$_REQUEST['submitted'];
+ if ($submitted) {
+ $showPassed = @$_REQUEST['showOK'] ? true : false;
+ }
+
+ $suiteResults = array();
+ foreach ($this->_suites as $aSuite) {
+ $aSuiteResult = array();
+ // remove the first directory's name from the test-suite name, since it
+ // mostly is something like 'tests' or alike
+ $removablePrefix = explode('_',$aSuite->getName());
+ $aSuiteResult['name'] = str_replace($removablePrefix[0].'_', '', $aSuite->getName());
+ if ($submitted && isset($_REQUEST[$aSuiteResult['name']])) {
+ $result = PHPUnit::run($aSuite);
+
+ $aSuiteResult['counts']['run'] = $result->runCount();
+ $aSuiteResult['counts']['error'] = $result->errorCount();
+ $aSuiteResult['counts']['failure'] = $result->failureCount();
+
+ $aSuiteResult['results'] = $this->_prepareResult($result,$showPassed);
+
+ $per = 100/$result->runCount();
+ $failed = ($per*$result->errorCount())+($per*$result->failureCount());
+ $aSuiteResult['percent'] = round(100-$failed,2);
+ } else {
+ $aSuiteResult['addInfo'] = 'NOT EXECUTED';
+ }
+
+ $suiteResults[] = $aSuiteResult;
+ }
+
+ $final['name'] = 'OVERALL RESULT';
+ $final['counts'] = array();
+ $final['percent'] = 0;
+ $numExecutedTests = 0;
+ foreach ($suiteResults as $aSuiteResult) {
+ if (sizeof(@$aSuiteResult['counts'])) {
+ foreach ($aSuiteResult['counts'] as $key=>$aCount) {
+ if (!isset($final['counts'][$key])) {
+ $final['counts'][$key] = 0;
+ }
+ $final['counts'][$key] += $aCount;
+ }
+ }
+ }
+ if (isset($final['counts']['run'])) {
+ $per = 100/$final['counts']['run'];
+ $failed = ($per*$final['counts']['error'])+($per*$final['counts']['failure']);
+ $final['percent'] = round(100-$failed,2);
+ } else {
+ $final['percent'] = 0;
+ }
+ array_unshift($suiteResults,$final);
+
+ include 'PHPUnit/GUI/HTML.tpl';
+ }
+
+ function _prepareResult($result,$showPassed)
+ {
+ $ret = array();
+ $failures = $result->failures();
+ foreach($failures as $aFailure) {
+ $ret['failures'][] = $this->_prepareFailure($aFailure);
+ }
+
+ $errors = $result->errors();
+ foreach($errors as $aError) {
+ $ret['errors'][] = $this->_prepareErrors($aError);
+ }
+
+ if ($showPassed) {
+ $passed = $result->passedTests();
+ foreach($passed as $aPassed) {
+ $ret['passed'][] = $this->_preparePassedTests($aPassed);
+ }
+ }
+
+ return $ret;
+ }
+
+ function _prepareFailure($failure)
+ {
+ $test = $failure->failedTest();
+ $ret['testName'] = $test->getName();
+
+ $exception = $failure->thrownException();
+ // a serialized string starts with a 'character:decimal:{'
+ // if so we try to unserialize it
+ // this piece of the regular expression is for detecting a serialized
+ // type like 'a:3:' for an array with three element or an object i.e. 'O:12:"class":3'
+ $serialized = '(\w:\d+:(?:"[^"]+":\d+:)?\{.*\})';
+ // Spaces might make a diff, so we shall show them properly (since a
+ // user agent ignores them).
+ if (preg_match('/^(.*)expected ' . $serialized . ', actual ' .
+ $serialized . '$/sU', $exception, $matches)) {
+ ob_start();
+ print_r(unserialize($matches[2]));
+ $ret['expected'] = htmlspecialchars($matches[1]) . "<pre>" .
+ htmlspecialchars(rtrim(ob_get_contents())) . "</pre>";
+ // Improved compatibility, ob_clean() would be PHP >= 4.2.0 only.
+ ob_end_clean();
+ ob_start();
+ print_r(unserialize($matches[3]));
+ $ret['actual'] = htmlspecialchars($matches[1]) . "<pre>" .
+ htmlspecialchars(rtrim(ob_get_contents())) . "</pre>";
+ ob_end_clean();
+ } elseif (preg_match('/^(.*)expected (.*), actual (.*)$/sU', $exception,
+ $matches)) {
+ $ret['expected'] = nl2br(str_replace(" ", "&nbsp;",
+ htmlspecialchars($matches[1] . $matches[2])));
+ $ret['actual'] = nl2br(str_replace(" ", "&nbsp;",
+ htmlspecialchars($matches[1] . $matches[3])));
+ } else {
+ $ret['message'] = nl2br(str_replace(" ", "&nbsp;",
+ htmlspecialchars($exception)));
+ }
+
+ return $ret;
+ }
+
+ function _preparePassedTests($passed)
+ {
+ $ret['testName'] = $passed->getName();
+ return $ret;
+ }
+
+ function _prepareError($error)
+ {
+ $ret['testName'] = $error->getName();
+ $ret['message'] = $error->toString();
+ return $ret;
+ }
+
+}
+
+?>
156 PHPUnit/GUI/HTML.tpl
@@ -0,0 +1,156 @@
+<html>
+ <head>
+ <STYLE type="text/css">
+
+ body, td {
+ background-color: lightgrey;
+ }
+
+ table.outline, outlineFailure {
+ background-color: black;
+ border-width: 1px;
+ }
+
+ td {
+ padding: 2px;
+ }
+
+ th {
+ text-align: left;
+ color: white;
+ background-color: black;
+ }
+
+ .success {
+ background-color: lightgreen;
+ }
+
+ .failure {
+ background-color: orange;
+ }
+ .info {
+ padding: 2px;
+ color: orange;
+ }
+
+ </STYLE>
+ </head>
+ <body>
+ <form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post" name="optionsForm">
+ <table align="center" class="outline" width="70%">
+ <tr>
+ <th colspan="10">
+ Options
+ </th>
+ </tr>
+ <tr>
+ <td colspan="10">
+ <input type="checkbox" onClick="unCheckAll()" name="allChecked">
+ (un)check all
+ &nbsp; &nbsp;
+ show OK <input type="checkbox" name="showOK" <?php echo @$_REQUEST['showOK']?'checked':''?>>
+ &nbsp; &nbsp;
+ <input type="submit" name="submitted" value="run tests">
+ </td>
+ </tr>
+
+ <?php foreach($suiteResults as $aResult): ?>
+ <tr>
+ <th colspan="10">
+ <input type="checkbox" name="<?php echo $aResult['name'] ?>" <?php echo @$_REQUEST[$aResult['name']]?'checked':'' ?>>
+ <?php echo $aResult['name'] ?>
+ &nbsp;
+ <?php if (isset($aResult['addInfo'])): ?>
+ <font class="info"><?php echo @$aResult['addInfo'] ?></font>
+ <?php endif ?>
+ </th>
+ </tr>
+
+ <?php if(@$aResult['percent']): ?>
+ <tr>
+ <td colspan="10" nowrap="nowrap">
+ <table style="width:100%; padding:2px;" cellspacing="0" cellspan="0" cellpadding="0">
+ <tr>
+ <td width="<?php echo $aResult['percent'] ?>%" class="success" align="center" style="padding:0;">
+ <?php echo $aResult['percent']?$aResult['percent'].'%':'' ?>
+ </td>
+ <td width="<?php echo 100-$aResult['percent'] ?>%" class="failure" align="center" style="padding:0;">
+ <?php echo (100-$aResult['percent'])?(100-$aResult['percent'].'%'):'' ?>
+ </td>
+ </tr>
+ </table>
+ </td>
+ </tr>
+ <?php endif ?>
+
+ <?php if(@$aResult['counts']): ?>
+ <tr>
+ <td colspan="10">
+ <?php foreach($aResult['counts'] as $aCount=>$value): ?>
+ <?php echo $aCount ?>s = <?php echo $value ?> &nbsp; &nbsp; &nbsp; &nbsp;
+ <?php endforeach ?>
+ </td>
+ </tr>
+ <?php endif ?>
+
+ <?php if(isset($aResult['results']['failures']) && sizeof($aResult['results']['failures']))
+ foreach($aResult['results']['failures'] as $aFailure): ?>
+ <tr>
+ <td class="failure"><?php echo $aFailure['testName'] ?></td>
+ <td class="failure">
+ <?php if(isset($aFailure['message']) && $aFailure['message']): ?>
+ <?php echo $aFailure['message'] ?>
+ <?php else: ?>
+ <table class="outlineFailure">
+ <tr>
+ <td>expected</td>
+ <td><?php echo $aFailure['expected'] ?></td>
+ </tr>
+ <tr>
+ <td>actual</td>
+ <td><?php echo $aFailure['actual'] ?></td>
+ </tr>
+ </table>
+ <?php endif ?>
+ </td>
+ </tr>
+ <?php endforeach ?>
+
+ <?php if(isset($aResult['results']['errors']) && sizeof($aResult['results']['errors']))
+ foreach($aResult['results']['errors'] as $aError): ?>
+ <tr>
+ <td class="failure"><?php echo $aError['testName'] ?></td>
+ <td class="failure">
+ <?php echo $aError['message'] ?>
+ </td>
+ </tr>
+ <?php endforeach ?>
+
+ <?php if(isset($aResult['results']['passed']) && sizeof($aResult['results']['passed']))
+ foreach($aResult['results']['passed'] as $aPassed): ?>
+ <tr>
+ <td class="success"><?php echo $aPassed['testName'] ?></td>
+ <td class="success"><b>OK</b></td>
+ </tr>
+ <?php endforeach ?>
+
+ <?php endforeach ?>
+ </table>
+ </form>
+
+ <script>
+ var allSuiteNames = new Array();
+ <?php foreach($suiteResults as $aResult): ?>
+ allSuiteNames[allSuiteNames.length] = "<?php echo $aResult['name'] ?>";
+ <?php endforeach ?>
+ function unCheckAll()
+ {
+ _checked = document.optionsForm.allChecked.checked;
+ for (i=0;i<allSuiteNames.length;i++) {
+ document.optionsForm[allSuiteNames[i]].checked = _checked;
+ }
+ }
+ </script>
+
+ </body>
+</html>
150 PHPUnit/GUI/SetupDecorator.php
@@ -0,0 +1,150 @@
+<?php
+//
+// +------------------------------------------------------------------------+
+// | PEAR :: PHPUnit |
+// +------------------------------------------------------------------------+
+// | Copyright (c) 2002-2003 Sebastian Bergmann <sb@sebastian-bergmann.de>. |
+// +------------------------------------------------------------------------+
+// | This source file is subject to version 3.00 of the PHP License, |
+// | that is available at http://www.php.net/license/3_0.txt. |
+// | If you did not receive a copy of the PHP license and are unable to |
+// | obtain it through the world-wide-web, please send a note to |
+// | license@php.net so we can mail you a copy immediately. |
+// +------------------------------------------------------------------------+
+//
+// $Id$
+//
+
+/**
+ * This decorator actually just adds the functionality to read the
+ * test-suite classes from a given directory and instanciate them
+ * automatically, use it as given in the example below.
+ *
+ * <code>
+ * <?php
+ * $gui = new PHPUnit_GUI_SetupDecorator(new PHPUnit_GUI_HTML());
+ * $gui->getSuitesFromDir('/path/to/dir/tests','.*\.php$',array('index.php','sql.php'));
+ * $gui->show();
+ * ?>
+ * </code>
+ *
+ * The example calls this class and tells it to:
+ *
+ * - find all file under the directory /path/to/dir/tests
+ * - for files, which end with '.php' (this is a piece of a regexp, that's why the . is escaped)
+ * - and to exclude the files 'index.php' and 'sql.php'
+ * - and include all the files that are left in the tests.
+ *
+ * Given that the path (the first parameter) ends with 'tests' it will be assumed
+ * that the classes are named tests_* where * is the directory plus the filename,
+ * according to PEAR standards.
+ *
+ * So that:
+ *
+ * - 'testMe.php' in the dir 'tests' bill be assumed to contain a class tests_testMe
+ * - '/moretests/aTest.php' should contain a class 'tests_moretests_aTest'
+ *
+ * @author Wolfram Kriesing <wolfram@kriesing.de>
+ * @license http://www.php.net/license/3_0.txt The PHP License, Version 3.0
+ * @category PHP
+ * @package PHPUnit
+ * @subpackage GUI
+ */
+class PHPUnit_GUI_SetupDecorator
+{
+ /**
+ *
+ *
+ */
+ function PHPUnit_GUI_SetupDecorator(&$gui)
+ {
+ $this->_gui = $gui;
+ }
+
+ /**
+ * just forwarding the action to the decorated class.
+ *
+ */
+ function show($showPassed=true)
+ {
+ $this->_gui->show($showPassed);
+ }
+
+ /**
+ * Setup test suites that can be found in the given directory
+ * Using the second parameter you can also choose a subsets of the files found
+ * in the given directory. I.e. only all the files that contain '_UnitTest_',
+ * in order to do this simply call it like this:
+ * <code>getSuitesFromDir($dir,'.*_UnitTest_.*')</code>.
+ * There you can already see that the pattern is built for the use within a regular expression.
+ *
+ * @param string the directory where to search for test-suite files
+ * @param string the pattern (a regexp) by which to find the files
+ * @param array an array of file names that shall be excluded
+ *
+ */
+ function getSuitesFromDir($dir,$filenamePattern='',$exclude=array())
+ {
+ // remove trailing DIRECTORY_SEPERATOR if missing
+ if ($dir{strlen($dir)-1} == DIRECTORY_SEPARATOR) {
+ $dir = substr($dir,0,-1);
+ }
+
+ $files = $this->_getFiles($dir,$filenamePattern,$exclude,realpath($dir.'/..'));
+ asort($files);
+ foreach ($files as $className=>$aFile) {
+ include_once($aFile);
+ if (class_exists($className)) {
+ $suites[] =& new PHPUnit_TestSuite($className);
+ } else {
+ trigger_error("$className could not be found in $dir$aFile!");
+ }
+ }
+
+ $this->_gui->addSuites($suites);
+ }
+
+ /**
+ * This method searches recursively through the directories
+ * to find all the files that shall be added to the be visible.
+ *
+ * @access private
+ * @param string the path where find the files
+ * @param srting the string pattern by which to find the files
+ * @param string the file names to be excluded
+ * @param string the root directory, which serves as the prefix to the fully qualified filename
+ */
+ function _getFiles($dir,$filenamePattern,$exclude,$rootDir)
+ {
+ $files = array();
+ if ($dp=opendir($dir)) {
+ while (false!==($file=readdir($dp))) {
+ $filename = $dir.DIRECTORY_SEPARATOR.$file;
+ $match = true;
+ if ($filenamePattern && !preg_match("~$filenamePattern~",$file)) {
+ $match = false;
+ }
+ if (sizeof($exclude)) {
+ foreach ($exclude as $aExclude) {
+ if (strpos($file,$aExclude)!==false) {
+ $match = false;
+ break;
+ }
+ }
+ }
+ if (is_file($filename) && $match) {
+ $className = str_replace(DIRECTORY_SEPARATOR, '_', substr(str_replace($rootDir, '', $filename), 1));
+ $className = basename($className,'.php'); // remove php-extension
+ $files[$className] = $filename;
+ }
+ if ($file!='.' && $file!='..' && is_dir($filename)) {
+ $files = array_merge($files,$this->_getFiles($filename,$filenamePattern,$exclude,$rootDir));
+ }
+ }
+ closedir($dp);
+ }
+ return $files;
+ }
+}
+
+?>
112 PHPUnit/RepeatedTest.php
@@ -0,0 +1,112 @@
+<?php
+//
+// +------------------------------------------------------------------------+
+// | PEAR :: PHPUnit |
+// +------------------------------------------------------------------------+
+// | Copyright (c) 2002-2003 Sebastian Bergmann <sb@sebastian-bergmann.de>. |
+// +------------------------------------------------------------------------+
+// | This source file is subject to version 3.00 of the PHP License, |
+// | that is available at http://www.php.net/license/3_0.txt. |
+// | If you did not receive a copy of the PHP license and are unable to |
+// | obtain it through the world-wide-web, please send a note to |
+// | license@php.net so we can mail you a copy immediately. |
+// +------------------------------------------------------------------------+
+//
+// $Id$
+//
+
+require_once 'PHPUnit/TestDecorator.php';
+
+/**
+ * A Decorator that runs a test repeatedly.
+ *
+ * Here is an example:
+ *
+ * <code>
+ * <?php
+ * require_once 'PHPUnit.php';
+ * require_once 'PHPUnit/RepeatedTest.php';
+ *
+ * class MathTest extends PHPUnit_TestCase {
+ * var $fValue1;
+ * var $fValue2;
+ *
+ * function MathTest($name) {
+ * $this->PHPUnit_TestCase($name);
+ * }
+ *
+ * function setUp() {
+ * $this->fValue1 = 2;
+ * $this->fValue2 = 3;
+ * }
+ *
+ * function testAdd() {
+ * $this->assertTrue($this->fValue1 + $this->fValue2 == 5);
+ * }
+ * }
+ *
+ * $suite = new PHPUnit_TestSuite;
+ *
+ * $suite->addTest(
+ * new PHPUnit_RepeatedTest(
+ * new MathTest('testAdd'),
+ * 10
+ * )
+ * );
+ *
+ * $result = PHPUnit::run($suite);
+ * print $result->toString();
+ * ?>
+ * </code>
+ *
+ * @author Sebastian Bergmann <sb@sebastian-bergmann.de>
+ * @copyright Copyright &copy; 2002-2004 Sebastian Bergmann <sb@sebastian-bergmann.de>
+ * @license http://www.php.net/license/3_0.txt The PHP License, Version 3.0
+ * @category PHP
+ * @package PHPUnit
+ */
+class PHPUnit_RepeatedTest extends PHPUnit_TestDecorator {
+ /**
+ * @var integer
+ * @access private
+ */
+ var $_timesRepeat = 1;
+
+ /**
+ * Constructor.
+ *
+ * @param object
+ * @param integer
+ * @access public
+ */
+ function PHPUnit_RepeatedTest(&$test, $timesRepeat = 1) {
+ $this->PHPUnit_TestDecorator($test);
+ $this->_timesRepeat = $timesRepeat;
+ }
+
+ /**
+ * Counts the number of test cases that
+ * will be run by this test.
+ *
+ * @return integer
+ * @access public
+ */
+ function countTestCases() {
+ return $this->_timesRepeat * $this->_test->countTestCases();
+ }
+
+ /**
+ * Runs the decorated test and collects the
+ * result in a TestResult.
+ *
+ * @param object
+ * @access public
+ * @abstract
+ */
+ function run(&$result) {
+ for ($i = 0; $i < $this->_timesRepeat; $i++) {
+ $this->_test->run($result);
+ }
+ }
+}
+?>
237 PHPUnit/TestCase.php
@@ -0,0 +1,237 @@
+<?php
+//
+// +------------------------------------------------------------------------+
+// | PEAR :: PHPUnit |
+// +------------------------------------------------------------------------+
+// | Copyright (c) 2002-2003 Sebastian Bergmann <sb@sebastian-bergmann.de>. |
+// +------------------------------------------------------------------------+
+// | This source file is subject to version 3.00 of the PHP License, |
+// | that is available at http://www.php.net/license/3_0.txt. |
+// | If you did not receive a copy of the PHP license and are unable to |
+// | obtain it through the world-wide-web, please send a note to |
+// | license@php.net so we can mail you a copy immediately. |
+// +------------------------------------------------------------------------+
+//
+// $Id$
+//
+
+require_once 'PHPUnit/Assert.php';
+require_once 'PHPUnit/TestResult.php';
+
+/**
+ * A TestCase defines the fixture to run multiple tests.
+ *
+ * To define a TestCase
+ *
+ * 1) Implement a subclass of PHPUnit_TestCase.
+ * 2) Define instance variables that store the state of the fixture.
+ * 3) Initialize the fixture state by overriding setUp().
+ * 4) Clean-up after a test by overriding tearDown().
+ *
+ * Each test runs in its own fixture so there can be no side effects
+ * among test runs.
+ *
+ * Here is an example:
+ *
+ * <code>
+ * <?php
+ * class MathTest extends PHPUnit_TestCase {
+ * var $fValue1;
+ * var $fValue2;
+ *
+ * function MathTest($name) {
+ * $this->PHPUnit_TestCase($name);
+ * }
+ *
+ * function setUp() {
+ * $this->fValue1 = 2;
+ * $this->fValue2 = 3;
+ * }
+ * }
+ * ?>
+ * </code>
+ *
+ * For each test implement a method which interacts with the fixture.
+ * Verify the expected results with assertions specified by calling
+ * assert with a boolean.
+ *
+ * <code>
+ * <?php
+ * function testPass() {
+ * $this->assertTrue($this->fValue1 + $this->fValue2 == 5);
+ * }
+ * ?>
+ * </code>
+ *
+ * @author Sebastian Bergmann <sb@sebastian-bergmann.de>
+ * @copyright Copyright &copy; 2002-2004 Sebastian Bergmann <sb@sebastian-bergmann.de>
+ * @license http://www.php.net/license/3_0.txt The PHP License, Version 3.0
+ * @category PHP
+ * @package PHPUnit
+ */
+class PHPUnit_TestCase extends PHPUnit_Assert {
+ /**
+ * @var boolean
+ * @access private
+ */
+ var $_failed = false;
+
+ /**
+ * The name of the test case.
+ *
+ * @var string
+ * @access private
+ */
+ var $_name = '';
+
+ /**
+ * PHPUnit_TestResult object
+ *
+ * @var object
+ * @access private
+ */
+ var $_result;
+
+ /**
+ * Constructs a test case with the given name.
+ *
+ * @param string
+ * @access public
+ */
+ function PHPUnit_TestCase($name = false) {
+ if ($name != false) {
+ $this->setName($name);
+ }
+ }
+
+ /**
+ * Counts the number of test cases executed by run(TestResult result).
+ *
+ * @return integer
+ * @access public
+ */
+ function countTestCases() {
+ return 1;
+ }
+
+ /**
+ * Gets the name of a TestCase.
+ *
+ * @return string
+ * @access public
+ */
+ function getName() {
+ return $this->_name;
+ }
+
+ /**
+ * Runs the test case and collects the results in a given TestResult object.
+ *
+ * @param object
+ * @return object
+ * @access public
+ */
+ function run(&$result) {
+ $this->_result = &$result;
+ $this->_result->run($this);
+
+ return $this->_result;
+ }
+
+ /**
+ * Runs the bare test sequence.
+ *
+ * @access public
+ */
+ function runBare() {
+ $this->setUp();
+ $this->runTest();
+ $this->tearDown();
+ $this->pass();
+ }
+
+ /**
+ * Override to run the test and assert its state.
+ *
+ * @access protected
+ */
+ function runTest() {
+ call_user_func(
+ array(
+ &$this,
+ $this->_name
+ )
+ );
+ }
+
+ /**
+ * Sets the name of a TestCase.
+ *
+ * @param string
+ * @access public
+ */
+ function setName($name) {
+ $this->_name = $name;
+ }
+
+ /**
+ * Returns a string representation of the test case.
+ *
+ * @return string
+ * @access public
+ */
+ function toString() {
+ return '';
+ }
+
+ /**
+ * Creates a default TestResult object.
+ *
+ * @return object
+ * @access protected
+ */
+ function &createResult() {
+ return new PHPUnit_TestResult;
+ }
+
+ /**
+ * Fails a test with the given message.
+ *
+ * @param string
+ * @access protected
+ */
+ function fail($message = '') {
+ $this->_result->addFailure($this, $message);
+ $this->_failed = true;
+ }
+
+ /**
+ * Passes a test.
+ *
+ * @access protected
+ */
+ function pass() {
+ if (!$this->_failed) {
+ $this->_result->addPassedTest($this);
+ }
+ }
+
+ /**
+ * Sets up the fixture, for example, open a network connection.
+ * This method is called before a test is executed.
+ *
+ * @access protected
+ * @abstract
+ */
+ function setUp() { /* abstract */ }
+
+ /**
+ * Tears down the fixture, for example, close a network connection.
+ * This method is called after a test is executed.
+ *
+ * @access protected
+ * @abstract
+ */
+ function tearDown() { /* abstract */ }
+}
+?>
110 PHPUnit/TestDecorator.php
@@ -0,0 +1,110 @@
+<?php
+//
+// +------------------------------------------------------------------------+
+// | PEAR :: PHPUnit |
+// +------------------------------------------------------------------------+
+// | Copyright (c) 2002-2003 Sebastian Bergmann <sb@sebastian-bergmann.de>. |
+// +------------------------------------------------------------------------+
+// | This source file is subject to version 3.00 of the PHP License, |
+// | that is available at http://www.php.net/license/3_0.txt. |
+// | If you did not receive a copy of the PHP license and are unable to |
+// | obtain it through the world-wide-web, please send a note to |
+// | license@php.net so we can mail you a copy immediately. |
+// +------------------------------------------------------------------------+
+//
+// $Id$
+//
+
+require_once 'PHPUnit/TestCase.php';
+require_once 'PHPUnit/TestSuite.php';
+
+/**
+ * A Decorator for Tests.
+ *
+ * Use TestDecorator as the base class for defining new
+ * test decorators. Test decorator subclasses can be introduced
+ * to add behaviour before or after a test is run.
+ *
+ * @author Sebastian Bergmann <sb@sebastian-bergmann.de>
+ * @copyright Copyright &copy; 2002-2004 Sebastian Bergmann <sb@sebastian-bergmann.de>
+ * @license http://www.php.net/license/3_0.txt The PHP License, Version 3.0
+ * @category PHP
+ * @package PHPUnit
+ */
+class PHPUnit_TestDecorator {
+ /**
+ * The Test to be decorated.
+ *
+ * @var object
+ * @access protected
+ */
+ var $_test = null;
+
+ /**
+ * Constructor.
+ *
+ * @param object
+ * @access public
+ */
+ function PHPUnit_TestDecorator(&$test) {
+ if (is_object($test) &&
+ (is_a($test, 'PHPUnit_TestCase') ||
+ is_a($test, 'PHPUnit_TestSuite'))) {
+
+ $this->_test = $test;
+ }
+ }
+
+ /**
+ * Runs the test and collects the
+ * result in a TestResult.
+ *
+ * @param object
+ * @access public
+ */
+ function basicRun(&$result) {
+ $this->_test->run($result);
+ }
+
+ /**
+ * Counts the number of test cases that
+ * will be run by this test.
+ *
+ * @return integer
+ * @access public
+ */
+ function countTestCases() {
+ return $this->_test->countTestCases();
+ }
+
+ /**
+ * Returns the test to be run.
+ *
+ * @return object
+ * @access public
+ */
+ function &getTest() {
+ return $this->_test;
+ }
+
+ /**
+ * Runs the decorated test and collects the
+ * result in a TestResult.
+ *
+ * @param object
+ * @access public
+ * @abstract
+ */
+ function run(&$result) { /* abstract */ }
+
+ /**
+ * Returns a string representation of the test.
+ *
+ * @return string
+ * @access public
+ */
+ function toString() {
+ return $this->_test->toString();
+ }
+}
+?>
88 PHPUnit/TestFailure.php
@@ -0,0 +1,88 @@
+<?php
+//
+// +------------------------------------------------------------------------+
+// | PEAR :: PHPUnit |
+// +------------------------------------------------------------------------+
+// | Copyright (c) 2002-2003 Sebastian Bergmann <sb@sebastian-bergmann.de>. |
+// +------------------------------------------------------------------------+
+// | This source file is subject to version 3.00 of the PHP License, |
+// | that is available at http://www.php.net/license/3_0.txt. |
+// | If you did not receive a copy of the PHP license and are unable to |
+// | obtain it through the world-wide-web, please send a note to |
+// | license@php.net so we can mail you a copy immediately. |
+// +------------------------------------------------------------------------+
+//
+// $Id$
+//
+
+/**
+ * A TestFailure collects a failed test together with the caught exception.
+ *
+ * @author Sebastian Bergmann <sb@sebastian-bergmann.de>
+ * @copyright Copyright &copy; 2002-2004 Sebastian Bergmann <sb@sebastian-bergmann.de>
+ * @license http://www.php.net/license/3_0.txt The PHP License, Version 3.0
+ * @category PHP
+ * @package PHPUnit
+ */
+class PHPUnit_TestFailure {
+ /**
+ * @var object
+ * @access private
+ */
+ var $_failedTest;
+
+ /**
+ * @var string
+ * @access private
+ */
+ var $_thrownException;
+
+ /**
+ * Constructs a TestFailure with the given test and exception.
+ *
+ * @param object
+ * @param string
+ * @access public
+ */
+ function PHPUnit_TestFailure(&$failedTest, &$thrownException) {
+ $this->_failedTest = $failedTest;
+ $this->_thrownException = $thrownException;
+ }
+
+ /**
+ * Gets the failed test.
+ *
+ * @return object
+ * @access public
+ */
+ function &failedTest() {
+ return $this->_failedTest;
+ }
+
+ /**
+ * Gets the thrown exception.
+ *
+ * @return object
+ * @access public
+ */
+ function &thrownException() {
+ return $this->_thrownException;
+ }
+
+ /**
+ * Returns a short description of the failure.
+ *
+ * @return string
+ * @access public
+ */
+ function toString() {
+ return sprintf(
+ "TestCase %s->%s() failed: %s\n",
+
+ get_class($this->_failedTest),
+ $this->_failedTest->getName(),
+ $this->_thrownException
+ );
+ }
+}
+?>
120 PHPUnit/TestListener.php
@@ -0,0 +1,120 @@
+<?php
+//
+// +------------------------------------------------------------------------+
+// | PEAR :: PHPUnit |
+// +------------------------------------------------------------------------+
+// | Copyright (c) 2002-2003 Sebastian Bergmann <sb@sebastian-bergmann.de>. |
+// +------------------------------------------------------------------------+
+// | This source file is subject to version 3.00 of the PHP License, |
+// | that is available at http://www.php.net/license/3_0.txt. |
+// | If you did not receive a copy of the PHP license and are unable to |
+// | obtain it through the world-wide-web, please send a note to |
+// | license@php.net so we can mail you a copy immediately. |
+// +------------------------------------------------------------------------+
+//
+// $Id$
+//
+
+/**
+ * A Listener for test progress.
+ *
+ * Here is an example:
+ *
+ * <code>
+ * <?php
+ * require_once 'PHPUnit.php';
+ * require_once 'PHPUnit/TestListener.php';
+ *
+ * class MathTest extends PHPUnit_TestCase {
+ * var $fValue1;
+ * var $fValue2;
+ *
+ * function MathTest($name) {
+ * $this->PHPUnit_TestCase($name);
+ * }
+ *
+ * function setUp() {
+ * $this->fValue1 = 2;
+ * $this->fValue2 = 3;
+ * }
+ *
+ * function testAdd() {
+ * $this->assertTrue($this->fValue1 + $this->fValue2 == 4);
+ * }
+ * }
+ *
+ * class MyListener extends PHPUnit_TestListener {
+ * function addError(&$test, &$t) {
+ * print "MyListener::addError() called.\n";
+ * }
+ *
+ * function addFailure(&$test, &$t) {
+ * print "MyListener::addFailure() called.\n";
+ * }
+ *
+ * function endTest(&$test) {
+ * print "MyListener::endTest() called.\n";
+ * }
+ *
+ * function startTest(&$test) {
+ * print "MyListener::startTest() called.\n";
+ * }
+ * }
+ *
+ * $suite = new PHPUnit_TestSuite;
+ * $suite->addTest(new MathTest('testAdd'));
+ *
+ * $result = new PHPUnit_TestResult;
+ * $result->addListener(new MyListener);
+ *
+ * $suite->run($result);
+ * print $result->toString();
+ * ?>
+ * </code>
+ *
+ * @author Sebastian Bergmann <sb@sebastian-bergmann.de>
+ * @copyright Copyright &copy; 2002-2004 Sebastian Bergmann <sb@sebastian-bergmann.de>
+ * @license http://www.php.net/license/3_0.txt The PHP License, Version 3.0
+ * @category PHP
+ * @package PHPUnit
+ */
+class PHPUnit_TestListener {
+ /**
+ * An error occurred.
+ *
+ * @param object
+ * @param object
+ * @access public
+ * @abstract
+ */
+ function addError(&$test, &$t) { /*abstract */ }
+
+ /**
+ * A failure occurred.
+ *
+ * @param object
+ * @param object
+ * @access public
+ * @abstract
+ */
+ function addFailure(&$test, &$t) { /*abstract */ }
+
+ /**
+ * A test ended.
+ *
+ * @param object
+ * @access public
+ * @abstract
+ */
+ function endTest(&$test) { /*abstract */ }
+
+ /**
+ * A test started.
+ *
+ * @param object
+ * @access public
+ * @abstract
+ */
+ function startTest(&$test) { /*abstract */ }
+}
+?>
300 PHPUnit/TestResult.php
@@ -0,0 +1,300 @@
+<?php
+//
+// +------------------------------------------------------------------------+
+// | PEAR :: PHPUnit |
+// +------------------------------------------------------------------------+
+// | Copyright (c) 2002-2003 Sebastian Bergmann <sb@sebastian-bergmann.de>. |
+// +------------------------------------------------------------------------+
+// | This source file is subject to version 3.00 of the PHP License, |
+// | that is available at http://www.php.net/license/3_0.txt. |
+// | If you did not receive a copy of the PHP license and are unable to |
+// | obtain it through the world-wide-web, please send a note to |
+// | license@php.net so we can mail you a copy immediately. |
+// +------------------------------------------------------------------------+
+//
+// $Id$
+//
+
+require_once 'PHPUnit/TestFailure.php';
+require_once 'PHPUnit/TestListener.php';
+
+/**
+ * A TestResult collects the results of executing a test case.
+ *
+ * @author Sebastian Bergmann <sb@sebastian-bergmann.de>
+ * @copyright Copyright &copy; 2002-2004 Sebastian Bergmann <sb@sebastian-bergmann.de>
+ * @license http://www.php.net/license/3_0.txt The PHP License, Version 3.0
+ * @category PHP
+ * @package PHPUnit
+ */
+class PHPUnit_TestResult {
+ /**
+ * @var array
+ * @access protected
+ */
+ var $_errors = array();
+
+ /**
+ * @var array
+ * @access protected
+ */
+ var $_failures = array();
+
+ /**
+ * @var array
+ * @access protected
+ */
+ var $_listeners = array();
+
+ /**
+ * @var array
+ * @access protected
+ */
+ var $_passedTests = array();
+
+ /**
+ * @var integer
+ * @access protected
+ */
+ var $_runTests = 0;
+
+ /**
+ * @var boolean
+ * @access private
+ */
+ var $_stop = false;
+
+ /**
+ * Adds an error to the list of errors.
+ * The passed in exception caused the error.
+ *
+ * @param object
+ * @param object
+ * @access public
+ */
+ function addError(&$test, &$t) {
+ $this->_errors[] = new PHPUnit_TestFailure($test, $t);
+
+ for ($i = 0; $i < sizeof($this->_listeners); $i++) {
+ $this->_listeners[$i]->addError($test, $t);
+ }
+ }
+
+ /**
+ * Adds a failure to the list of failures.
+ * The passed in exception caused the failure.
+ *
+ * @param object
+ * @param object
+ * @access public
+ */
+ function addFailure(&$test, &$t) {
+ $this->_failures[] = new PHPUnit_TestFailure($test, $t);
+
+ for ($i = 0; $i < sizeof($this->_listeners); $i++) {
+ $this->_listeners[$i]->addFailure($test, $t);
+ }
+ }
+
+ /**
+ * Registers a TestListener.
+ *
+ * @param object
+ * @access public
+ */
+ function addListener(&$listener) {
+ if (is_object($listener) &&
+ is_a($listener, 'PHPUnit_TestListener')) {
+ $this->_listeners[] = $listener;
+ }
+ }
+
+ /**
+ * Adds a passed test to the list of passed tests.
+ *
+ * @param object
+ * @access public
+ */
+ function addPassedTest(&$test) {
+ $this->_passedTests[] = $test;
+ }
+
+ /**
+ * Informs the result that a test was completed.
+ *
+ * @param object
+ * @access public
+ */
+ function endTest(&$test) {
+ for ($i = 0; $i < sizeof($this->_listeners); $i++) {
+ $this->_listeners[$i]->endTest($test);
+ }
+ }
+
+ /**
+ * Gets the number of detected errors.
+ *
+ * @return integer
+ * @access public
+ */
+ function errorCount() {
+ return sizeof($this->_errors);
+ }
+
+ /**
+ * Returns an Enumeration for the errors.
+ *
+ * @return array
+ * @access public
+ */
+ function &errors() {
+ return $this->_errors;
+ }
+
+ /**
+ * Gets the number of detected failures.
+ *
+ * @return integer
+ * @access public
+ */
+ function failureCount() {
+ return sizeof($this->_failures);
+ }
+
+ /**
+ * Returns an Enumeration for the failures.
+ *
+ * @return array
+ * @access public
+ */
+ function &failures() {
+ return $this->_failures;
+ }
+
+ /**
+ * Returns an Enumeration for the passed tests.
+ *
+ * @return array
+ * @access public
+ */
+ function &passedTests() {
+ return $this->_passedTests;
+ }
+
+ /**
+ * Unregisters a TestListener.
+ * This requires the Zend Engine 2 (to work properly).
+ *
+ * @param object
+ * @access public
+ */
+ function removeListener(&$listener) {
+ for ($i = 0; $i < sizeof($this->_listeners); $i++) {
+ if ($this->_listeners[$i] === $listener) {
+ unset($this->_listeners[$i]);
+ }
+ }
+ }
+
+ /**
+ * Runs a TestCase.
+ *
+ * @param object
+ * @access public
+ */
+ function run(&$test) {
+ $this->startTest($test);
+ $this->_runTests++;
+ $test->runBare();
+ $this->endTest($test);
+ }
+
+ /**
+ * Gets the number of run tests.
+ *
+ * @return integer
+ * @access public
+ */
+ function runCount() {
+ return $this->_runTests;
+ }
+
+ /**
+ * Checks whether the test run should stop.
+ *
+ * @access public
+ */
+ function shouldStop() {
+ return $this->_stop;
+ }
+
+ /**
+ * Informs the result that a test will be started.
+ *
+ * @param object
+ * @access public
+ */
+ function startTest(&$test) {
+ for ($i = 0; $i < sizeof($this->_listeners); $i++) {
+ $this->_listeners[$i]->startTest($test);
+ }
+ }
+
+ /**
+ * Marks that the test run should stop.
+ *
+ * @access public
+ */
+ function stop() {
+ $this->_stop = true;
+ }
+
+ /**
+ * Returns a HTML representation of the test result.
+ *
+ * @return string
+ * @access public
+ */
+ function toHTML() {
+ return '<pre>' . htmlspecialchars($this->toString()) . '</pre>';
+ }
+
+ /**
+ * Returns a text representation of the test result.
+ *
+ * @return string
+ * @access public
+ */
+ function toString() {
+ $result = '';
+
+ foreach ($this->_passedTests as $passedTest) {
+ $result .= sprintf(
+ "TestCase %s->%s() passed\n",
+
+ get_class($passedTest),
+ $passedTest->getName()
+ );
+ }
+
+ foreach ($this->_failures as $failedTest) {
+ $result .= $failedTest->toString();
+ }
+
+ return $result;
+ }
+ /**
+ * Returns whether the entire test was successful or not.
+ *
+ * @return boolean
+ * @access public
+ */
+ function wasSuccessful() {
+ if (empty($this->_errors) && empty($this->_failures)) {
+ return true;
+ } else {
+ return false;
+ }
+ }
+}
+?>
220 PHPUnit/TestSuite.php
@@ -0,0 +1,220 @@
+<?php
+//
+// +------------------------------------------------------------------------+
+// | PEAR :: PHPUnit |
+// +------------------------------------------------------------------------+
+// | Copyright (c) 2002-2003 Sebastian Bergmann <sb@sebastian-bergmann.de>. |
+// +------------------------------------------------------------------------+
+// | This source file is subject to version 3.00 of the PHP License, |
+// | that is available at http://www.php.net/license/3_0.txt. |
+// | If you did not receive a copy of the PHP license and are unable to |
+// | obtain it through the world-wide-web, please send a note to |
+// | license@php.net so we can mail you a copy immediately. |
+// +------------------------------------------------------------------------+
+//
+// $Id$
+//
+
+require_once 'PHPUnit/TestCase.php';
+
+/**
+ * A TestSuite is a Composite of Tests. It runs a collection of test cases.
+ *
+ * Here is an example using the dynamic test definition.
+ *
+ * <code>
+ * <?php
+ * $suite = new PHPUnit_TestSuite();
+ * $suite->addTest(new MathTest('testPass'));
+ * ?>
+ * </code>
+ *
+ * Alternatively, a TestSuite can extract the tests to be run automatically.
+ * To do so you pass the classname of your TestCase class to the TestSuite
+ * constructor.
+ *
+ * <code>
+ * <?php
+ * $suite = new TestSuite('classname');
+ * ?>
+ * </code>
+ *
+ * This constructor creates a suite with all the methods starting with
+ * "test" that take no arguments.
+ *
+ * @author Sebastian Bergmann <sb@sebastian-bergmann.de>
+ * @copyright Copyright &copy; 2002-2004 Sebastian Bergmann <sb@sebastian-bergmann.de>
+ * @license http://www.php.net/license/3_0.txt The PHP License, Version 3.0
+ * @category PHP
+ * @package PHPUnit
+ */
+class PHPUnit_TestSuite {
+ /**
+ * The name of the test suite.
+ *
+ * @var string
+ * @access private
+ */
+ var $_name = '';
+
+ /**
+ * The tests in the test suite.
+ *
+ * @var array
+ * @access private
+ */
+ var $_tests = array();
+
+ /**
+ * Constructs a TestSuite.
+ *
+ * @param mixed
+ * @access public
+ */
+ function PHPUnit_TestSuite($test = false) {
+ if ($test != false) {
+ $this->setName($test);
+ $this->addTestSuite($test);
+ }
+ }
+
+ /**
+ * Adds a test to the suite.
+ *
+ * @param object
+ * @access public
+ */
+ function addTest(&$test) {
+ $this->_tests[] = $test;
+ }
+
+ /**
+ * Adds the tests from the given class to the suite.
+ *
+ * @param string
+ * @access public
+ */
+ function addTestSuite($testClass) {
+ if (class_exists($testClass)) {
+ $methods = get_class_methods($testClass);
+ $parentClasses = array(strtolower($testClass));
+ $parentClass = $testClass;
+
+ while(is_string($parentClass = get_parent_class($parentClass))) {
+ $parentClasses[] = $parentClass;
+ }
+
+ foreach ($methods as $method) {
+ if (substr($method, 0, 4) == 'test' &&
+ !in_array($method, $parentClasses)) {
+ $this->addTest(new $testClass($method));
+ }
+ }
+ }
+ }
+
+ /**
+ * Counts the number of test cases that will be run by this test.
+ *
+ * @return integer
+ * @access public
+ */
+ function countTestCases() {
+ $count = 0;
+
+ foreach ($this->_tests as $test) {
+ $count += $test->countTestCases();
+ }
+
+ return $count;
+ }
+
+ /**
+ * Returns the name of the suite.
+ *
+ * @return string
+ * @access public
+ */
+ function getName() {
+ return $this->_name;
+ }
+
+ /**
+ * Runs the tests and collects their result in a TestResult.
+ *
+ * @param object
+ * @access public
+ */
+ function run(&$result) {
+ for ($i = 0; $i < sizeof($this->_tests) && !$result->shouldStop(); $i++) {
+ $this->_tests[$i]->run($result);
+ }
+ }
+
+ /**
+ * Runs a test.
+ *
+ * @param object
+ * @param object
+ * @access public
+ */
+ function runTest(&$test, &$result) {
+ $test->run($result);
+ }
+
+ /**
+ * Sets the name of the suite.
+ *
+ * @param string
+ * @access public
+ */
+ function setName($name) {
+ $this->_name = $name;
+ }
+
+ /**
+ * Returns the test at the given index.
+ *
+ * @param integer
+ * @return object
+ * @access public
+ */
+ function &testAt($index) {
+ if (isset($this->_tests[$index])) {
+ return $this->_tests[$index];
+ } else {
+ return false;
+ }
+ }
+
+ /**
+ * Returns the number of tests in this suite.
+ *
+ * @return integer
+ * @access public
+ */
+ function testCount() {
+ return sizeof($this->_tests);
+ }
+
+ /**
+ * Returns the tests as an enumeration.
+ *
+ * @return array
+ * @access public
+ */
+ function &tests() {
+ return $this->_tests;
+ }
+
+ /**
+ * Returns a string representation of the test suite.
+ *
+ * @return string
+ * @access public
+ */
+ function toString() {
+ return '';
+ }
+}
+?>
175 package.xml
@@ -0,0 +1,175 @@
+<?xml version="1.0" encoding="ISO-8859-1" ?>
+<!DOCTYPE package SYSTEM "http://pear.php.net/dtd/package-1.0">
+<package version="1.0">
+ <name>PHPUnit</name>
+ <summary>Regression testing framework for unit tests.</summary>
+ <description>PHPUnit is a regression testing framework used by the developer
+who implements unit tests in PHP. It is based upon JUnit, which
+can be found at http://www.junit.org/.</description>
+ <maintainers>
+ <maintainer>
+ <user>sebastian</user>
+ <name>Sebastian Bergmann</name>
+ <email>sb@sebastian-bergmann.de</email>
+ <role>lead</role>
+ </maintainer>
+ </maintainers>
+ <release>
+ <version>1.0.2</version>
+ <date>2004-09-21</date>
+ <license>PHP License</license>
+ <state>stable</state>
+ <notes>* Added/fixed phpDocumentor code documentation.</notes>
+ <filelist>
+ <file role="php" baseinstalldir="/" name="PHPUnit.php"/>
+ <file role="php" name="PHPUnit\GUI\HTML.php"/>
+ <file role="php" name="PHPUnit\GUI\HTML.tpl"/>
+ <file role="php" name="PHPUnit\GUI\SetupDecorator.php"/>
+ <file role="php" name="PHPUnit\Assert.php"/>
+ <file role="php" name="PHPUnit\RepeatedTest.php"/>
+ <file role="php" name="PHPUnit\TestCase.php"/>
+ <file role="php" name="PHPUnit\TestDecorator.php"/>
+ <file role="php" name="PHPUnit\TestFailure.php"/>
+ <file role="php" name="PHPUnit\TestListener.php"/>
+ <file role="php" name="PHPUnit\TestResult.php"/>
+ <file role="php" name="PHPUnit\TestSuite.php"/>
+ </filelist>
+ </release>
+ <changelog>
+ <release>
+ <version>1.0.1</version>
+ <date>2004-04-17</date>
+ <state>stable</state>
+ <notes>* Fixed a bug that let the HTML GUI fail for expected and/or actual
+ results with newline characters.
+
+ (Patch by Thiemo Maettig &lt;thiemo.maettig@gmx.de&gt;)
+
+* Fixed bug #1169.
+
+
+</notes>
+ </release>
+ <release>
+ <version>1.0.0</version>
+ <date>2004-03-15</date>
+ <state>stable</state>
+ <notes>* No functional changes since PHPUnit-0.6.2.
+
+
+</notes>
+ </release>
+ <release>
+ <version>0.6.2</version>
+ <date>2003-06-21</date>
+ <notes>* Fixed PHPUnit_Assert::assertType().
+
+
+</notes>
+ </release>
+ <release>
+ <version>0.6.1</version>
+ <date>2003-05-15</date>
+ <notes>+ Added assertType() assertion method.
+
+* Fixed parse error in GUI/HTML.php that occured with PHP 5.
+
+* Fixed a layout issue in the HTML GUI.
+
+
+</notes>
+ </release>
+ <release>
+ <version>0.6</version>
+ <date>2003-04-25</date>
+ <notes>* Fixed bug that caused the constructor detection in
+ PHPUnit_TestSuite to fail with upper-case class names.
+
+* Fixed bug with multiple assertions per Test Case.
+
+* call_user_func() was called with a copy instead of a reference
+ in PHPUnit_TestCase::runTest().
+
+* Assertion methods append the generated actual/expected
+ message to a user-defined message, rather than overwriting
+ it.
+
+* Constructors are now excluded from automatic TestCase creation
+ in TestSuite::addTestSuite().
+
+ Patch by Wolfram Kriesing &lt;wolfram@kriesing.de&gt;.
+
+* serialize() was called twice in PHPUnit_Assert::assertEquals().
+
+
+</notes>
+ </release>
+ <release>
+ <version>0.5</version>
+ <date>2003-03-26</date>
+ <notes>+ Added new HTML GUI.
+
+ Patch by Wolfram Kriesing &lt;wolfram@kriesing.de&gt;.
+
++ Added &quot;loosely typed&quot; mode to assertEquals() that can
+ be turned on/off using setLooselyTyped().
+
+ When activated, array elements are cast to strings
+ before comparison.
+
+* Allow multiple assertions per Test Case.
+
+* Use call_user_func() instead of $object-&gt;$method().
+
+
+</notes>
+ </release>
+ <release>
+ <version>0.4</version>
+ <date>2002-09-25</date>
+ <notes>+ Added PHPUnit_Assert::assertFalse() and PHPUnit_Assert::assertNotSame().
+ JUnit introduced those two convenience methods in the JUnit 3.8 release.
+
+* Fixed directory structure.
+
+
+</notes>
+ </release>
+ <release>
+ <version>0.3</version>
+ <date>2002-07-12</date>
+ <notes>* Added PHPUnit_TestDecorator and PHPUnit_RepeatedTest.
+
+* Implemented PHPUnit_TestResult::addListener() and
+ PHPUnit_TestResult::removeListener().
+
+* Added object support to PHPUnit_Assert::assertEquals().
+
+* Implemented PHPUnit_Assert::assertSame() using Zend
+ Engine 2 object handles.
+
+
+</notes>
+ </release>
+ <release>
+ <version>0.2</version>
+ <date>2002-07-11</date>
+ <notes>* Added Array support to PHPUnit_Assert::equals().
+ (Patch by Wolfram Kriesing &lt;wolfram@kriesing.de&gt;)
+
+* Added PHPUnit_Assert::assertRegExp().
+ (Patch by M@rms &lt;marms@marms.com&gt;)
+
+
+</notes>
+ </release>
+ <release>
+ <version>0.1</version>
+ <date>2002-04-06</date>
+ <notes>First release.
+
+
+</notes>
+ </release>
+ </changelog>
+</package>

0 comments on commit bc4c477

Please sign in to comment.