premier commit
[bazdig.git] / test / PHPUnit / TestCase.php
blob93d49f554468ec01a77b1a83da6edea3ec309656
1 <?php
2 //
3 // +------------------------------------------------------------------------+
4 // | PEAR :: PHPUnit |
5 // +------------------------------------------------------------------------+
6 // | Copyright (c) 2002-2003 Sebastian Bergmann <sb@sebastian-bergmann.de>. |
7 // +------------------------------------------------------------------------+
8 // | This source file is subject to version 3.00 of the PHP License, |
9 // | that is available at http://www.php.net/license/3_0.txt. |
10 // | If you did not receive a copy of the PHP license and are unable to |
11 // | obtain it through the world-wide-web, please send a note to |
12 // | license@php.net so we can mail you a copy immediately. |
13 // +------------------------------------------------------------------------+
15 // $Id: TestCase.php,v 1.15 2004/10/01 06:11:39 sebastian Exp $
18 require_once 'PHPUnit/Assert.php';
19 require_once 'PHPUnit/TestResult.php';
21 /**
22 * A TestCase defines the fixture to run multiple tests.
24 * To define a TestCase
26 * 1) Implement a subclass of PHPUnit_TestCase.
27 * 2) Define instance variables that store the state of the fixture.
28 * 3) Initialize the fixture state by overriding setUp().
29 * 4) Clean-up after a test by overriding tearDown().
31 * Each test runs in its own fixture so there can be no side effects
32 * among test runs.
34 * Here is an example:
36 * <code>
37 * <?php
38 * class MathTest extends PHPUnit_TestCase {
39 * var $fValue1;
40 * var $fValue2;
42 * function MathTest($name) {
43 * $this->PHPUnit_TestCase($name);
44 * }
46 * function setUp() {
47 * $this->fValue1 = 2;
48 * $this->fValue2 = 3;
49 * }
50 * }
51 * ?>
52 * </code>
54 * For each test implement a method which interacts with the fixture.
55 * Verify the expected results with assertions specified by calling
56 * assert with a boolean.
58 * <code>
59 * <?php
60 * function testPass() {
61 * $this->assertTrue($this->fValue1 + $this->fValue2 == 5);
62 * }
63 * ?>
64 * </code>
66 * @author Sebastian Bergmann <sb@sebastian-bergmann.de>
67 * @copyright Copyright &copy; 2002-2004 Sebastian Bergmann <sb@sebastian-bergmann.de>
68 * @license http://www.php.net/license/3_0.txt The PHP License, Version 3.0
69 * @category Testing
70 * @package PHPUnit
72 class PHPUnit_TestCase extends PHPUnit_Assert {
73 /**
74 * @var boolean
75 * @access private
77 var $_failed = FALSE;
79 /**
80 * The name of the test case.
82 * @var string
83 * @access private
85 var $_name = '';
87 /**
88 * PHPUnit_TestResult object
90 * @var object
91 * @access private
93 var $_result;
95 /**
96 * Constructs a test case with the given name.
98 * @param string
99 * @access public
101 function PHPUnit_TestCase($name = FALSE) {
102 if ($name !== FALSE) {
103 $this->setName($name);
108 * Counts the number of test cases executed by run(TestResult result).
110 * @return integer
111 * @access public
113 function countTestCases() {
114 return 1;
118 * Gets the name of a TestCase.
120 * @return string
121 * @access public
123 function getName() {
124 return $this->_name;
128 * Runs the test case and collects the results in a given TestResult object.
130 * @param object
131 * @return object
132 * @access public
134 function run(&$result) {
135 $this->_result = &$result;
136 $this->_result->run($this);
138 return $this->_result;
142 * Runs the bare test sequence.
144 * @access public
146 function runBare() {
147 $this->setUp();
148 $this->runTest();
149 $this->tearDown();
150 $this->pass();
154 * Override to run the test and assert its state.
156 * @access protected
158 function runTest() {
159 call_user_func(
160 array(
161 &$this,
162 $this->_name
168 * Sets the name of a TestCase.
170 * @param string
171 * @access public
173 function setName($name) {
174 $this->_name = $name;
178 * Returns a string representation of the test case.
180 * @return string
181 * @access public
183 function toString() {
184 return '';
188 * Creates a default TestResult object.
190 * @return object
191 * @access protected
193 function &createResult() {
194 return new PHPUnit_TestResult;
198 * Fails a test with the given message.
200 * @param string
201 * @access protected
203 function fail($message = '') {
204 $this->_result->addFailure($this, $message);
205 $this->_failed = TRUE;
209 * Passes a test.
211 * @access protected
213 function pass() {
214 if (!$this->_failed) {
215 $this->_result->addPassedTest($this);
220 * Sets up the fixture, for example, open a network connection.
221 * This method is called before a test is executed.
223 * @access protected
224 * @abstract
226 function setUp() { /* abstract */ }
229 * Tears down the fixture, for example, close a network connection.
230 * This method is called after a test is executed.
232 * @access protected
233 * @abstract
235 function tearDown() { /* abstract */ }