small makefile tweak
[plcapi.git] / test / PHPUnit / TestCase.php
1 <?php
2 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
3
4 /**
5  * PHP Version 4
6  *
7  * LICENSE: This source file is subject to version 3.0 of the PHP license
8  * that is available through the world-wide-web at the following URI:
9  * http://www.php.net/license/3_0.txt.  If you did not receive a copy of
10  * the PHP License and are unable to obtain it through the web, please
11  * send a note to license@php.net so we can mail you a copy immediately.
12  *
13  * @category   Testing
14  * @package    PHPUnit
15  * @author     Sebastian Bergmann <sb@sebastian-bergmann.de>
16  * @copyright  2002-2005 Sebastian Bergmann <sb@sebastian-bergmann.de>
17  * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
18  * @version    CVS: $Id$
19  * @link       http://pear.php.net/package/PHPUnit
20  * @since      File available since Release 1.0.0
21  */
22
23 require_once 'PHPUnit/Assert.php';
24 require_once 'PHPUnit/TestResult.php';
25
26 /**
27  * A TestCase defines the fixture to run multiple tests.
28  *
29  * To define a TestCase
30  *
31  *   1) Implement a subclass of PHPUnit_TestCase.
32  *   2) Define instance variables that store the state of the fixture.
33  *   3) Initialize the fixture state by overriding setUp().
34  *   4) Clean-up after a test by overriding tearDown().
35  *
36  * Each test runs in its own fixture so there can be no side effects
37  * among test runs.
38  *
39  * Here is an example:
40  *
41  * <code>
42  * <?php
43  * class MathTest extends PHPUnit_TestCase {
44  *     var $fValue1;
45  *     var $fValue2;
46  *
47  *     function MathTest($name) {
48  *         $this->PHPUnit_TestCase($name);
49  *     }
50  *
51  *     function setUp() {
52  *         $this->fValue1 = 2;
53  *         $this->fValue2 = 3;
54  *     }
55  * }
56  * ?>
57  * </code>
58  *
59  * For each test implement a method which interacts with the fixture.
60  * Verify the expected results with assertions specified by calling
61  * assert with a boolean.
62  *
63  * <code>
64  * <?php
65  * function testPass() {
66  *     $this->assertTrue($this->fValue1 + $this->fValue2 == 5);
67  * }
68  * ?>
69  * </code>
70  *
71  * @category   Testing
72  * @package    PHPUnit
73  * @author     Sebastian Bergmann <sb@sebastian-bergmann.de>
74  * @copyright  2002-2005 Sebastian Bergmann <sb@sebastian-bergmann.de>
75  * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
76  * @version    Release: @package_version@
77  * @link       http://pear.php.net/package/PHPUnit
78  * @since      Class available since Release 1.0.0
79  */
80 class PHPUnit_TestCase extends PHPUnit_Assert {
81     /**
82      * @var    boolean
83      * @access private
84      */
85     var $_failed = FALSE;
86
87     /**
88      * The name of the test case.
89      *
90      * @var    string
91      * @access private
92      */
93     var $_name = '';
94
95     /**
96      * PHPUnit_TestResult object
97      *
98      * @var    object
99      * @access private
100      */
101     var $_result;
102
103     /**
104      * Constructs a test case with the given name.
105      *
106      * @param  string
107      * @access public
108      */
109     function PHPUnit_TestCase($name = FALSE) {
110         if ($name !== FALSE) {
111             $this->setName($name);
112         }
113     }
114
115     /**
116      * Counts the number of test cases executed by run(TestResult result).
117      *
118      * @return integer
119      * @access public
120      */
121     function countTestCases() {
122         return 1;
123     }
124
125     /**
126      * Gets the name of a TestCase.
127      *
128      * @return string
129      * @access public
130      */
131     function getName() {
132         return $this->_name;
133     }
134
135     /**
136      * Runs the test case and collects the results in a given TestResult object.
137      *
138      * @param  object
139      * @return object
140      * @access public
141      */
142     function run(&$result) {
143         $this->_result = &$result;
144         $this->_result->run($this);
145
146         return $this->_result;
147     }
148
149     /**
150      * Runs the bare test sequence.
151      *
152      * @access public
153      */
154     function runBare() {
155         $this->setUp();
156         $this->runTest();
157         $this->tearDown();
158         $this->pass();
159     }
160
161     /**
162      * Override to run the test and assert its state.
163      *
164      * @access protected
165      */
166     function runTest() {
167         call_user_func(
168           array(
169             &$this,
170             $this->_name
171           )
172         );
173     }
174
175     /**
176      * Sets the name of a TestCase.
177      *
178      * @param  string
179      * @access public
180      */
181     function setName($name) {
182         $this->_name = $name;
183     }
184
185     /**
186      * Returns a string representation of the test case.
187      *
188      * @return string
189      * @access public
190      */
191     function toString() {
192         return '';
193     }
194
195     /**
196      * Creates a default TestResult object.
197      *
198      * @return object
199      * @access protected
200      */
201     function &createResult() {
202         return new PHPUnit_TestResult;
203     }
204
205     /**
206      * Fails a test with the given message.
207      *
208      * @param  string
209      * @access protected
210      */
211     function fail($message = '') {
212         if (function_exists('debug_backtrace')) {
213             $trace = debug_backtrace();
214
215             if (isset($trace['1']['file'])) {
216                 $message = sprintf(
217                   "%s in %s:%s",
218
219                   $message,
220                   $trace['1']['file'],
221                   $trace['1']['line']
222                 );
223             }
224         }
225
226         $this->_result->addFailure($this, $message);
227         $this->_failed = TRUE;
228     }
229
230     /**
231      * Passes a test.
232      *
233      * @access protected
234      */
235     function pass() {
236         if (!$this->_failed) {
237             $this->_result->addPassedTest($this);
238         }
239     }
240
241     /**
242      * Sets up the fixture, for example, open a network connection.
243      * This method is called before a test is executed.
244      *
245      * @access protected
246      * @abstract
247      */
248     function setUp() { /* abstract */ }
249
250     /**
251      * Tears down the fixture, for example, close a network connection.
252      * This method is called after a test is executed.
253      *
254      * @access protected
255      * @abstract
256      */
257     function tearDown() { /* abstract */ }
258 }
259
260 /*
261  * Local variables:
262  * tab-width: 4
263  * c-basic-offset: 4
264  * c-hanging-comment-ender-p: nil
265  * End:
266  */
267 ?>