5 * Copyright (c) 2002-2009, Sebastian Bergmann <sb@sebastian-bergmann.de>.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * * Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
20 * * Neither the name of Sebastian Bergmann nor the names of his
21 * contributors may be used to endorse or promote products derived
22 * from this software without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
39 * @author Sebastian Bergmann <sb@sebastian-bergmann.de>
40 * @copyright 2002-2009 Sebastian Bergmann <sb@sebastian-bergmann.de>
41 * @license http://www.opensource.org/licenses/bsd-license.php BSD License
43 * @link http://www.phpunit.de/
44 * @since File available since Release 2.0.0
47 require_once 'PHPUnit/Framework/TestCase.php';
49 require_once dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'Error.php';
50 require_once dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'Failure.php';
51 require_once dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'NoArgTestCaseTest.php';
52 require_once dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'SetupFailure.php';
53 require_once dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'Success.php';
54 require_once dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'TearDownFailure.php';
55 require_once dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'ThrowExceptionTestCase.php';
56 require_once dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'ThrowNoExceptionTestCase.php';
57 require_once dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'TornDown.php';
58 require_once dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'TornDown2.php';
59 require_once dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'TornDown3.php';
60 require_once dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'TornDown4.php';
61 require_once dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'WasRun.php';
77 * @author Sebastian Bergmann <sb@sebastian-bergmann.de>
78 * @copyright 2002-2009 Sebastian Bergmann <sb@sebastian-bergmann.de>
79 * @license http://www.opensource.org/licenses/bsd-license.php BSD License
80 * @version Release: 3.3.17
81 * @link http://www.phpunit.de/
82 * @since Class available since Release 2.0.0
84 class Framework_TestCaseTest extends PHPUnit_Framework_TestCase
86 public function testCaseToString()
89 'testCaseToString(Framework_TestCaseTest)',
94 public function testError()
96 $this->verifyError(new Error);
99 public function testExceptionRunningAndTearDown()
101 $result = new PHPUnit_Framework_TestResult();
106 $errors = $result->errors();
110 $errors[0]->thrownException()->getMessage()
114 public function testFailure()
116 $this->verifyFailure(new Failure);
119 /* PHP does not support anonymous classes
120 public function testNamelessTestCase()
125 public function testNoArgTestCasePasses()
127 $result = new PHPUnit_Framework_TestResult();
128 $t = new PHPUnit_Framework_TestSuite('NoArgTestCaseTest');
132 $this->assertEquals(1, count($result));
133 $this->assertEquals(0, $result->failureCount());
134 $this->assertEquals(0, $result->errorCount());
137 public function testRunAndTearDownFails()
139 $fails = new TornDown2;
141 $this->verifyError($fails);
142 $this->assertTrue($fails->tornDown);
145 public function testSetupFails()
147 $this->verifyError(new SetupFailure);
150 public function testSuccess()
152 $this->verifySuccess(new Success);
155 public function testTearDownAfterError()
157 $fails = new TornDown;
159 $this->verifyError($fails);
160 $this->assertTrue($fails->tornDown);
163 public function testTearDownFails()
165 $this->verifyError(new TearDownFailure);
168 public function testTearDownSetupFails()
170 $fails = new TornDown3;
172 $this->verifyError($fails);
173 $this->assertFalse($fails->tornDown);
176 public function testWasRun()
181 $this->assertTrue($test->wasRun);
184 public function testException()
186 $test = new ThrowExceptionTestCase('test');
187 $test->setExpectedException('Exception');
189 $result = $test->run();
191 $this->assertEquals(1, count($result));
192 $this->assertTrue($result->wasSuccessful());
195 public function testNoException()
197 $test = new ThrowNoExceptionTestCase('test');
198 $test->setExpectedException('Exception');
200 $result = $test->run();
202 $this->assertEquals(1, $result->failureCount());
203 $this->assertEquals(1, count($result));
206 public function testWrongException()
208 $test = new ThrowExceptionTestCase('test');
209 $test->setExpectedException('RuntimeException');
211 $result = $test->run();
213 $this->assertEquals(1, $result->errorCount());
214 $this->assertEquals(1, count($result));
217 public function testGlobalsBackupPre()
221 $this->assertEquals('a', $a);
222 $this->assertEquals('a', $GLOBALS['a']);
223 $this->assertEquals('b', $_ENV['b']);
224 $this->assertEquals('c', $_POST['c']);
225 $this->assertEquals('d', $_GET['d']);
226 $this->assertEquals('e', $_COOKIE['e']);
227 $this->assertEquals('f', $_SERVER['f']);
228 $this->assertEquals('g', $_FILES['g']);
229 $this->assertEquals('h', $_REQUEST['h']);
231 $GLOBALS['a'] = 'aa';
232 $GLOBALS['foo'] = 'bar';
236 $_COOKIE['e'] = 'ee';
237 $_SERVER['f'] = 'ff';
239 $_REQUEST['h'] = 'hh';
241 $this->assertEquals('aa', $a);
242 $this->assertEquals('aa', $GLOBALS['a']);
243 $this->assertEquals('bar', $GLOBALS['foo']);
244 $this->assertEquals('bb', $_ENV['b']);
245 $this->assertEquals('cc', $_POST['c']);
246 $this->assertEquals('dd', $_GET['d']);
247 $this->assertEquals('ee', $_COOKIE['e']);
248 $this->assertEquals('ff', $_SERVER['f']);
249 $this->assertEquals('gg', $_FILES['g']);
250 $this->assertEquals('hh', $_REQUEST['h']);
253 public function testGlobalsBackupPost()
257 $this->assertEquals('a', $a);
258 $this->assertEquals('a', $GLOBALS['a']);
259 $this->assertEquals('b', $_ENV['b']);
260 $this->assertEquals('c', $_POST['c']);
261 $this->assertEquals('d', $_GET['d']);
262 $this->assertEquals('e', $_COOKIE['e']);
263 $this->assertEquals('f', $_SERVER['f']);
264 $this->assertEquals('g', $_FILES['g']);
265 $this->assertEquals('h', $_REQUEST['h']);
267 $this->assertArrayNotHasKey('foo', $GLOBALS);
270 protected function verifyError(PHPUnit_Framework_TestCase $test)
272 $result = $test->run();
274 $this->assertEquals(1, $result->errorCount());
275 $this->assertEquals(0, $result->failureCount());
276 $this->assertEquals(1, count($result));
279 protected function verifyFailure(PHPUnit_Framework_TestCase $test)
281 $result = $test->run();
283 $this->assertEquals(0, $result->errorCount());
284 $this->assertEquals(1, $result->failureCount());
285 $this->assertEquals(1, count($result));
288 protected function verifySuccess(PHPUnit_Framework_TestCase $test)
290 $result = $test->run();
292 $this->assertEquals(0, $result->errorCount());
293 $this->assertEquals(0, $result->failureCount());
294 $this->assertEquals(1, count($result));