cvs: pear /PHPUnit PHPUnit.php PHPUnit_Assert.php PHPUnit_TestCase.php PHPUnit_TestFailure.php PHPUnit_TestResult.php PHPUnit_TestSuite.php
package.xml

From: Date: Tue, 27 Nov 2001 16:37:39 +0000
Subject: cvs: pear /PHPUnit PHPUnit.php PHPUnit_Assert.php PHPUnit_TestCase.php PHPUnit_TestFailure.php PHPUnit_TestResult.php PHPUnit_TestSuite.php
package.xml
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-1415@lists.php.net to get a copy of this message
sebastian Tue Nov 27 11:37:39 2001 EDT Added files: /pear/PHPUnit PHPUnit.php PHPUnit_Assert.php PHPUnit_TestCase.php PHPUnit_TestFailure.php PHPUnit_TestResult.php PHPUnit_TestSuite.php package.xml Log: Initial commit of PHPUnit.

Index: pear/PHPUnit/PHPUnit.php +++ pear/PHPUnit/PHPUnit.php <?php // // +----------------------------------------------------------------------+ // | PHP version 4.0 | // +----------------------------------------------------------------------+ // | Copyright (c) 1997-2001 The PHP Group | // +----------------------------------------------------------------------+ // | This source file is subject to version 2.02 of the PHP license, | // | that is bundled with this package in the file LICENSE, and is | // | available at through the world-wide-web at | // | http://www.php.net/license/2_02.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. | // +----------------------------------------------------------------------+ // | Author: Sebastian Bergmann <sb@sebastian-bergmann.de> | // +----------------------------------------------------------------------+ // // $Id: PHPUnit.php,v 1.1 2001/11/27 16:37:38 sebastian Exp $ // require_once 'PHPUnit/PHPUnit_TestSuite.php'; /** * PHPUnit runs a TestSuite and returns a TestResult object. * * Here is an example: * * require_once 'PHPUnit/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); * echo $result->toHTML(); * * 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: * * $suite = new PHPUnit_TestSuite('MathTest'); * $result = PHPUnit::run($suite); * echo $result->toHTML(); * * @package PHPUnit * @author Sebastian Bergmann <sb@sebastian-bergmann.de> * Based upon JUnit, see http://www.junit.org/ for details. */ class PHPUnit { function &run(&$suite) { $result = new PHPUnit_TestResult(); $suite->run($result); return $result; } } ?> Index: pear/PHPUnit/PHPUnit_Assert.php +++ pear/PHPUnit/PHPUnit_Assert.php <?php // // +----------------------------------------------------------------------+ // | PHP version 4.0 | // +----------------------------------------------------------------------+ // | Copyright (c) 1997-2001 The PHP Group | // +----------------------------------------------------------------------+ // | This source file is subject to version 2.02 of the PHP license, | // | that is bundled with this package in the file LICENSE, and is | // | available at through the world-wide-web at | // | http://www.php.net/license/2_02.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. | // +----------------------------------------------------------------------+ // | Author: Sebastian Bergmann <sb@sebastian-bergmann.de> | // +----------------------------------------------------------------------+ // // $Id: PHPUnit_Assert.php,v 1.1 2001/11/27 16:37:38 sebastian Exp $ // /** * A set of assert methods. * * @package PHPUnit * @author Sebastian Bergmann <sb@sebastian-bergmann.de> * Based upon JUnit, see http://www.junit.org/ for details. */ class PHPUnit_Assert { /** * Asserts that two variables are equal. * * @param mixed * @param mixed * @param string * @param mixed * @access public */ function assertEquals($expected, $actual, $message = '', $delta = 0) { if (empty($message)) { $_delta = ($delta != 0) ? ('+/- ' . $delta) : ''; $message = sprintf('expected %s%s, actual %s', $expected, $_delta, $actual ); } if (is_numeric($expected) && is_numeric($actual)) { if (!($actual >= ($expected - $delta) && $actual <= ($expected + $delta))) { return $this->fail($message); } } else { if ($expected != $actual) { return $this->fail($message); } } return $this->pass(); } /** * Asserts that an object isn't null. * * @param object * @param string * @access public */ function assertNotNull($object, $message = '') { if (empty($message)) { $message = 'expected NOT NULL, actual NULL'; } if ($object == NULL) { return $this->fail($message); } return $this->pass(); } /** * Asserts that an object is null. * * @param object * @param string * @access public */ function assertNull($object, $message = '') { if (empty($message)) { $message = 'expected NULL, actual NOT NULL'; } if ($object != NULL) { return $this->fail($message); } return $this->pass(); } /** * Asserts that two objects refer to the same object. * * @param object * @param object * @param string * @access public */ function assertSame($expected, $actual, $message = '') { // not implemented } /** * Asserts that a condition is true. * * @param boolean * @param string * @access public */ function assertTrue($condition, $message = '') { if (empty($message)) { $message = 'expected true, actual false'; } if (!$condition) { return $this->fail($message); } return $this->pass(); } /** * Fails a test with the given message. * * @param string * @access protected */ function fail($message = '') { } /** * Passes a test. * * @access protected */ function pass() { } } ?> Index: pear/PHPUnit/PHPUnit_TestCase.php +++ pear/PHPUnit/PHPUnit_TestCase.php <?php // // +----------------------------------------------------------------------+ // | PHP version 4.0 | // +----------------------------------------------------------------------+ // | Copyright (c) 1997-2001 The PHP Group | // +----------------------------------------------------------------------+ // | This source file is subject to version 2.02 of the PHP license, | // | that is bundled with this package in the file LICENSE, and is | // | available at through the world-wide-web at | // | http://www.php.net/license/2_02.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. | // +----------------------------------------------------------------------+ // | Author: Sebastian Bergmann <sb@sebastian-bergmann.de> | // +----------------------------------------------------------------------+ // // $Id: PHPUnit_TestCase.php,v 1.1 2001/11/27 16:37:38 sebastian Exp $ // require_once 'PHPUnit/PHPUnit_Assert.php'; require_once 'PHPUnit/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: * * class MathTest extends PHPUnit_TestCase { * var $fValue1; * var $fValue2; * * function MathTest($name) { * $this->PHPUnit_TestCase($name); * } * * function setUp() { * $this->fValue1 = 2; * $this->fValue2 = 3; * } * } * * For each test implement a method which interacts with the fixture. * Verify the expected results with assertions specified by calling * assert with a boolean. * * function testPass() { * $this->assertTrue($this->fValue1 + $this->fValue2 == 5); * } * * @package PHPUnit * @author Sebastian Bergmann <sb@sebastian-bergmann.de> * Based upon JUnit, see http://www.junit.org/ for details. */ class PHPUnit_TestCase extends PHPUnit_Assert { /** * The name of the test case. * * @var string * @access private */ var $fName = ''; /** * PHPUnit_TestResult object * * @var object * @access private */ var $fResult; /** * 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->fName; } /** * Runs the test case and collects the results in a given TestResult object. * * @param object * @return object * @access public */ function run(&$result) { $this->fResult = &$result; $this->fResult->run(&$this); return $this->fResult; } /** * Runs the bare test sequence. * * @access public */ function runBare() { $this->setUp(); $this->runTest(); $this->tearDown(); } /** * Override to run the test and assert its state. * * @access protected */ function runTest() { $name = $this->fName; $this->$name(); } /** * Sets the name of a TestCase. * * @param string * @access public */ function setName($name) { $this->fName = $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() { // not implemented } /** * Fails a test with the given message. * * @param string * @access protected */ function fail($message = '') { $this->fResult->addFailure($this, $message); } /** * Passes a test. * * @access protected */ function pass() { $this->fResult->addPassedTest($this); } /** * Sets up the fixture, for example, open a network connection. * This method is called before a test is executed. * * @access protected */ function setUp() { } /** * Tears down the fixture, for example, close a network connection. * This method is called after a test is executed. * * @access protected */ function tearDown() { } } ?> Index: pear/PHPUnit/PHPUnit_TestFailure.php +++ pear/PHPUnit/PHPUnit_TestFailure.php <?php // // +----------------------------------------------------------------------+ // | PHP version 4.0 | // +----------------------------------------------------------------------+ // | Copyright (c) 1997-2001 The PHP Group | // +----------------------------------------------------------------------+ // | This source file is subject to version 2.02 of the PHP license, | // | that is bundled with this package in the file LICENSE, and is | // | available at through the world-wide-web at | // | http://www.php.net/license/2_02.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. | // +----------------------------------------------------------------------+ // | Author: Sebastian Bergmann <sb@sebastian-bergmann.de> | // +----------------------------------------------------------------------+ // // $Id: PHPUnit_TestFailure.php,v 1.1 2001/11/27 16:37:38 sebastian Exp $ // /** * A TestFailure collects a failed test together with the caught exception. * * @package PHPUnit * @author Sebastian Bergmann <sb@sebastian-bergmann.de> * Based upon JUnit, see http://www.junit.org/ for details. */ class PHPUnit_TestFailure { /** * @var object * @access private */ var $fFailedTest; /** * @var string * @access private */ var $fThrownException; /** * Constructs a TestFailure with the given test and exception. * * @param object * @param string * @access public */ function PHPUnit_TestFailure(&$failedTest, &$thrownException) { $this->fFailedTest = $failedTest; $this->fThrownException = $thrownException; } /** * Gets the failed test. * * @return object * @access public */ function &failedTest() { return $this->fFailedTest; } /** * Gets the thrown exception. * * @return object * @access public */ function &thrownException() { return $this->fThrownException; } /** * Returns a short description of the failure. * * @return string * @access public */ function toString() { return sprintf("TestCase %s->%s() failed: %s\n", get_class($this->fFailedTest), $this->fFailedTest->getName(), $this->fThrownException ); } } ?> Index: pear/PHPUnit/PHPUnit_TestResult.php +++ pear/PHPUnit/PHPUnit_TestResult.php <?php // // +----------------------------------------------------------------------+ // | PHP version 4.0 | // +----------------------------------------------------------------------+ // | Copyright (c) 1997-2001 The PHP Group | // +----------------------------------------------------------------------+ // | This source file is subject to version 2.02 of the PHP license, | // | that is bundled with this package in the file LICENSE, and is | // | available at through the world-wide-web at | // | http://www.php.net/license/2_02.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. | // +----------------------------------------------------------------------+ // | Author: Sebastian Bergmann <sb@sebastian-bergmann.de> | // +----------------------------------------------------------------------+ // // $Id: PHPUnit_TestResult.php,v 1.1 2001/11/27 16:37:38 sebastian Exp $ // require_once 'PHPUnit/PHPUnit_TestFailure.php'; /** * A TestResult collects the results of executing a test case. * * @package PHPUnit * @author Sebastian Bergmann <sb@sebastian-bergmann.de> * Based upon JUnit, see http://www.junit.org/ for details. */ class PHPUnit_TestResult { /** * @var array * @access protected */ var $fErrors = array(); /** * @var array * @access protected */ var $fFailures = array(); /** * @var array * @access protected */ var $fListeners = array(); /** * @var array * @access protected */ var $fPassedTests = array(); /** * @var integer * @access protected */ var $fRunTests = 0; /** * @var boolean * @access private */ var $fStop = 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) { // not implemented } /** * 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->fFailures[] = new PHPUnit_TestFailure($test, $t); } /** * Registers a TestListener. * * @param object * @access public */ function addListener(&$listener) { // not implemented } /** * Adds a passed test to the list of passed tests. * * @param object * @access public */ function addPassedTest(&$test) { $this->fPassedTests[] = $test; } /** * Informs the result that a test was completed. * * @param object * @access public */ function endTest(&$test) { } /** * Gets the number of detected errors. * * @return integer * @access public */ function errorCount() { return sizeof($this->fErrors); } /** * Returns an Enumeration for the errors. * * @return array * @access public */ function &errors() { return $this->fErrors; } /** * Gets the number of detected failures. * * @return integer * @access public */ function failureCount() { return sizeof($this->fFailures); } /** * Returns an Enumeration for the failures. * * @return array * @access public */ function &failures() { return $this->fFailures; } /** * Unregisters a TestListener. * * @param object * @access public */ function removeListener(&$listener) { // not implemented } /** * Runs a TestCase. * * @param object * @access public */ function run(&$test) { $this->startTest($test); $this->fRunTests++; $test->runBare(); $this->endTest($test); } /** * Gets the number of run tests. * * @return integer * @access public */ function runCount() { return $this->fRunTests; } /** * Runs a TestCase. * * @param object * @param object * @access public */ function runProtected($test, $p) { // not implemented } /** * Checks whether the test run should stop. * * @access public */ function shouldStop() { return $this->fStop; } /** * Informs the result that a test will be started. * * @param object * @access public */ function startTest(&$test) { } /** * Marks that the test run should stop. * * @access public */ function stop() { $this->fStop = 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->fPassedTests as $passedTest) { $result .= sprintf("TestCase %s->%s() passed\n", get_class($passedTest), $passedTest->getName() ); } foreach ($this->fFailures 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->fErrors) && empty($this->fFailures)) { return true; } else { return false; } } } ?> Index: pear/PHPUnit/PHPUnit_TestSuite.php +++ pear/PHPUnit/PHPUnit_TestSuite.php <?php // // +----------------------------------------------------------------------+ // | PHP version 4.0 | // +----------------------------------------------------------------------+ // | Copyright (c) 1997-2001 The PHP Group | // +----------------------------------------------------------------------+ // | This source file is subject to version 2.02 of the PHP license, | // | that is bundled with this package in the file LICENSE, and is | // | available at through the world-wide-web at | // | http://www.php.net/license/2_02.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. | // +----------------------------------------------------------------------+ // | Author: Sebastian Bergmann <sb@sebastian-bergmann.de> | // +----------------------------------------------------------------------+ // // $Id: PHPUnit_TestSuite.php,v 1.1 2001/11/27 16:37:38 sebastian Exp $ // require_once 'PHPUnit/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. * * $suite = new PHPUnit_TestSuite(); * $suite->addTest(new MathTest('testPass')); * * 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. * * $suite = new TestSuite('classname'); * * This constructor creates a suite with all the methods starting with * "test" that take no arguments. * * @package PHPUnit * @author Sebastian Bergmann <sb@sebastian-bergmann.de> * Based upon JUnit, see http://www.junit.org/ for details. */ class PHPUnit_TestSuite { /** * The name of the test suite. * * @var string * @access private */ var $fName = ''; /** * The tests in the test suite. * * @var array * @access private */ var $fTests = array(); /** * Constructs a TestSuite. * * @param mixed * @access public */ function PHPUnit_TestSuite($test = false) { if ($test && class_exists($test)) { $this->setName($test); $methods = get_class_methods($test); foreach ($methods as $method) { if (substr($method, 0, 4) == 'test' && $method != 'phpunit_testcase') { $this->addTest(new $test($method)); } } } } /** * Adds a test to the suite. * * @param object * @access public */ function addTest(&$test) { $this->fTests[] = $test; } /** * * * @param string * @param array * @param string * @access private */ function addTestMethod($m, $names, $constructor) { // not implemented } /** * Adds the tests from the given class to the suite. * * @param string * @access public */ function addTestSuite($testClass) { // not implemented } /** * Counts the number of test cases that will be run by this test. * * @return integer * @access public */ function countTestCases() { $count = 0; foreach ($this->fTests as $test) { $count += $test->countTestCases(); } return $count; } /** * Converts the stack trace into a string. * * @param object * @return string * @access private */ function exceptionToString($t) { // not implemented } /** * Gets a constructor which takes a single String as its argument. * * @param string * @return string * @access private */ function getConstructor($theClass) { // not implemented } /** * Returns the name of the suite. * * @return string * @access public */ function getName() { return $this->fName; } /** * Runs the tests and collects their result in a TestResult. * * @param object * @access public */ function run(&$result) { for ($i = 0; $i < sizeof($this->fTests) && !$result->shouldStop(); $i++) { $this->fTests[$i]->run($result); } } /** * * * @param object * @param object * @access public */ function runTest(&$test, &$result) { } /** * Sets the name of the suite. * * @param string * @access public */ function setName($name) { $this->fName = $name; } /** * Returns the test at the given index. * * @param integer * @return object * @access public */ function &testAt($index) { if (isset($this->fTests[$index])) { return $this->fTests[$index]; } else { return false; } } /** * Returns the number of tests in this suite. * * @return integer * @access public */ function testCount() { return sizeof($this->fTests); } /** * Returns the tests as an enumeration. * * @return array * @access public */ function &tests() { return $this->fTests; } /** * Returns a string representation of the test suite. * * @return string * @access public */ function toString() { return ''; } } ?> Index: pear/PHPUnit/package.xml +++ pear/PHPUnit/package.xml <?xml version="1.0" encoding="ISO-8859-1" ?> <package> <name>PHPUnit</name> <summary></summary> <maintainer> <user>sebastian</user> <name>Sebastian Bergmann</name> <email>sebastian@php.net</email> </maintainer> <filelist> <dir name="/" baseinstalldir="PHPUnit"> <file role="php">PHPUnit.php</file> <file role="php">PHPUnit_Assert.php</file> <file role="php">PHPUnit_TestCase.php</file> <file role="php">PHPUnit_TestFailure.php</file> <file role="php">PHPUnit_TestResult.php</file> <file role="php">PHPUnit_TestSuite.php</file> </dir> </filelist> </package>
« previous php.pear.cvs (#1415) next »