Browse files

Added ability to ignore irrelevant exceptions left over at the end of…

… a test
  • Loading branch information...
1 parent 101b87a commit 06025a1889f2228967060ad5067c8729550352b7 lastcraft committed Jul 1, 2009
Showing with 93 additions and 22 deletions.
  1. +10 −8 docs/source/en/unit_test_documentation.xml
  2. +36 −8 exceptions.php
  3. +36 −6 test/exceptions_test.php
  4. +11 −0 unit_tester.php
View
18 docs/source/en/unit_test_documentation.xml
@@ -28,15 +28,15 @@ class FileTestCase extends UnitTestCase {
function FileTestCase() {
$this->UnitTestCase('File test');
}<strong>
-
+
function setUp() {
@unlink('../temp/test.txt');
}
-
+
function tearDown() {
@unlink('../temp/test.txt');
}
-
+
function testCreation() {
$writer = &new FileWriter('../temp/test.txt');
$writer->write('Hello');
@@ -99,7 +99,9 @@ class FileTestCase extends UnitTestCase {
<tr><td><code>assertClone($x, $y)</code></td><td>Fail unless $x and $y are identical copies</td></tr>
<tr><td><code>assertPattern($p, $x)</code></td><td>Fail unless the regex $p matches $x</td></tr>
<tr><td><code>assertNoPattern($p, $x)</code></td><td>Fail if the regex $p matches $x</td></tr>
- <tr><td><code>expectError($x)</code></td><td>Swallows any upcoming matching error</td></tr>
+ <tr><td><code>expectError($x)</code></td><td>Fail if matching error does not occour</td></tr>
+ <tr><td><code>expectException($x)</code></td><td>Fail if matching exception is not thrown</td></tr>
+ <tr><td><code>ignoreException($x)</code></td><td>Swallows any upcoming matching exception</td></tr>
<tr><td><code>assert($e)</code></td><td>Fail on failed <a local="expectation_documentation">expectation</a> object $e</td></tr>
</tbody></table>
All assertion methods can take an optional description as a
@@ -184,7 +186,7 @@ class FileTester extends UnitTestCase {
function FileTester($name = false) {
$this->UnitTestCase($name);
}
-
+
function assertFileExists($filename, $message = '%s') {
$this->assertTrue(
file_exists($filename),
@@ -210,15 +212,15 @@ class FileTester extends UnitTestCase {
a normal test case...
<php><![CDATA[
class FileTestCase extends <strong>FileTester</strong> {
-
+
function setUp() {
@unlink('../temp/test.txt');
}
-
+
function tearDown() {
@unlink('../temp/test.txt');
}
-
+
function testCreation() {
$writer = &new FileWriter('../temp/test.txt');
$writer->write('Hello');<strong>
View
44 exceptions.php
@@ -7,7 +7,7 @@
*/
/**#@+
- * Include required SimpleTest files
+ * Include required SimpleTest files
*/
require_once dirname(__FILE__) . '/invoker.php';
require_once dirname(__FILE__) . '/expectation.php';
@@ -136,6 +136,7 @@ protected function describeException($exception) {
*/
class SimpleExceptionTrap {
private $expected;
+ private $ignored;
private $message;
/**
@@ -154,17 +155,21 @@ function __construct() {
* @access public
*/
function expectException($expected = false, $message = '%s') {
- if ($expected === false) {
- $expected = new AnythingExpectation();
- }
- if (! SimpleExpectation::isExpectation($expected)) {
- $expected = new ExceptionExpectation($expected);
- }
- $this->expected = $expected;
+ $this->expected = $this->coerceToExpectation($expected);
$this->message = $message;
}
/**
+ * Adds an exception to the ignore list. This is the list
+ * of exceptions that when thrown do not affect the test.
+ * @param SimpleExpectation $ignored Exception to skip.
+ * @access public
+ */
+ function ignoreException($ignored) {
+ $this->ignored[] = $this->coerceToExpectation($ignored);
+ }
+
+ /**
* Compares the expected exception with any
* in the queue. Issues a pass or fail and
* returns the state of the test.
@@ -176,10 +181,32 @@ function isExpected($test, $exception) {
if ($this->expected) {
return $test->assert($this->expected, $exception, $this->message);
}
+ foreach ($this->ignored as $ignored) {
+ if ($ignored->test($exception)) {
+ return true;
+ }
+ }
return false;
}
/**
+ * Turns an expected exception into a SimpleExpectation object.
+ * @param mixed $exception Exception, expectation or
+ * class name of exception.
+ * @return SimpleExpectation Expectation that will match the
+ * exception.
+ */
+ private function coerceToExpectation($exception) {
+ if ($exception === false) {
+ return new AnythingExpectation();
+ }
+ if (! SimpleExpectation::isExpectation($exception)) {
+ return new ExceptionExpectation($exception);
+ }
+ return $exception;
+ }
+
+ /**
* Tests for any left over exception.
* @return string/false The failure message or false if none.
*/
@@ -193,6 +220,7 @@ function getOutstanding() {
function clear() {
$this->expected = false;
$this->message = false;
+ $this->ignored = array();
}
}
?>
View
42 test/exceptions_test.php
@@ -91,14 +91,44 @@ function testLastListedExceptionIsTheOneThatCounts() {
}
}
+class TestOfIgnoringExceptions extends UnitTestCase {
+
+ function testCanIgnoreAnyException() {
+ $this->ignoreException();
+ throw new Exception();
+ }
+
+ function testCanIgnoreSpecificException() {
+ $this->ignoreException('MyTestException');
+ throw new MyTestException();
+ }
+
+ function testCanIgnoreExceptionExactly() {
+ $this->ignoreException(new Exception('Ouch'));
+ throw new Exception('Ouch');
+ }
+
+ function testIgnoredExceptionsDoNotMaskExpectedExceptions() {
+ $this->ignoreException('Exception');
+ $this->expectException('MyTestException');
+ throw new MyTestException();
+ }
+
+ function testCanIgnoreMultipleExceptions() {
+ $this->ignoreException('MyTestException');
+ $this->ignoreException('OtherTestException');
+ throw new OtherTestException();
+ }
+}
+
class TestOfCallingTearDownAfterExceptions extends UnitTestCase {
private $debri = 0;
-
+
function tearDown() {
$this->debri--;
}
- function testLeaveSomeDebri() {
+ function testLeaveSomeDebri() {
$this->debri++;
$this->expectException();
throw new Exception(__FUNCTION__);
@@ -115,7 +145,7 @@ function setUp() {
$this->expectException();
throw new Exception();
}
-
+
function testShouldNotBeRun() {
$this->fail('This test body should not be run');
}
@@ -130,7 +160,7 @@ class TestOfExpectExceptionWithSetUp extends UnitTestCase {
function setUp() {
$this->expectException();
}
-
+
function testThisExceptionShouldBeCaught() {
throw new Exception();
}
@@ -141,11 +171,11 @@ function testJustThrowingMyTestException() {
}
class TestOfThrowingExceptionsInTearDown extends UnitTestCase {
-
+
function tearDown() {
throw new Exception();
}
-
+
function testDoesntFatal() {
$this->expectException();
}
View
11 unit_tester.php
@@ -381,6 +381,17 @@ function expectException($expected = false, $message = '%s') {
}
/**
+ * Tells SimpleTest to ignore an upcoming exception as not relevant
+ * to the current test. It doesn't affect the test, whether thrown or
+ * not.
+ * @param SimpleExpectation/Exception $ignored The error to ignore.
+ * @access public
+ */
+ function ignoreException($ignored = false) {
+ SimpleTest::getContext()->get('SimpleExceptionTrap')->ignoreException($ignored);
+ }
+
+ /**
* Creates an equality expectation if the
* object/value is not already some type
* of expectation.

0 comments on commit 06025a1

Please sign in to comment.